diff --git a/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp b/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp index d2799425fb8..aa3b883b9ae 100644 --- a/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp +++ b/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp @@ -169,7 +169,7 @@ void BindingEditorFactory::decorateEditor(TextEditor::TextEditorWidget *editor) { editor->textDocument()->resetSyntaxHighlighter( [] { return new QmlJSEditor::QmlJSHighlighter(); }); - editor->textDocument()->setIndenter(new QmlJSEditor::Internal::Indenter( + editor->textDocument()->setIndenter(QmlJSEditor::createQmlJsIndenter( editor->textDocument()->document())); editor->setAutoCompleter(new QmlJSEditor::AutoCompleter); } diff --git a/src/plugins/qmldesigner/designercore/model/basetexteditmodifier.cpp b/src/plugins/qmldesigner/designercore/model/basetexteditmodifier.cpp index d0d8953415a..d3799e997bf 100644 --- a/src/plugins/qmldesigner/designercore/model/basetexteditmodifier.cpp +++ b/src/plugins/qmldesigner/designercore/model/basetexteditmodifier.cpp @@ -33,20 +33,8 @@ void BaseTextEditModifier::indentLines(int startLine, int endLine) if (!m_textEdit) return; - TextEditor::TextDocument *baseTextEditorDocument = m_textEdit->textDocument(); - TextEditor::TabSettings tabSettings = baseTextEditorDocument->tabSettings(); - QTextCursor tc(textDocument()); - - tc.beginEditBlock(); - for (int i = startLine; i <= endLine; i++) { - QTextBlock start = textDocument()->findBlockByNumber(i); - - if (start.isValid()) { - QmlJSEditor::Internal::Indenter indenter(textDocument()); - indenter.indentBlock(start, QChar::Null, tabSettings); - } - } - tc.endEditBlock(); + QmlJSEditor::indentQmlJs(textDocument(), startLine, endLine, + m_textEdit->textDocument()->tabSettings()); } void BaseTextEditModifier::indent(int offset, int length) diff --git a/src/plugins/qmldesigner/designercore/model/plaintexteditmodifier.cpp b/src/plugins/qmldesigner/designercore/model/plaintexteditmodifier.cpp index a1f88b3a687..f37c9a6841d 100644 --- a/src/plugins/qmldesigner/designercore/model/plaintexteditmodifier.cpp +++ b/src/plugins/qmldesigner/designercore/model/plaintexteditmodifier.cpp @@ -186,20 +186,6 @@ void IndentingTextEditModifier::indent(int offset, int length) void IndentingTextEditModifier::indentLines(int startLine, int endLine) { - if (startLine < 0) - return; - - QTextCursor tc(textDocument()); - - tc.beginEditBlock(); - for (int i = startLine; i <= endLine; i++) { - QTextBlock start = textDocument()->findBlockByNumber(i); - - if (start.isValid()) { - QmlJSEditor::Internal::Indenter indenter(textDocument()); - indenter.indentBlock(start, QChar::Null, m_tabSettings); - } - } - tc.endEditBlock(); + QmlJSEditor::indentQmlJs(textDocument(), startLine, endLine, m_tabSettings); } diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp index 710663ae364..51778d9717d 100644 --- a/src/plugins/qmljseditor/qmljseditor.cpp +++ b/src/plugins/qmljseditor/qmljseditor.cpp @@ -1184,7 +1184,7 @@ QmlJSEditorFactory::QmlJSEditorFactory(Utils::Id _id) void QmlJSEditorFactory::decorateEditor(TextEditorWidget *editor) { editor->textDocument()->resetSyntaxHighlighter([] { return new QmlJSHighlighter(); }); - editor->textDocument()->setIndenter(new Internal::Indenter(editor->textDocument()->document())); + editor->textDocument()->setIndenter(createQmlJsIndenter(editor->textDocument()->document())); editor->setAutoCompleter(new AutoCompleter); } diff --git a/src/plugins/qmljseditor/qmljseditordocument.cpp b/src/plugins/qmljseditor/qmljseditordocument.cpp index 2c145f38c7c..9cec12c996d 100644 --- a/src/plugins/qmljseditor/qmljseditordocument.cpp +++ b/src/plugins/qmljseditor/qmljseditordocument.cpp @@ -822,7 +822,7 @@ QmlJSEditorDocument::QmlJSEditorDocument(Utils::Id id) d, &Internal::QmlJSEditorDocumentPrivate::settingsChanged); resetSyntaxHighlighter([] { return new QmlJSHighlighter(); }); setCodec(QTextCodec::codecForName("UTF-8")); // qml files are defined to be utf-8 - setIndenter(new Internal::Indenter(document())); + setIndenter(createQmlJsIndenter(document())); } bool QmlJSEditorDocument::supportsCodec(const QTextCodec *codec) const diff --git a/src/plugins/qmljseditor/quicktoolbar.cpp b/src/plugins/qmljseditor/quicktoolbar.cpp index 2e1d18d9bef..9154b7d47d9 100644 --- a/src/plugins/qmljseditor/quicktoolbar.cpp +++ b/src/plugins/qmljseditor/quicktoolbar.cpp @@ -396,17 +396,8 @@ void QuickToolBar::onEnabledChanged(bool b) void QuickToolBar::indentLines(int startLine, int endLine) { - if (startLine > 0) { - TextEditor::TabSettings tabSettings = m_editorWidget->textDocument()->tabSettings(); - for (int i = startLine; i <= endLine; i++) { - QTextBlock start = m_editorWidget->document()->findBlockByNumber(i); - - if (start.isValid()) { - QmlJSEditor::Internal::Indenter indenterMy(m_editorWidget->document()); - indenterMy.indentBlock(start, QChar::Null, tabSettings); - } - } - } + QmlJSEditor::indentQmlJs(m_editorWidget->document(), startLine, endLine, + m_editorWidget->textDocument()->tabSettings()); } ContextPaneWidget *QuickToolBar::contextWidget() diff --git a/src/plugins/qmljstools/qmljscodestylepreferencesfactory.cpp b/src/plugins/qmljstools/qmljscodestylepreferencesfactory.cpp index 1b5bd092583..a1eeb7b1ace 100644 --- a/src/plugins/qmljstools/qmljscodestylepreferencesfactory.cpp +++ b/src/plugins/qmljstools/qmljscodestylepreferencesfactory.cpp @@ -64,7 +64,7 @@ TextEditor::CodeStyleEditorWidget *QmlJSCodeStylePreferencesFactory::createEdito TextEditor::Indenter *QmlJSCodeStylePreferencesFactory::createIndenter(QTextDocument *doc) const { - return new QmlJSEditor::Internal::Indenter(doc); + return QmlJSEditor::createQmlJsIndenter(doc); } QString QmlJSCodeStylePreferencesFactory::snippetProviderGroupId() const diff --git a/src/plugins/qmljstools/qmljsindenter.cpp b/src/plugins/qmljstools/qmljsindenter.cpp index ec87f24c0c5..86d0466466b 100644 --- a/src/plugins/qmljstools/qmljsindenter.cpp +++ b/src/plugins/qmljstools/qmljsindenter.cpp @@ -6,30 +6,47 @@ #include #include -#include #include #include -using namespace QmlJSEditor; -using namespace Internal; +using namespace TextEditor; -Indenter::Indenter(QTextDocument *doc) - : TextEditor::TextIndenter(doc) -{} +namespace QmlJSEditor { +namespace Internal { -Indenter::~Indenter() = default; - -bool Indenter::isElectricCharacter(const QChar &ch) const +class QmlJsIndenter final : public TextEditor::TextIndenter { - if (ch == QLatin1Char('{') - || ch == QLatin1Char('}') - || ch == QLatin1Char(']') - || ch == QLatin1Char(':')) - return true; - return false; +public: + explicit QmlJsIndenter(QTextDocument *doc) + : TextEditor::TextIndenter(doc) + {} + + bool isElectricCharacter(const QChar &ch) const final; + void indentBlock(const QTextBlock &block, + const QChar &typedChar, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) final; + void invalidateCache() final; + + int indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) final; + int visualIndentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings) final; + TextEditor::IndentationForBlock indentationForBlocks(const QVector &blocks, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) final; +}; + +bool QmlJsIndenter::isElectricCharacter(const QChar &ch) const +{ + return ch == QLatin1Char('{') + || ch == QLatin1Char('}') + || ch == QLatin1Char(']') + || ch == QLatin1Char(':'); } -void Indenter::indentBlock(const QTextBlock &block, +void QmlJsIndenter::indentBlock(const QTextBlock &block, const QChar &typedChar, const TextEditor::TabSettings &tabSettings, int /*cursorPositionInEditor*/) @@ -52,13 +69,13 @@ void Indenter::indentBlock(const QTextBlock &block, tabSettings.indentLine(block, depth); } -void Indenter::invalidateCache() +void QmlJsIndenter::invalidateCache() { QmlJSTools::CreatorCodeFormatter codeFormatter; codeFormatter.invalidateCache(m_doc); } -int Indenter::indentFor(const QTextBlock &block, +int QmlJsIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings, int /*cursorPositionInEditor*/) { @@ -67,12 +84,12 @@ int Indenter::indentFor(const QTextBlock &block, return codeFormatter.indentFor(block); } -int Indenter::visualIndentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) +int QmlJsIndenter::visualIndentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) { return indentFor(block, tabSettings); } -TextEditor::IndentationForBlock Indenter::indentationForBlocks( +TextEditor::IndentationForBlock QmlJsIndenter::indentationForBlocks( const QVector &blocks, const TextEditor::TabSettings &tabSettings, int /*cursorPositionInEditor*/) @@ -86,3 +103,31 @@ TextEditor::IndentationForBlock Indenter::indentationForBlocks( ret.insert(block.blockNumber(), codeFormatter.indentFor(block)); return ret; } + +} // Internal + +TextEditor::TextIndenter *createQmlJsIndenter(QTextDocument *doc) +{ + return new Internal::QmlJsIndenter(doc); +} + +void indentQmlJs(QTextDocument *doc, int startLine, int endLine, const TextEditor::TabSettings &tabSettings) +{ + if (startLine <= 0) + return; + + QTextCursor tc(doc); + + tc.beginEditBlock(); + for (int i = startLine; i <= endLine; i++) { + // FIXME: block.next() should be faster. + QTextBlock block = doc->findBlockByNumber(i); + if (block.isValid()) { + Internal::QmlJsIndenter indenter(doc); + indenter.indentBlock(block, QChar::Null, tabSettings); + } + } + tc.endEditBlock(); +} + +} // QmlJsEditor diff --git a/src/plugins/qmljstools/qmljsindenter.h b/src/plugins/qmljstools/qmljsindenter.h index d39b3175884..c7f62bd4ca4 100644 --- a/src/plugins/qmljstools/qmljsindenter.h +++ b/src/plugins/qmljstools/qmljsindenter.h @@ -8,30 +8,10 @@ #include namespace QmlJSEditor { -namespace Internal { -class QMLJSTOOLS_EXPORT Indenter : public TextEditor::TextIndenter -{ -public: - explicit Indenter(QTextDocument *doc); - ~Indenter() override; +QMLJSTOOLS_EXPORT TextEditor::TextIndenter *createQmlJsIndenter(QTextDocument *doc); - bool isElectricCharacter(const QChar &ch) const override; - void indentBlock(const QTextBlock &block, - const QChar &typedChar, - const TextEditor::TabSettings &tabSettings, - int cursorPositionInEditor = -1) override; - void invalidateCache() override; +QMLJSTOOLS_EXPORT void indentQmlJs(QTextDocument *doc, int startLine, int endLine, + const TextEditor::TabSettings &tabSettings); - int indentFor(const QTextBlock &block, - const TextEditor::TabSettings &tabSettings, - int cursorPositionInEditor = -1) override; - int visualIndentFor(const QTextBlock &block, - const TextEditor::TabSettings &tabSettings) override; - TextEditor::IndentationForBlock indentationForBlocks(const QVector &blocks, - const TextEditor::TabSettings &tabSettings, - int cursorPositionInEditor = -1) override; -}; - -} // Internal } // QmlJSEditor