ClangFormat: Add cursor position to the indenter interface

Sometimes it's imnportant where the cursor currently is
to properly format the code without affecting the current line.

Change-Id: I8b1fb11d2303adb5f960c7cb80a0ed2e6e45010f
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Ivan Donchevskii
2019-01-28 08:11:20 +01:00
parent 80fb0178fd
commit 1dee275f58
24 changed files with 185 additions and 95 deletions

View File

@@ -274,15 +274,19 @@ ClangFormatBaseIndenter::ClangFormatBaseIndenter(QTextDocument *doc)
{}
TextEditor::IndentationForBlock ClangFormatBaseIndenter::indentationForBlocks(
const QVector<QTextBlock> &blocks, const TextEditor::TabSettings & /*tabSettings*/)
const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings & /*tabSettings*/,
int cursorPositionInEditor)
{
TextEditor::IndentationForBlock ret;
for (QTextBlock block : blocks)
ret.insert(block.blockNumber(), indentFor(block));
ret.insert(block.blockNumber(), indentFor(block, cursorPositionInEditor));
return ret;
}
void ClangFormatBaseIndenter::indent(const QTextCursor &cursor, const QChar &typedChar)
void ClangFormatBaseIndenter::indent(const QTextCursor &cursor,
const QChar &typedChar,
int cursorPositionInEditor)
{
if (cursor.hasSelection()) {
// Calling currentBlock.next() might be unsafe because we change the document.
@@ -294,30 +298,33 @@ void ClangFormatBaseIndenter::indent(const QTextCursor &cursor, const QChar &typ
const QTextBlock currentBlock = m_doc->findBlockByNumber(currentBlockNumber);
if (currentBlock.isValid()) {
const int blocksAmount = m_doc->blockCount();
indentBlock(currentBlock, typedChar);
indentBlock(currentBlock, typedChar, cursorPositionInEditor);
QTC_CHECK(blocksAmount == m_doc->blockCount()
&& "ClangFormat plugin indentation changed the amount of blocks.");
}
}
} else {
indentBlock(cursor.block(), typedChar);
indentBlock(cursor.block(), typedChar, cursorPositionInEditor);
}
}
void ClangFormatBaseIndenter::indent(const QTextCursor &cursor,
const QChar &typedChar,
const TextEditor::TabSettings & /*tabSettings*/)
const TextEditor::TabSettings & /*tabSettings*/,
int cursorPositionInEditor)
{
indent(cursor, typedChar);
indent(cursor, typedChar, cursorPositionInEditor);
}
void ClangFormatBaseIndenter::reindent(const QTextCursor &cursor,
const TextEditor::TabSettings & /*tabSettings*/)
const TextEditor::TabSettings & /*tabSettings*/,
int cursorPositionInEditor)
{
indent(cursor, QChar::Null);
indent(cursor, QChar::Null, cursorPositionInEditor);
}
TextEditor::Replacements ClangFormatBaseIndenter::format(const QTextCursor &cursor)
TextEditor::Replacements ClangFormatBaseIndenter::format(const QTextCursor &cursor,
int cursorPositionInEditor)
{
int utf8Offset;
int utf8Length;
@@ -345,12 +352,16 @@ TextEditor::Replacements ClangFormatBaseIndenter::format(const QTextCursor &curs
}
TextEditor::Replacements ClangFormatBaseIndenter::format(
const QTextCursor &cursor, const TextEditor::TabSettings & /*tabSettings*/)
const QTextCursor &cursor,
const TextEditor::TabSettings & /*tabSettings*/,
int cursorPositionInEditor)
{
return format(cursor);
return format(cursor, cursorPositionInEditor);
}
void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block, const QChar &typedChar)
void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar,
int /*cursorPositionInEditor*/)
{
trimFirstNonEmptyBlock(block);
trimCurrentBlock(block);
@@ -363,12 +374,13 @@ void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block, const QChar &
void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings & /*tabSettings*/)
const TextEditor::TabSettings & /*tabSettings*/,
int cursorPositionInEditor)
{
indentBlock(block, typedChar);
indentBlock(block, typedChar, cursorPositionInEditor);
}
int ClangFormatBaseIndenter::indentFor(const QTextBlock &block)
int ClangFormatBaseIndenter::indentFor(const QTextBlock &block, int /*cursorPositionInEditor*/)
{
trimFirstNonEmptyBlock(block);
trimCurrentBlock(block);
@@ -388,9 +400,10 @@ int ClangFormatBaseIndenter::indentFor(const QTextBlock &block)
}
int ClangFormatBaseIndenter::indentFor(const QTextBlock &block,
const TextEditor::TabSettings & /*tabSettings*/)
const TextEditor::TabSettings & /*tabSettings*/,
int cursorPositionInEditor)
{
return indentFor(block);
return indentFor(block, cursorPositionInEditor);
}
bool ClangFormatBaseIndenter::isElectricCharacter(const QChar &ch) const
@@ -412,12 +425,12 @@ bool ClangFormatBaseIndenter::isElectricCharacter(const QChar &ch) const
void ClangFormatBaseIndenter::formatOrIndent(const QTextCursor &cursor,
const TextEditor::TabSettings & /*tabSettings*/,
int /*cursorPositionInEditor*/)
int cursorPositionInEditor)
{
if (formatCodeInsteadOfIndent())
format(cursor);
format(cursor, cursorPositionInEditor);
else
indent(cursor, QChar::Null);
indent(cursor, QChar::Null, cursorPositionInEditor);
}
clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const