forked from qt-creator/qt-creator
ClangFormat: Trim space-only lines to properly indent them
Do not add indentation to the existing spaces in the line but trim the line first and then indent it as an empty line. Change-Id: I0e95ab5e4550410e55abfb12362035751c47b0b3 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io> Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
@@ -133,6 +133,18 @@ void trimFirstNonEmptyBlock(const QTextBlock ¤tBlock)
|
||||
cursor.endEditBlock();
|
||||
}
|
||||
|
||||
void trimCurrentBlock(const QTextBlock ¤tBlock)
|
||||
{
|
||||
if (currentBlock.text().trimmed().isEmpty()) {
|
||||
// Clear the block containing only spaces
|
||||
QTextCursor cursor(currentBlock);
|
||||
cursor.beginEditBlock();
|
||||
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
||||
cursor.removeSelectedText();
|
||||
cursor.endEditBlock();
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the total langth of previous lines with pure whitespace.
|
||||
int previousEmptyLinesLength(const QTextBlock ¤tBlock)
|
||||
{
|
||||
@@ -418,6 +430,7 @@ void ClangFormatIndenter::indentBlock(QTextDocument *doc,
|
||||
|
||||
const Utils::FileName fileName = editor->textDocument()->filePath();
|
||||
trimFirstNonEmptyBlock(block);
|
||||
trimCurrentBlock(block);
|
||||
const QByteArray buffer = doc->toPlainText().toUtf8();
|
||||
const int utf8Offset = Utils::Text::utf8NthLineOffset(doc, buffer, block.blockNumber() + 1);
|
||||
QTC_ASSERT(utf8Offset >= 0, return;);
|
||||
@@ -436,6 +449,7 @@ int ClangFormatIndenter::indentFor(const QTextBlock &block, const TextEditor::Ta
|
||||
|
||||
const Utils::FileName fileName = editor->textDocument()->filePath();
|
||||
trimFirstNonEmptyBlock(block);
|
||||
trimCurrentBlock(block);
|
||||
const QTextDocument *doc = block.document();
|
||||
const QByteArray buffer = doc->toPlainText().toUtf8();
|
||||
const int utf8Offset = Utils::Text::utf8NthLineOffset(doc, buffer, block.blockNumber() + 1);
|
||||
|
||||
Reference in New Issue
Block a user