ClangFormat: Fix indentation in the middle of line

Before, indentation applies in the middle of the line if an electric
 character is typed.
Now it happens only if the character is typed at the line's end or start
 and if new line character was typed. It should reduce jumping while typing.

Fixes: QTCREATORBUG-30731
Change-Id: I018cb4a03af5e6450be2cd423cb29bd384048871
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2024-05-27 10:55:59 +02:00
parent 968051eb72
commit c14845181d
2 changed files with 60 additions and 1 deletions

View File

@@ -782,12 +782,15 @@ void ClangFormatBaseIndenterPrivate::indent(const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
int cursorPositionInEditor) int cursorPositionInEditor)
{ {
const QString blockText = cursor.block().text().trimmed();
if (cursor.hasSelection()) { if (cursor.hasSelection()) {
indentBlocks(m_doc->findBlock(cursor.selectionStart()), indentBlocks(m_doc->findBlock(cursor.selectionStart()),
m_doc->findBlock(cursor.selectionEnd()), m_doc->findBlock(cursor.selectionEnd()),
typedChar, typedChar,
cursorPositionInEditor); cursorPositionInEditor);
} else { } else if (
typedChar == QChar::Null || blockText.startsWith(typedChar) || blockText.endsWith(typedChar)
|| blockText.isEmpty()) {
indentBlocks(cursor.block(), cursor.block(), typedChar, cursorPositionInEditor); indentBlocks(cursor.block(), cursor.block(), typedChar, cursorPositionInEditor);
} }
} }

View File

@@ -113,6 +113,9 @@ private slots:
void testUtf8SymbolLine(); void testUtf8SymbolLine();
void testFunctionCallClosingParenthesis(); void testFunctionCallClosingParenthesis();
void testFunctionCallClosingParenthesisEmptyLine(); void testFunctionCallClosingParenthesisEmptyLine();
void testNoIndentationInMiddleOfLine();
void testIndentationInTheBegginingOfLine();
void testIndentationInMiddleOfLine();
private: private:
void insertLines(const std::vector<QString> &lines); void insertLines(const std::vector<QString> &lines);
@@ -909,6 +912,59 @@ void ClangFormatTest::testFunctionCallClosingParenthesisEmptyLine()
})); }));
} }
void ClangFormatTest::testNoIndentationInMiddleOfLine()
{
insertLines({"int main()",
"{",
" S s = {.i = 1}, .l = 2, .f = 3, .d = 4};",
" return 0;",
"}"});
m_cursor->setPosition(30);
m_extendedIndenter->indent(*m_cursor, '}', TextEditor::TabSettings(), 30);
QCOMPARE(documentLines(),
(std::vector<QString>{"int main()",
"{",
" S s = {.i = 1}, .l = 2, .f = 3, .d = 4};",
" return 0;",
"}"}));
}
void ClangFormatTest::testIndentationInMiddleOfLine()
{
insertLines({"int main()",
"{",
" S s = {.i = 1,",
".l = 2, .f = 3, .d = 4};",
" return 0;",
"}"});
m_cursor->setPosition(32);
m_extendedIndenter->indent(*m_cursor, QChar::Null, TextEditor::TabSettings(), 32);
QCOMPARE(documentLines(),
(std::vector<QString>{"int main()",
"{",
" S s = {.i = 1,",
" .l = 2, .f = 3, .d = 4};",
" return 0;",
"}"}));
}
void ClangFormatTest::testIndentationInTheBegginingOfLine()
{
insertLines({"int main()",
"{",
" if () {",
" } else",
"}"});
m_cursor->setPosition(35);
m_extendedIndenter->indent(*m_cursor, '}', TextEditor::TabSettings(), 35);
QCOMPARE(documentLines(),
(std::vector<QString>{"int main()",
"{",
" if () {",
" } else",
"}"}));
}
QObject *createClangFormatTest() QObject *createClangFormatTest()
{ {
return new ClangFormatTest; return new ClangFormatTest;