From c14845181dfe553ac7b23c64412c0b04d4815029 Mon Sep 17 00:00:00 2001 From: Artem Sokolovskii Date: Mon, 27 May 2024 10:55:59 +0200 Subject: [PATCH] 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 --- .../clangformat/clangformatbaseindenter.cpp | 5 +- .../clangformat/tests/clangformat-test.cpp | 56 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index 1a39371538c..4d89ea8aa2e 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -782,12 +782,15 @@ void ClangFormatBaseIndenterPrivate::indent(const QTextCursor &cursor, const QChar &typedChar, int cursorPositionInEditor) { + const QString blockText = cursor.block().text().trimmed(); if (cursor.hasSelection()) { indentBlocks(m_doc->findBlock(cursor.selectionStart()), m_doc->findBlock(cursor.selectionEnd()), typedChar, cursorPositionInEditor); - } else { + } else if ( + typedChar == QChar::Null || blockText.startsWith(typedChar) || blockText.endsWith(typedChar) + || blockText.isEmpty()) { indentBlocks(cursor.block(), cursor.block(), typedChar, cursorPositionInEditor); } } diff --git a/src/plugins/clangformat/tests/clangformat-test.cpp b/src/plugins/clangformat/tests/clangformat-test.cpp index 452c1fcfc2e..c52767baa27 100644 --- a/src/plugins/clangformat/tests/clangformat-test.cpp +++ b/src/plugins/clangformat/tests/clangformat-test.cpp @@ -113,6 +113,9 @@ private slots: void testUtf8SymbolLine(); void testFunctionCallClosingParenthesis(); void testFunctionCallClosingParenthesisEmptyLine(); + void testNoIndentationInMiddleOfLine(); + void testIndentationInTheBegginingOfLine(); + void testIndentationInMiddleOfLine(); private: void insertLines(const std::vector &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{"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{"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{"int main()", + "{", + " if () {", + " } else", + "}"})); +} + QObject *createClangFormatTest() { return new ClangFormatTest;