forked from qt-creator/qt-creator
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:
@@ -136,7 +136,7 @@ int forceIndentWithExtraText(QByteArray &buffer, const QTextBlock &block, bool s
|
|||||||
int utf8Offset = Utils::Text::utf8NthLineOffset(block.document(),
|
int utf8Offset = Utils::Text::utf8NthLineOffset(block.document(),
|
||||||
buffer,
|
buffer,
|
||||||
block.blockNumber() + 1);
|
block.blockNumber() + 1);
|
||||||
if (firstNonWhitespace > 0)
|
if (firstNonWhitespace >= 0)
|
||||||
utf8Offset += firstNonWhitespace;
|
utf8Offset += firstNonWhitespace;
|
||||||
else
|
else
|
||||||
utf8Offset += blockText.length();
|
utf8Offset += blockText.length();
|
||||||
@@ -336,6 +336,7 @@ ClangFormatBaseIndenter::ClangFormatBaseIndenter(QTextDocument *doc)
|
|||||||
TextEditor::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffer,
|
TextEditor::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffer,
|
||||||
const QTextBlock &startBlock,
|
const QTextBlock &startBlock,
|
||||||
const QTextBlock &endBlock,
|
const QTextBlock &endBlock,
|
||||||
|
int cursorPositionInEditor,
|
||||||
ReplacementsToKeep replacementsToKeep,
|
ReplacementsToKeep replacementsToKeep,
|
||||||
const QChar &typedChar,
|
const QChar &typedChar,
|
||||||
bool secondTry) const
|
bool secondTry) const
|
||||||
@@ -353,19 +354,24 @@ TextEditor::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffer
|
|||||||
if (replacementsToKeep == ReplacementsToKeep::IndentAndBefore)
|
if (replacementsToKeep == ReplacementsToKeep::IndentAndBefore)
|
||||||
rangeStart = formattingRangeStart(startBlock, buffer, lastSaveRevision());
|
rangeStart = formattingRangeStart(startBlock, buffer, lastSaveRevision());
|
||||||
|
|
||||||
|
adjustFormatStyleForLineBreak(style, replacementsToKeep);
|
||||||
|
if (typedChar == QChar::Null) {
|
||||||
if (replacementsToKeep == ReplacementsToKeep::IndentAndBefore) {
|
if (replacementsToKeep == ReplacementsToKeep::IndentAndBefore) {
|
||||||
buffer.insert(utf8Offset - 1, " //");
|
buffer.insert(utf8Offset - 1, " //");
|
||||||
utf8Offset += 3;
|
utf8Offset += 3;
|
||||||
}
|
utf8Length += forceIndentWithExtraText(buffer,
|
||||||
|
cursorPositionInEditor < 0
|
||||||
adjustFormatStyleForLineBreak(style, replacementsToKeep);
|
? endBlock
|
||||||
if (typedChar == QChar::Null) {
|
: 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;
|
||||||
@@ -394,6 +400,7 @@ TextEditor::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffer
|
|||||||
return replacements(originalBuffer,
|
return replacements(originalBuffer,
|
||||||
startBlock,
|
startBlock,
|
||||||
endBlock,
|
endBlock,
|
||||||
|
cursorPositionInEditor,
|
||||||
replacementsToKeep,
|
replacementsToKeep,
|
||||||
typedChar,
|
typedChar,
|
||||||
true);
|
true);
|
||||||
@@ -468,6 +475,7 @@ TextEditor::Replacements ClangFormatBaseIndenter::indentsFor(QTextBlock startBlo
|
|||||||
return replacements(buffer,
|
return replacements(buffer,
|
||||||
startBlock,
|
startBlock,
|
||||||
endBlock,
|
endBlock,
|
||||||
|
cursorPositionInEditor,
|
||||||
replacementsToKeep,
|
replacementsToKeep,
|
||||||
typedChar);
|
typedChar);
|
||||||
}
|
}
|
||||||
|
@@ -87,6 +87,7 @@ private:
|
|||||||
TextEditor::Replacements replacements(QByteArray buffer,
|
TextEditor::Replacements replacements(QByteArray buffer,
|
||||||
const QTextBlock &startBlock,
|
const QTextBlock &startBlock,
|
||||||
const QTextBlock &endBlock,
|
const QTextBlock &endBlock,
|
||||||
|
int cursorPositionInEditor,
|
||||||
ReplacementsToKeep replacementsToKeep,
|
ReplacementsToKeep replacementsToKeep,
|
||||||
const QChar &typedChar = QChar::Null,
|
const QChar &typedChar = QChar::Null,
|
||||||
bool secondTry = false) const;
|
bool secondTry = false) const;
|
||||||
|
@@ -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)
|
TEST_F(ClangFormat, FormatBasicFile)
|
||||||
{
|
{
|
||||||
insertLines({"int main()",
|
insertLines({"int main()",
|
||||||
|
Reference in New Issue
Block a user