forked from qt-creator/qt-creator
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:
@@ -355,25 +355,13 @@ TextEditor::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffer
|
|||||||
rangeStart = formattingRangeStart(startBlock, buffer, lastSaveRevision());
|
rangeStart = formattingRangeStart(startBlock, buffer, lastSaveRevision());
|
||||||
|
|
||||||
adjustFormatStyleForLineBreak(style, replacementsToKeep);
|
adjustFormatStyleForLineBreak(style, replacementsToKeep);
|
||||||
if (typedChar == QChar::Null) {
|
if (replacementsToKeep == ReplacementsToKeep::OnlyIndent) {
|
||||||
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 {
|
|
||||||
for (int index = startBlock.blockNumber(); index <= endBlock.blockNumber(); ++index) {
|
for (int index = startBlock.blockNumber(); index <= endBlock.blockNumber(); ++index) {
|
||||||
utf8Length += forceIndentWithExtraText(buffer,
|
utf8Length += forceIndentWithExtraText(buffer,
|
||||||
m_doc->findBlockByNumber(index),
|
m_doc->findBlockByNumber(index),
|
||||||
secondTry);
|
secondTry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (replacementsToKeep != ReplacementsToKeep::IndentAndBefore || utf8Offset < rangeStart)
|
if (replacementsToKeep != ReplacementsToKeep::IndentAndBefore || utf8Offset < rangeStart)
|
||||||
rangeStart = utf8Offset;
|
rangeStart = utf8Offset;
|
||||||
|
@@ -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)
|
TEST_F(ClangFormat, IndentFunctionBodyButNotFormatBeforeIt)
|
||||||
{
|
{
|
||||||
insertLines({"int foo(int a, int b,",
|
insertLines({"int foo(int a, int b,",
|
||||||
|
Reference in New Issue
Block a user