diff options
author | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-11-26 12:44:44 +0100 |
---|---|---|
committer | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-11-27 10:20:17 +0000 |
commit | f1634df8b303fb2b107913c0deb16e1c5d42c111 (patch) | |
tree | d67ad683089830c73558ae305ba3543ae4339dae | |
parent | 386535cf181814c8f1a7d76cd69b081a32b1de26 (diff) | |
download | qt-creator-f1634df8b303fb2b107913c0deb16e1c5d42c111.tar.gz |
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>
-rw-r--r-- | src/plugins/clangformat/clangformatindenter.cpp | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/plugins/clangformat/clangformatindenter.cpp b/src/plugins/clangformat/clangformatindenter.cpp index 7f055d3d7e..18a79889c1 100644 --- a/src/plugins/clangformat/clangformatindenter.cpp +++ b/src/plugins/clangformat/clangformatindenter.cpp @@ -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); |