Editor: add formatter support

Change-Id: I65590273b2541e08a39970cd9bb4739a5634b2f7
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2019-11-18 10:38:35 +01:00
parent cadb00cdf3
commit 4070d6a289
14 changed files with 154 additions and 35 deletions

View File

@@ -349,24 +349,6 @@ Utils::Text::Replacements utf16Replacements(const QTextDocument *doc,
return convertedReplacements;
}
void applyReplacements(QTextDocument *doc, const Utils::Text::Replacements &replacements)
{
if (replacements.empty())
return;
int fullOffsetShift = 0;
QTextCursor editCursor(doc);
for (const Utils::Text::Replacement &replacement : replacements) {
editCursor.setPosition(replacement.offset + fullOffsetShift);
editCursor.movePosition(QTextCursor::NextCharacter,
QTextCursor::KeepAnchor,
replacement.length);
editCursor.removeSelectedText();
editCursor.insertText(replacement.text);
fullOffsetShift += replacement.text.length() - replacement.length;
}
}
QString selectedLines(QTextDocument *doc, const QTextBlock &startBlock, const QTextBlock &endBlock)
{
return Utils::Text::textAt(QTextCursor(doc),
@@ -554,7 +536,7 @@ Utils::Text::Replacements ClangFormatBaseIndenter::format(
clangReplacements = clangReplacements.merge(formatReplacements);
const Utils::Text::Replacements toReplace = utf16Replacements(m_doc, buffer, clangReplacements);
applyReplacements(m_doc, toReplace);
Utils::Text::applyReplacements(m_doc, toReplace);
return toReplace;
}

View File

@@ -287,7 +287,7 @@ void ClangFormatConfigWidget::updatePreview()
QTextCursor cursor(m_preview->document());
cursor.setPosition(0);
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
m_preview->textDocument()->autoFormatOrIndent(cursor);
m_preview->textDocument()->autoIndent(cursor);
}
static inline void ltrim(std::string &s)