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,23 +355,11 @@ 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;
|
||||
}
|
||||
if (replacementsToKeep == ReplacementsToKeep::OnlyIndent) {
|
||||
for (int index = startBlock.blockNumber(); index <= endBlock.blockNumber(); ++index) {
|
||||
utf8Length += forceIndentWithExtraText(buffer,
|
||||
cursorPositionInEditor < 0
|
||||
? endBlock
|
||||
: m_doc->findBlock(cursorPositionInEditor),
|
||||
m_doc->findBlockByNumber(index),
|
||||
secondTry);
|
||||
} else {
|
||||
for (int index = startBlock.blockNumber(); index <= endBlock.blockNumber(); ++index) {
|
||||
utf8Length += forceIndentWithExtraText(buffer,
|
||||
m_doc->findBlockByNumber(index),
|
||||
secondTry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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,",
|
||||
|
Reference in New Issue
Block a user