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