diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index 6378b2af530..4a595dd4116 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -250,6 +250,9 @@ int forceIndentWithExtraText(QByteArray &buffer, const QTextBlock &block, bool secondTry) { + if (!block.isValid()) + return 0; + const QString blockText = block.text(); int firstNonWhitespace = Utils::indexOf(blockText, [](const QChar &ch) { return !ch.isSpace(); }); @@ -287,7 +290,16 @@ int forceIndentWithExtraText(QByteArray &buffer, dummyText = dummyTextForContext(charContext, closingBraceBlock); } - buffer.insert(utf8Offset, dummyText); + // A comment at the end of the line appears to prevent clang-format from removing line breaks. + if (dummyText == "/**/" || dummyText.isEmpty()) { + if (block.previous().isValid()) { + const int prevEndOffset = Utils::Text::utf8NthLineOffset(block.document(), buffer, + block.blockNumber()) + block.previous().text().length(); + buffer.insert(prevEndOffset, "//"); + extraLength += 2; + } + } + buffer.insert(utf8Offset + extraLength, dummyText); extraLength += dummyText.length(); if (secondTry) { diff --git a/tests/unit/unittest/clangformat-test.cpp b/tests/unit/unittest/clangformat-test.cpp index 822f4b76d25..2ce3eba24ab 100644 --- a/tests/unit/unittest/clangformat-test.cpp +++ b/tests/unit/unittest/clangformat-test.cpp @@ -198,14 +198,7 @@ TEST_F(ClangFormat, IndentLambdaWithReturnType) "}")); } -#ifndef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED -# define DISABLED_FOR_VANILLA_CLANG(x) DISABLED_##x -#else -# define DISABLED_FOR_VANILLA_CLANG(x) x -#endif - -// This test requires the custom clang patch https://code.qt.io/cgit/clang/llvm-project.git/commit/?h=release_100-based&id=9b992a0f7f160dd6c75f20a4dcfcf7c60a4894df -TEST_F(ClangFormat, DISABLED_FOR_VANILLA_CLANG(IndentFunctionArgumentLambdaWithNextLineScope)) +TEST_F(ClangFormat, ClangFormatIndentFunctionArgumentLambdaWithNextLineScope) { insertLines({"foo([]()", "{", @@ -954,6 +947,16 @@ TEST_F(ClangFormat, SortIncludes) "#include ", "#include ")); } + +TEST_F(ClangFormat, ChainedMemberFunctionCalls) +{ + insertLines({"S().func().func()", + ".func();"}); + indenter.indent(cursor, QChar::Null, TextEditor::TabSettings()); + ASSERT_THAT(documentLines(), ElementsAre("S().func().func()", + " .func();")); +} + // clang-format on } // namespace