1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
From 1173d5184e8b073790556dbdba92a8b8abdd62dc Mon Sep 17 00:00:00 2001
From: Waqar Ahmed <waqar.17a@gmail.com>
Date: Mon, 14 Mar 2022 09:44:54 +0500
Subject: [PATCH] Fix toggle comment with space at the start
BUG: 451471
---
autotests/src/katedocument_test.cpp | 48 +++++++++++++++++++++++++++++
autotests/src/katedocument_test.h | 1 +
src/document/katedocument.cpp | 10 +++++-
3 files changed, 58 insertions(+), 1 deletion(-)
diff --git a/autotests/src/katedocument_test.cpp b/autotests/src/katedocument_test.cpp
index f0946829..a842f960 100644
--- a/autotests/src/katedocument_test.cpp
+++ b/autotests/src/katedocument_test.cpp
@@ -851,4 +851,52 @@ void KateDocumentTest::testKeepUndoOverReload()
QCOMPARE(doc.text(), insertedText + initialText);
}
+void KateDocumentTest::testToggleComment()
+{
+ { // BUG: 451471
+ KTextEditor::DocumentPrivate doc;
+ QVERIFY(doc.highlightingModes().contains(QStringLiteral("Python")));
+ doc.setHighlightingMode(QStringLiteral("Python"));
+ const QString original = QStringLiteral("import hello;\n def method():");
+ doc.setText(original);
+ QVERIFY(doc.lines() == 2);
+
+ doc.commentSelection(doc.documentRange(), {1, 2}, false, 0); // 0 == ToggleComment
+ QCOMPARE(doc.text(), QStringLiteral("#import hello;\n #def method():"));
+
+ doc.commentSelection(doc.documentRange(), {1, 2}, false, 0); // 0 == ToggleComment
+ QCOMPARE(doc.text(), original);
+ }
+
+ { // Comment C++;
+ KTextEditor::DocumentPrivate doc;
+ QVERIFY(doc.highlightingModes().contains(QStringLiteral("C++")));
+ doc.setHighlightingMode(QStringLiteral("C++"));
+ QString original = QStringLiteral("#include<iostream>\nint main()\n{\nreturn 0;\n}\n");
+ doc.setText(original);
+ QVERIFY(doc.lines() == 6);
+
+ doc.commentSelection(doc.documentRange(), {5, 0}, false, 0); // 0 == ToggleComment
+ QCOMPARE(doc.text(), QStringLiteral("// #include<iostream>\n// int main()\n// {\n// return 0;\n// }\n"));
+
+ doc.commentSelection(doc.documentRange(), {5, 0}, false, 0); // 0 == ToggleComment
+ QCOMPARE(doc.text(), original);
+
+ // Comment just a portion
+ doc.commentSelection(Range(1, 0, 1, 3), Cursor(1, 3), false, 0);
+ QCOMPARE(doc.text(), QStringLiteral("#include<iostream>\n/*int*/ main()\n{\nreturn 0;\n}\n"));
+ doc.commentSelection(Range(1, 0, 1, 7), Cursor(1, 3), false, 0);
+ QCOMPARE(doc.text(), original);
+
+ // mixed, one line commented, one not => both get commented
+ original = QStringLiteral(" // int main()\n{}");
+ doc.setText(original);
+ doc.commentSelection(doc.documentRange(), {1, 2}, false, 0);
+ QCOMPARE(doc.text(), QStringLiteral("// // int main()\n// {}"));
+ doc.commentSelection(doc.documentRange(), {1, 2}, false, 0);
+ // after uncommenting, we get original text back with one line commented
+ QCOMPARE(doc.text(), original);
+ }
+}
+
#include "katedocument_test.moc"
diff --git a/autotests/src/katedocument_test.h b/autotests/src/katedocument_test.h
index c3b24b4c..0076f879 100644
--- a/autotests/src/katedocument_test.h
+++ b/autotests/src/katedocument_test.h
@@ -46,6 +46,7 @@ private Q_SLOTS:
void testIndentOnPaste();
void testAboutToSave();
void testKeepUndoOverReload();
+ void testToggleComment();
};
#endif // KATE_DOCUMENT_TEST_H
diff --git a/src/document/katedocument.cpp b/src/document/katedocument.cpp
index 56e8fc7d..b72592b6 100644
--- a/src/document/katedocument.cpp
+++ b/src/document/katedocument.cpp
@@ -3995,7 +3995,11 @@ bool KTextEditor::DocumentPrivate::removeStartLineCommentFromSelection(KTextEdit
bool allLinesAreCommented = true;
for (int line = endLine; line >= startLine; line--) {
const auto ln = m_buffer->plainLine(line);
- if (!ln->startsWith(shortCommentMark) && !ln->startsWith(longCommentMark)) {
+ const QString &text = ln->text();
+ QStringView textView(text.data(), text.size());
+ // Must trim any spaces at the beginning
+ textView = textView.trimmed();
+ if (!textView.startsWith(shortCommentMark) && !textView.startsWith(longCommentMark)) {
allLinesAreCommented = false;
break;
}
@@ -4031,6 +4035,10 @@ void KTextEditor::DocumentPrivate::commentSelection(KTextEditor::Range selection
int startAttrib = 0;
Kate::TextLine ln = kateTextLine(line);
+ if (!ln) {
+ qWarning() << __FUNCTION__ << __LINE__ << "Unexpected null TextLine for " << line << " lineCount: " << lines();
+ return;
+ }
if (selectionCol < ln->length()) {
startAttrib = ln->attribute(selectionCol);
--
GitLab
|