From 8a93074d0ef711b8bba6545a8bc270315661a36e Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Wed, 20 Feb 2019 08:43:29 +0100 Subject: [PATCH] ClangFormat: Fix the indentation for lines starting with ')' Fix the insetred text position for such cases. In the 'smart indentation mode" do not indent empty lines above the current one but apply the formatting for them instead. Change-Id: I4cfe7f9778bac5e311aa339e2fcfc717f436cb20 Reviewed-by: Marco Bubke --- .../clangformat/clangformatbaseindenter.cpp | 24 ++++++++++++------- .../clangformat/clangformatbaseindenter.h | 1 + tests/unit/unittest/clangformat-test.cpp | 13 ++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index 7a87eabcd79..c295a8a650a 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -136,7 +136,7 @@ int forceIndentWithExtraText(QByteArray &buffer, const QTextBlock &block, bool s int utf8Offset = Utils::Text::utf8NthLineOffset(block.document(), buffer, block.blockNumber() + 1); - if (firstNonWhitespace > 0) + if (firstNonWhitespace >= 0) utf8Offset += firstNonWhitespace; else utf8Offset += blockText.length(); @@ -336,6 +336,7 @@ ClangFormatBaseIndenter::ClangFormatBaseIndenter(QTextDocument *doc) TextEditor::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffer, const QTextBlock &startBlock, const QTextBlock &endBlock, + int cursorPositionInEditor, ReplacementsToKeep replacementsToKeep, const QChar &typedChar, bool secondTry) const @@ -353,17 +354,22 @@ TextEditor::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffer if (replacementsToKeep == ReplacementsToKeep::IndentAndBefore) rangeStart = formattingRangeStart(startBlock, buffer, lastSaveRevision()); - if (replacementsToKeep == ReplacementsToKeep::IndentAndBefore) { - buffer.insert(utf8Offset - 1, " //"); - utf8Offset += 3; - } - adjustFormatStyleForLineBreak(style, replacementsToKeep); if (typedChar == QChar::Null) { - for (int index = startBlock.blockNumber(); index <= endBlock.blockNumber(); ++index) { + if (replacementsToKeep == ReplacementsToKeep::IndentAndBefore) { + buffer.insert(utf8Offset - 1, " //"); + utf8Offset += 3; utf8Length += forceIndentWithExtraText(buffer, - m_doc->findBlockByNumber(index), + cursorPositionInEditor < 0 + ? endBlock + : m_doc->findBlock(cursorPositionInEditor), secondTry); + } else { + for (int index = startBlock.blockNumber(); index <= endBlock.blockNumber(); ++index) { + utf8Length += forceIndentWithExtraText(buffer, + m_doc->findBlockByNumber(index), + secondTry); + } } } @@ -394,6 +400,7 @@ TextEditor::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffer return replacements(originalBuffer, startBlock, endBlock, + cursorPositionInEditor, replacementsToKeep, typedChar, true); @@ -468,6 +475,7 @@ TextEditor::Replacements ClangFormatBaseIndenter::indentsFor(QTextBlock startBlo return replacements(buffer, startBlock, endBlock, + cursorPositionInEditor, replacementsToKeep, typedChar); } diff --git a/src/plugins/clangformat/clangformatbaseindenter.h b/src/plugins/clangformat/clangformatbaseindenter.h index ed63ff6efa3..b412b57d6d4 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.h +++ b/src/plugins/clangformat/clangformatbaseindenter.h @@ -87,6 +87,7 @@ private: TextEditor::Replacements replacements(QByteArray buffer, const QTextBlock &startBlock, const QTextBlock &endBlock, + int cursorPositionInEditor, ReplacementsToKeep replacementsToKeep, const QChar &typedChar = QChar::Null, bool secondTry = false) const; diff --git a/tests/unit/unittest/clangformat-test.cpp b/tests/unit/unittest/clangformat-test.cpp index c0ff7dcd2ce..115ecaf763e 100644 --- a/tests/unit/unittest/clangformat-test.cpp +++ b/tests/unit/unittest/clangformat-test.cpp @@ -496,6 +496,19 @@ TEST_F(ClangFormat, IndentAndFormatCompleteStatementOnClosingScope) "}")); } +TEST_F(ClangFormat, IndentAndFormatWithEmptyLines) +{ + insertLines({"foo(a,", + " ", + ")"}); + + extendedIndenter.indentBlock(doc.findBlockByNumber(2), QChar::Null, TextEditor::TabSettings()); + + ASSERT_THAT(documentLines(), ElementsAre("foo(a,", + "", + " )")); +} + TEST_F(ClangFormat, FormatBasicFile) { insertLines({"int main()",