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;