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
This commit is contained in:
mgrojo
2018-11-21 21:13:40 +01:00
parent 17827ce5be
commit 33266b7707

View File

@@ -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<lineTo; line++) {
// or uncommented, and replace the line text. All in a single undo action.
for (int line=lineFrom; line<=lineTo; line++) {
QString lineText = text(line);
if (uncomment)
@@ -115,4 +128,5 @@ void SqlTextEdit::toggleBlockComment()
setSelection(line, 0, line, indexTo);
replaceSelectedText(lineText);
}
endUndoAction();
}