Skip to content

Commit c2a4861

Browse files
committed
SQL Editor: incorrect commenting/uncommenting with non-US-ASCII characters
- Selection works with characters, but lineLength returns bytes. - QRegExp changed to QRegularExpression (recommended by Qt) See issue sqlitebrowser#3328
1 parent 9a3ebd1 commit c2a4861

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/sqltextedit.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <Qsci/qscicommand.h>
88

99
#include <QShortcut>
10-
#include <QRegExp>
10+
#include <QRegularExpression>
1111

1212
SqlUiLexer* SqlTextEdit::sqlLexer = nullptr;
1313

@@ -104,16 +104,19 @@ void SqlTextEdit::toggleBlockComment()
104104
if (!hasSelectedText()) {
105105
getCursorPosition(&lineFrom, &indexFrom);
106106

107+
indexTo = text(lineFrom).length();
108+
107109
// Windows lines requires an adjustment, otherwise the selection would
108110
// end in the next line.
109-
indexTo = text(lineFrom).endsWith("\r\n") ? lineLength(lineFrom)-1 : lineLength(lineFrom);
111+
if (text(lineFrom).endsWith("\r\n"))
112+
indexTo--;
110113

111114
setSelection(lineFrom, 0, lineFrom, indexTo);
112115
}
113116

114117
getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo);
115118

116-
bool uncomment = text(lineFrom).contains(QRegExp("^[ \t]*--"));
119+
bool uncomment = text(lineFrom).contains(QRegularExpression("^[ \t]*--"));
117120

118121
// If the selection ends before the first character of a line, don't
119122
// take this line into account for un/commenting.
@@ -129,11 +132,13 @@ void SqlTextEdit::toggleBlockComment()
129132
QString lineText = text(line);
130133

131134
if (uncomment)
132-
lineText.replace(QRegExp("^([ \t]*)-- ?"), "\\1");
135+
lineText.replace(QRegularExpression("^([ \t]*)-- ?"), "\\1");
133136
else
134-
lineText.replace(QRegExp("^"), "-- ");
137+
lineText.replace(QRegularExpression("^"), "-- ");
135138

136-
indexTo = lineText.endsWith("\r\n") ? lineLength(line)-1 : lineLength(line);
139+
indexTo = text(line).length();
140+
if (lineText.endsWith("\r\n"))
141+
indexTo--;
137142

138143
setSelection(line, 0, line, indexTo);
139144
replaceSelectedText(lineText);

0 commit comments

Comments
 (0)