From 1173d5184e8b073790556dbdba92a8b8abdd62dc Mon Sep 17 00:00:00 2001 From: Waqar Ahmed 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\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\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\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