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 <marco.bubke@qt.io>
This commit is contained in:
Ivan Donchevskii
2019-03-04 13:40:20 +01:00
parent f670e80c88
commit 2484a5e209
2 changed files with 20 additions and 25 deletions

View File

@@ -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.

View File

@@ -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,",
" ",
@@ -622,5 +616,6 @@ TEST_F(ClangFormat, FormatTemplateparameters)
ASSERT_THAT(documentLines(), ElementsAre("using Alias = Template<A, B, C>"));
}
// clang-format on
}
} // namespace