From 4dc40b414fd1ebc587208e1a60bb2808680ff180 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Fri, 1 Nov 2024 18:02:15 +0100 Subject: [PATCH] ClangFormat: Fix removal of common prefix and suffix Amends 61fc1fd45262a8f78c49bc2fb2a7777bdfa02845. Fixes: QTCREATORBUG-31004 Change-Id: I9fea637d36a97b472204a7601efcec9e0f610eb7 Reviewed-by: David Schulz --- .../clangformat/clangformatbaseindenter.cpp | 6 ++++-- .../clangformat/tests/clangformat-test.cpp | 20 +++++++++++++++++++ .../clangformat/tests/data/.clang-format | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index c1f137f10c4..50ea78f8b03 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -433,8 +433,10 @@ static ChangeSet convertReplacements(const QTextDocument *doc, QString replacementText = QString::fromStdString(replacement.getReplacementText().str()); replacementText.replace("\r", ""); auto sameCharAt = [&](int replacementOffset) { - if (replacementText.size() <= replacementOffset || replacementOffset < 0) + if (utf16Length == 0 || replacementText.size() <= replacementOffset + || replacementOffset < 0) { return false; + } const QChar docChar = doc->characterAt(utf16Offset + replacementOffset); const QChar replacementChar = replacementText.at(replacementOffset); return docChar == replacementChar @@ -444,7 +446,7 @@ static ChangeSet convertReplacements(const QTextDocument *doc, while (sameCharAt(0)) { ++utf16Offset; --utf16Length; - replacementText = replacementText.mid(1); + replacementText.removeFirst(); } // remove identical suffix from replacement text while (sameCharAt(utf16Length - 1)) { diff --git a/src/plugins/clangformat/tests/clangformat-test.cpp b/src/plugins/clangformat/tests/clangformat-test.cpp index 6e7cdd367be..48df33fa9e4 100644 --- a/src/plugins/clangformat/tests/clangformat-test.cpp +++ b/src/plugins/clangformat/tests/clangformat-test.cpp @@ -118,6 +118,7 @@ private slots: void testIndentationInTheBegginingOfLine(); void testIndentationReturnAfterIf(); void testIndentationReturnAfterIfSomthingFunction(); + void testReformatQualifier(); private: void insertLines(const std::vector &lines); @@ -999,6 +1000,25 @@ void ClangFormatTest::testIndentationReturnAfterIfSomthingFunction() "}"})); } +void ClangFormatTest::testReformatQualifier() +{ + insertLines({ + "struct S", + "{", + " S &operator=(S const &s);", + "};", + "S &S::operator=(const S &s) {}" + }); + m_extendedIndenter->autoIndent(*m_cursor, TextEditor::TabSettings()); + const std::vector expected{ + "struct S", + "{", + " S &operator=(S const &s);", + "};", + "S &S::operator=(S const &s) {}"}; + QCOMPARE(documentLines(), expected); +} + QObject *createClangFormatTest() { return new ClangFormatTest; diff --git a/src/plugins/clangformat/tests/data/.clang-format b/src/plugins/clangformat/tests/data/.clang-format index 953e0b44cd6..cde673f6c11 100644 --- a/src/plugins/clangformat/tests/data/.clang-format +++ b/src/plugins/clangformat/tests/data/.clang-format @@ -96,6 +96,7 @@ PenaltyBreakString: 600 PenaltyExcessCharacter: 50 PenaltyReturnTypeOnItsOwnLine: 300 PointerAlignment: Right +QualifierAlignment: Right ReflowComments: false SortIncludes: true SortUsingDeclarations: true