ClangFormat: Fix utf8 symbol indentation

Fixes: QTCREATORBUG-29927
Change-Id: I5a5a326e13cbe3a874a66e275ded0c6dea9964d7
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Artem Sokolovskii
2023-12-08 11:49:01 +01:00
parent 2f49c0830e
commit ee75c7276b
3 changed files with 21 additions and 2 deletions

View File

@@ -316,8 +316,10 @@ int forceIndentWithExtraText(QByteArray &buffer,
// A comment at the end of the line appears to prevent clang-format from removing line breaks. // A comment at the end of the line appears to prevent clang-format from removing line breaks.
if (dummyText == "/*//*/" || dummyText.isEmpty()) { if (dummyText == "/*//*/" || dummyText.isEmpty()) {
if (block.previous().isValid()) { if (block.previous().isValid()) {
const int prevEndOffset = Utils::Text::utf8NthLineOffset(block.document(), buffer, const int prevEndOffset = Utils::Text::utf8NthLineOffset(block.document(),
block.blockNumber()) + block.previous().text().length(); buffer,
block.blockNumber())
+ block.previous().text().toUtf8().length();
buffer.insert(prevEndOffset, " //"); buffer.insert(prevEndOffset, " //");
extraLength += 3; extraLength += 3;
} }

View File

@@ -730,4 +730,20 @@ void ClangFormatTest::testIndentCommentOnNewLine()
})); }));
} }
void ClangFormatTest::testUtf8SymbolLine()
{
insertLines({"int main()",
"{",
" cout << \"ä\" << endl;",
" return 0;",
"}"});
m_indenter->indent(*m_cursor, QChar::Null, TextEditor::TabSettings());
QCOMPARE(documentLines(),
(std::vector<QString>{"int main()",
"{",
" cout << \"ä\" << endl;",
" return 0;",
"}"}));
}
} // namespace ClangFormat::Internal } // namespace ClangFormat::Internal

View File

@@ -91,6 +91,7 @@ private slots:
void testIndentInitializeVector(); void testIndentInitializeVector();
void testIndentFunctionArgumentOnNewLine(); void testIndentFunctionArgumentOnNewLine();
void testIndentCommentOnNewLine(); void testIndentCommentOnNewLine();
void testUtf8SymbolLine();
private: private:
void insertLines(const std::vector<QString> &lines); void insertLines(const std::vector<QString> &lines);