aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@ubuntu.com>2016-06-13 18:42:57 -0400
committerLibravatarUnit 193 <unit193@ubuntu.com>2016-06-13 18:42:57 -0400
commit65d5f68cc6dc81799c5a5d90400a2c1f0dd02547 (patch)
tree6f245ba901b87ef42fed69965aea22f5eea6b590 /SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp
parentaeb6bad01630d325a4e768e798a7a6d44e18fdaf (diff)
parent5d9314f134ddd3dc4c853e398ac90ba247fb2e4f (diff)
Merge tag 'upstream/3.1.0'
Upstream version 3.1.0 # gpg: Signature made Mon 13 Jun 2016 06:42:54 PM EDT using RSA key ID EBE9BD91 # gpg: Good signature from "Unit 193 <unit193@gmail.com>" # gpg: aka "Unit 193 <unit193@ninthfloor.org>" # gpg: aka "Unit 193 <unit193@ubuntu.com>" # gpg: aka "Unit 193 <unit193@ninthfloor.com>"
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp')
-rw-r--r--SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp b/SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp
index 4b0628b..9dc85f7 100644
--- a/SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp
+++ b/SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp
@@ -139,6 +139,7 @@ void SqlEditor::createActions()
createAction(FIND_NEXT, tr("Find next", "sql editor"), this, SLOT(findNext()), this);
createAction(FIND_PREV, tr("Find previous", "sql editor"), this, SLOT(findPrevious()), this);
createAction(REPLACE, tr("Replace", "sql editor"), this, SLOT(replace()), this);
+ createAction(TOGGLE_COMMENT, tr("Toggle comment", "sql editor"), this, SLOT(toggleComment()), this);
actionMap[CUT]->setEnabled(false);
actionMap[COPY]->setEnabled(false);
@@ -255,6 +256,21 @@ void SqlEditor::saveToFile(const QString &fileName)
notifyInfo(tr("Saved SQL contents to file: %1").arg(fileName));
}
+void SqlEditor::toggleLineCommentForLine(const QTextBlock& block)
+{
+ QTextCursor cur = textCursor();
+ QString line = block.text();
+ cur.setPosition(block.position());
+ if (line.startsWith("--"))
+ {
+ cur.deleteChar();
+ cur.deleteChar();
+ }
+ else
+ cur.insertText("--");
+
+}
+
void SqlEditor::updateUndoAction(bool enabled)
{
actionMap[UNDO]->setEnabled(enabled);
@@ -1298,6 +1314,91 @@ void SqlEditor::configModified()
highlighter->rehighlight();
}
+void SqlEditor::toggleComment()
+{
+ // Handle no selection - toggle single line
+ QTextCursor cur = textCursor();
+ int start = cur.selectionStart();
+ int end = cur.selectionEnd();
+
+ if (start == end)
+ {
+ toggleLineCommentForLine(cur.block());
+ return;
+ }
+
+ // Handle multiline selection - from begin of the line to begin of the line
+ QTextDocument* doc = document();
+
+ QTextBlock startBlock = doc->findBlock(start);
+ bool startAtLineBegining = startBlock.position() == start;
+
+ QTextBlock endBlock = doc->findBlock(end);
+ bool endAtLineBegining = endBlock.position() == end;
+
+ if (startAtLineBegining && endAtLineBegining)
+ {
+ // Check if all lines where commented previously
+ bool allCommented = true;
+ for (QTextBlock theBlock = startBlock; theBlock != endBlock; theBlock = theBlock.next())
+ {
+ if (!theBlock.text().startsWith("--"))
+ {
+ allCommented = false;
+ break;
+ }
+ }
+
+ // Apply comment toggle
+ cur.beginEditBlock();
+ for (QTextBlock theBlock = startBlock; theBlock != endBlock; theBlock = theBlock.next())
+ {
+ cur.setPosition(theBlock.position());
+ if (allCommented)
+ {
+ cur.deleteChar();
+ cur.deleteChar();
+ }
+ else
+ cur.insertText("--");
+ }
+
+ cur.setPosition(start);
+ cur.setPosition(endBlock.position(), QTextCursor::KeepAnchor);
+ cur.endEditBlock();
+ setTextCursor(cur);
+ return;
+ }
+
+ // Handle custom selection
+ QString txt = cur.selectedText().trimmed();
+ cur.beginEditBlock();
+ if (txt.startsWith("/*") && txt.endsWith("*/"))
+ {
+ cur.setPosition(end);
+ cur.deletePreviousChar();
+ cur.deletePreviousChar();
+ cur.setPosition(start);
+ cur.deleteChar();
+ cur.deleteChar();
+
+ cur.setPosition(start);
+ cur.setPosition(end - 4, QTextCursor::KeepAnchor);
+ }
+ else
+ {
+ cur.setPosition(end);
+ cur.insertText("*/");
+ cur.setPosition(start);
+ cur.insertText("/*");
+
+ cur.setPosition(start);
+ cur.setPosition(end + 4, QTextCursor::KeepAnchor);
+ }
+ cur.endEditBlock();
+ setTextCursor(cur);
+}
+
void SqlEditor::keyPressEvent(QKeyEvent* e)
{
switch (e->key())