diff options
| author | 2016-06-13 18:42:42 -0400 | |
|---|---|---|
| committer | 2016-06-13 18:42:42 -0400 | |
| commit | 5d9314f134ddd3dc4c853e398ac90ba247fb2e4f (patch) | |
| tree | 5c457fc188036988d7abd29a3eb09931e406510f /SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp | |
| parent | 8e640722c62692818ab840d50b3758f89a41a54e (diff) | |
Imported Upstream version 3.1.0upstream/3.1.0
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp')
| -rw-r--r-- | SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp | 101 |
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()) |
