ClangFormat: Do not remove empty lines while only indenting

Insert dummy text into empty lines also for the electic characters
not to remove empty lines when only indentation is intended.

Fixes: QTCREATORBUG-22050
Change-Id: Ife5374459feb510a0587880a6772c90a2d68d70e
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Ivan Donchevskii
2019-03-05 17:16:29 +01:00
parent dc9c9249fc
commit ad8cabdf45
2 changed files with 20 additions and 15 deletions

View File

@@ -355,25 +355,13 @@ TextEditor::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffer
rangeStart = formattingRangeStart(startBlock, buffer, lastSaveRevision());
adjustFormatStyleForLineBreak(style, replacementsToKeep);
if (typedChar == QChar::Null) {
if (replacementsToKeep == ReplacementsToKeep::IndentAndBefore) {
if (utf8Offset > 0) {
buffer.insert(utf8Offset - 1, " //");
utf8Offset += 3;
}
utf8Length += forceIndentWithExtraText(buffer,
cursorPositionInEditor < 0
? endBlock
: m_doc->findBlock(cursorPositionInEditor),
secondTry);
} else {
if (replacementsToKeep == ReplacementsToKeep::OnlyIndent) {
for (int index = startBlock.blockNumber(); index <= endBlock.blockNumber(); ++index) {
utf8Length += forceIndentWithExtraText(buffer,
m_doc->findBlockByNumber(index),
secondTry);
}
}
}
if (replacementsToKeep != ReplacementsToKeep::IndentAndBefore || utf8Offset < rangeStart)
rangeStart = utf8Offset;

View File

@@ -362,6 +362,23 @@ TEST_F(ClangFormat, IndentEmptyLineAndKeepPreviousEmptyLines)
"}"));
}
TEST_F(ClangFormat, IndentOnElectricCharacterButNotRemoveEmptyLinesBefore)
{
insertLines({"{",
" ",
" ",
"if ()",
"}"});
indenter.indentBlock(doc.findBlockByNumber(3), '(', TextEditor::TabSettings());
ASSERT_THAT(documentLines(), ElementsAre("{",
" ",
" ",
" if ()",
"}"));
}
TEST_F(ClangFormat, IndentFunctionBodyButNotFormatBeforeIt)
{
insertLines({"int foo(int a, int b,",