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 <marco.bubke@qt.io>
This commit is contained in:
Ivan Donchevskii
2019-02-20 08:43:29 +01:00
parent 0a3aa12962
commit 8a93074d0e
3 changed files with 30 additions and 8 deletions

View File

@@ -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);
}

View File

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

View File

@@ -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()",