ClangFormat: Fix removal of common prefix and suffix

Amends 61fc1fd452.

Fixes: QTCREATORBUG-31004
Change-Id: I9fea637d36a97b472204a7601efcec9e0f610eb7
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-11-01 18:02:15 +01:00
parent 0c6a49a19c
commit 4dc40b414f
3 changed files with 25 additions and 2 deletions

View File

@@ -433,8 +433,10 @@ static ChangeSet convertReplacements(const QTextDocument *doc,
QString replacementText = QString::fromStdString(replacement.getReplacementText().str()); QString replacementText = QString::fromStdString(replacement.getReplacementText().str());
replacementText.replace("\r", ""); replacementText.replace("\r", "");
auto sameCharAt = [&](int replacementOffset) { auto sameCharAt = [&](int replacementOffset) {
if (replacementText.size() <= replacementOffset || replacementOffset < 0) if (utf16Length == 0 || replacementText.size() <= replacementOffset
|| replacementOffset < 0) {
return false; return false;
}
const QChar docChar = doc->characterAt(utf16Offset + replacementOffset); const QChar docChar = doc->characterAt(utf16Offset + replacementOffset);
const QChar replacementChar = replacementText.at(replacementOffset); const QChar replacementChar = replacementText.at(replacementOffset);
return docChar == replacementChar return docChar == replacementChar
@@ -444,7 +446,7 @@ static ChangeSet convertReplacements(const QTextDocument *doc,
while (sameCharAt(0)) { while (sameCharAt(0)) {
++utf16Offset; ++utf16Offset;
--utf16Length; --utf16Length;
replacementText = replacementText.mid(1); replacementText.removeFirst();
} }
// remove identical suffix from replacement text // remove identical suffix from replacement text
while (sameCharAt(utf16Length - 1)) { while (sameCharAt(utf16Length - 1)) {

View File

@@ -118,6 +118,7 @@ private slots:
void testIndentationInTheBegginingOfLine(); void testIndentationInTheBegginingOfLine();
void testIndentationReturnAfterIf(); void testIndentationReturnAfterIf();
void testIndentationReturnAfterIfSomthingFunction(); void testIndentationReturnAfterIfSomthingFunction();
void testReformatQualifier();
private: private:
void insertLines(const std::vector<QString> &lines); void insertLines(const std::vector<QString> &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<QString> expected{
"struct S",
"{",
" S &operator=(S const &s);",
"};",
"S &S::operator=(S const &s) {}"};
QCOMPARE(documentLines(), expected);
}
QObject *createClangFormatTest() QObject *createClangFormatTest()
{ {
return new ClangFormatTest; return new ClangFormatTest;

View File

@@ -96,6 +96,7 @@ PenaltyBreakString: 600
PenaltyExcessCharacter: 50 PenaltyExcessCharacter: 50
PenaltyReturnTypeOnItsOwnLine: 300 PenaltyReturnTypeOnItsOwnLine: 300
PointerAlignment: Right PointerAlignment: Right
QualifierAlignment: Right
ReflowComments: false ReflowComments: false
SortIncludes: true SortIncludes: true
SortUsingDeclarations: true SortUsingDeclarations: true