From 2c4ff851b1a54a6664106194693ae057ecd02291 Mon Sep 17 00:00:00 2001 From: mgrojo Date: Sun, 18 Nov 2018 01:44:59 +0100 Subject: [PATCH] Comment/uncomment code command for the SQL editor A new command is added for commenting or uncommenting SQL code (depending of the state of the first line of the block). It is bound to "Ctrl+/" as used by many IDEs ("Ctrl+-" is more standard as Zoom out). Unusual Scintilla binding for "Ctrl+/" is removed for avoiding interference. If there is no selection, the current line is un/commented. See issue #1614 --- src/sqltextedit.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/sqltextedit.h | 3 +++ 2 files changed, 47 insertions(+) diff --git a/src/sqltextedit.cpp b/src/sqltextedit.cpp index d05c7beb..f5b73565 100644 --- a/src/sqltextedit.cpp +++ b/src/sqltextedit.cpp @@ -3,6 +3,12 @@ #include "Settings.h" #include "SqlUiLexer.h" +#include +#include + +#include +#include + SqlUiLexer* SqlTextEdit::sqlLexer = nullptr; SqlTextEdit::SqlTextEdit(QWidget* parent) : @@ -22,6 +28,15 @@ SqlTextEdit::SqlTextEdit(QWidget* parent) : registerImage(SqlUiLexer::ApiCompleterIconIdColumn, QImage(":/icons/field")); registerImage(SqlUiLexer::ApiCompleterIconIdSchema, QImage(":/icons/database")); + // Remove command bindings that would interfere with our shortcutToggleComment + QsciCommand * command = standardCommands()->boundTo(Qt::ControlModifier+Qt::Key_Slash); + command->setKey(0); + command = standardCommands()->boundTo(Qt::ControlModifier+Qt::ShiftModifier+Qt::Key_Slash); + command->setKey(0); + + QShortcut* shortcutToggleComment = new QShortcut(QKeySequence(tr("Ctrl+/")), this, nullptr, nullptr, Qt::WidgetShortcut); + connect(shortcutToggleComment, &QShortcut::activated, this, &SqlTextEdit::toggleBlockComment); + // Do rest of initialisation reloadSettings(); } @@ -69,3 +84,32 @@ void SqlTextEdit::reloadSettings() setupSyntaxHighlightingFormat(sqlLexer, "identifier", QsciLexerSQL::Identifier); setupSyntaxHighlightingFormat(sqlLexer, "identifier", QsciLexerSQL::QuotedIdentifier); } + + +void SqlTextEdit::toggleBlockComment() +{ + int lineFrom, indexFrom, lineTo, indexTo; + // If there is no selection, select the current line + if (!hasSelectedText()) { + getCursorPosition(&lineFrom, &indexFrom); + setSelection(lineFrom, 0, lineFrom, lineLength(lineFrom)); + } + + getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo); + + bool uncomment = text(lineFrom).contains(QRegExp("^[ \t]*--")); + + // Iterate over the selected lines, get line text, make + // replacement depending on whether the first line was commented + // or uncommented, and replace the line text. + for (int line=lineFrom; line