From 2484a5e209a95bf5d369372ed85a80ca7bc90f03 Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Mon, 4 Mar 2019 13:40:20 +0100 Subject: [PATCH] ClangFormat: Do not apply "smart" formatting before new lines In the concept that behavior seemed fine but in practive it looks quite strange. Add comma to the dummy text inserted into the empty line to have a proper indentation for the following empty lines. Change-Id: I770af02a475e6489bdc8f44d9f84eb3c5e7398d7 Reviewed-by: Marco Bubke --- .../clangformat/clangformatbaseindenter.cpp | 4 +- tests/unit/unittest/clangformat-test.cpp | 41 ++++++++----------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index a53eb50adb2..ee168b301f3 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -157,7 +157,7 @@ int forceIndentWithExtraText(QByteArray &buffer, const QTextBlock &block, bool s prevBlockIsEmpty = prevBlock.position() > 0 && prevBlock.text().trimmed().isEmpty(); } if (closingParenBlock || prevBlock.text().endsWith(',')) - dummyText = "&& a"; + dummyText = "&& a,"; buffer.insert(utf8Offset, dummyText); extraLength += dummyText.length(); @@ -464,7 +464,7 @@ TextEditor::Replacements ClangFormatBaseIndenter::indentsFor(QTextBlock startBlo ReplacementsToKeep replacementsToKeep = ReplacementsToKeep::OnlyIndent; if (formatWhileTyping() && (cursorPositionInEditor == -1 || cursorPositionInEditor >= startBlockPosition) - && (typedChar == QChar::Null || typedChar == ';' || typedChar == '}')) { + && (typedChar == ';' || typedChar == '}')) { // Format before current position only in case the cursor is inside the indented block. // So if cursor position is less then the block position then the current line is before // the indented block - don't trigger extra formatting in this case. diff --git a/tests/unit/unittest/clangformat-test.cpp b/tests/unit/unittest/clangformat-test.cpp index 115ecaf763e..67c31221e51 100644 --- a/tests/unit/unittest/clangformat-test.cpp +++ b/tests/unit/unittest/clangformat-test.cpp @@ -32,9 +32,8 @@ namespace TextEditor { class TabSettings -{ -}; -} +{}; +} // namespace TextEditor namespace { @@ -58,10 +57,7 @@ public: : ClangFormatIndenter(doc) {} - bool formatWhileTyping() const override - { - return true; - } + bool formatWhileTyping() const override { return true; } }; class ClangFormat : public ::testing::Test @@ -104,6 +100,7 @@ protected: QTextCursor cursor{&doc}; }; +// clang-format off TEST_F(ClangFormat, IndentBasicFile) { insertLines({"int main()", @@ -365,7 +362,7 @@ TEST_F(ClangFormat, IndentEmptyLineAndKeepPreviousEmptyLines) "}")); } -TEST_F(ClangFormat, IndentFunctionBodyAndFormatBeforeIt) +TEST_F(ClangFormat, IndentFunctionBodyButNotFormatBeforeIt) { insertLines({"int foo(int a, int b,", " int c, int d", @@ -375,8 +372,9 @@ TEST_F(ClangFormat, IndentFunctionBodyAndFormatBeforeIt) extendedIndenter.indentBlock(doc.findBlockByNumber(3), QChar::Null, TextEditor::TabSettings()); - ASSERT_THAT(documentLines(), ElementsAre("int foo(int a, int b, int c, int d)", - "{", + ASSERT_THAT(documentLines(), ElementsAre("int foo(int a, int b,", + " int c, int d", + " ) {", " ", "}")); } @@ -404,13 +402,11 @@ TEST_F(ClangFormat, ReformatToEmptyFunction) insertLines({"int foo(int a, int b, int c, int d)", "{", " ", - "}", - ""}); + "}"}); - extendedIndenter.indentBlock(doc.findBlockByNumber(4), QChar::Null, TextEditor::TabSettings()); + extendedIndenter.indentBlock(doc.findBlockByNumber(3), '}', TextEditor::TabSettings()); - ASSERT_THAT(documentLines(), ElementsAre("int foo(int a, int b, int c, int d) {}", - "")); + ASSERT_THAT(documentLines(), ElementsAre("int foo(int a, int b, int c, int d) {}")); } TEST_F(ClangFormat, ReformatToNonEmptyFunction) @@ -421,13 +417,12 @@ TEST_F(ClangFormat, ReformatToNonEmptyFunction) extendedIndenter.indentBlock(doc.findBlockByNumber(1), QChar::Null, TextEditor::TabSettings()); - ASSERT_THAT(documentLines(), ElementsAre("int foo(int a, int b)", - "{", + ASSERT_THAT(documentLines(), ElementsAre("int foo(int a, int b) {", " ", "}")); } -TEST_F(ClangFormat, IndentIfBodyAndFormatBeforeIt) +TEST_F(ClangFormat, IndentClosingScopeAndFormatBeforeIt) { insertLines({"if(a && b", " &&c && d", @@ -435,10 +430,9 @@ TEST_F(ClangFormat, IndentIfBodyAndFormatBeforeIt) "", "}"}); - extendedIndenter.indentBlock(doc.findBlockByNumber(3), QChar::Null, TextEditor::TabSettings()); + extendedIndenter.indentBlock(doc.findBlockByNumber(4), '}', TextEditor::TabSettings()); ASSERT_THAT(documentLines(), ElementsAre("if (a && b && c && d) {", - " ", "}")); } @@ -496,7 +490,7 @@ TEST_F(ClangFormat, IndentAndFormatCompleteStatementOnClosingScope) "}")); } -TEST_F(ClangFormat, IndentAndFormatWithEmptyLines) +TEST_F(ClangFormat, OnlyIndentClosingParenthesis) { insertLines({"foo(a,", " ", @@ -505,7 +499,7 @@ TEST_F(ClangFormat, IndentAndFormatWithEmptyLines) extendedIndenter.indentBlock(doc.findBlockByNumber(2), QChar::Null, TextEditor::TabSettings()); ASSERT_THAT(documentLines(), ElementsAre("foo(a,", - "", + " ", " )")); } @@ -622,5 +616,6 @@ TEST_F(ClangFormat, FormatTemplateparameters) ASSERT_THAT(documentLines(), ElementsAre("using Alias = Template")); } +// clang-format on -} +} // namespace