From 33266b7707cc80bb077e6f4beac245d759fc48d5 Mon Sep 17 00:00:00 2001 From: mgrojo Date: Wed, 21 Nov 2018 21:13:40 +0100 Subject: [PATCH] Improvement and fixes to the un/comment block feature The last line of the selection is considered part of the block, even if it is not completely selected. At the same time, we have to discard this last line of the selection, if the index is 0, since in that case there is no actual selected character in the line. Additionally all the steps in the replacement are considered a single action for history, so that user can undo the change in a single undo action. See issue #1614 --- src/sqltextedit.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/sqltextedit.cpp b/src/sqltextedit.cpp index 30380b0c..5bc978e0 100644 --- a/src/sqltextedit.cpp +++ b/src/sqltextedit.cpp @@ -89,20 +89,33 @@ void SqlTextEdit::reloadSettings() 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)); + + // Windows lines requires an adjustment, otherwise the selection would + // end in the next line. + indexTo = text(lineFrom).endsWith("\r\n") ? lineLength(lineFrom)-1 : lineLength(lineFrom); + + setSelection(lineFrom, 0, lineFrom, indexTo); } getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo); bool uncomment = text(lineFrom).contains(QRegExp("^[ \t]*--")); + // If the selection ends before the first character of a line, don't + // take this line into account for un/commenting. + if (indexTo==0) + lineTo--; + + beginUndoAction(); + // 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