From c8a53a66ad668624c32b65535ac123b33e8727c6 Mon Sep 17 00:00:00 2001 From: Leandro Melo <leandro.melo@nokia.com> Date: Tue, 9 Nov 2010 10:36:02 +0100 Subject: [PATCH] Editors: Continue refactoring indenters out of the editors. This is pretty much a complement of commit 3a684586fabf103b8e09cef31a18ffae1fd9f0c7, which is an attempt to make editors and indenters a bit more decoupled. Reviewed-by: Thorbjorn Lindeijer --- src/plugins/fakevim/fakevimplugin.cpp | 5 ++-- src/plugins/texteditor/basetexteditor.cpp | 29 ++++++++--------------- src/plugins/texteditor/basetexteditor.h | 11 +++------ src/plugins/texteditor/indenter.cpp | 11 +++++++++ src/plugins/texteditor/indenter.h | 7 +++++- 5 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/plugins/fakevim/fakevimplugin.cpp b/src/plugins/fakevim/fakevimplugin.cpp index 481020581ac..80bbef57be4 100644 --- a/src/plugins/fakevim/fakevimplugin.cpp +++ b/src/plugins/fakevim/fakevimplugin.cpp @@ -58,6 +58,7 @@ #include <texteditor/texteditorconstants.h> #include <texteditor/tabsettings.h> #include <texteditor/texteditorsettings.h> +#include <texteditor/indenter.h> #include <find/findplugin.h> #include <find/textfindconstants.h> @@ -935,7 +936,7 @@ void FakeVimPluginPrivate::checkForElectricCharacter(bool *result, QChar c) if (!handler) return; if (BaseTextEditor *bt = qobject_cast<BaseTextEditor *>(handler->widget())) - *result = bt->isElectricCharacter(c); + *result = bt->indenter()->isElectricCharacter(c); } void FakeVimPluginPrivate::handleExCommand(bool *handled, const ExCommand &cmd) @@ -1132,7 +1133,7 @@ void FakeVimPluginPrivate::indentRegion(int beginLine, int endLine, while (!cursor.atBlockEnd()) cursor.deleteChar(); } else { - bt->indentBlock(doc, block, typedChar); + bt->indenter()->indentBlock(doc, block, typedChar, bt); } block = block.next(); } diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index f2fe088e6c5..d505d1ebfe2 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -1344,7 +1344,7 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e) QChar electricChar; if (d->m_document->tabSettings().m_autoIndent) { foreach (QChar c, text) { - if (isElectricCharacter(c)) { + if (d->m_indenter->isElectricCharacter(c)) { electricChar = c; break; } @@ -1890,6 +1890,11 @@ void BaseTextEditor::setIndenter(Indenter *indenter) d->m_indenter.reset(indenter); } +Indenter *BaseTextEditor::indenter() const +{ + return d->m_indenter.data(); +} + void BaseTextEditor::setAutoCompleter(AutoCompleter *autoCompleter) { d->m_autoCompleter.reset(autoCompleter); @@ -1949,7 +1954,8 @@ BaseTextEditorPrivate::BaseTextEditorPrivate() m_requestAutoCompletionPosition(0), m_requestAutoCompletionTimer(0), m_cursorBlockNumber(-1), - m_autoCompleter(new AutoCompleter) + m_autoCompleter(new AutoCompleter), + m_indenter(new Indenter) { } @@ -4019,13 +4025,6 @@ void BaseTextEditor::zoomReset() emit requestZoomReset(); } -bool BaseTextEditor::isElectricCharacter(QChar ch) const -{ - if (!d->m_indenter.isNull()) - return d->m_indenter->isElectricCharacter(ch); - return false; -} - void BaseTextEditor::indentInsertedText(const QTextCursor &tc) { indent(tc.document(), tc, QChar::Null); @@ -4247,24 +4246,16 @@ int BaseTextEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor) return 1; } -void BaseTextEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar ch) -{ - if (!d->m_indenter.isNull()) - d->m_indenter->indentBlock(doc, block, ch, this); -} - void BaseTextEditor::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar) { maybeClearSomeExtraSelections(cursor); - if (!d->m_indenter.isNull()) - d->m_indenter->indent(doc, cursor, typedChar, this); + d->m_indenter->indent(doc, cursor, typedChar, this); } void BaseTextEditor::reindent(QTextDocument *doc, const QTextCursor &cursor) { maybeClearSomeExtraSelections(cursor); - if (!d->m_indenter.isNull()) - d->m_indenter->reindent(doc, cursor, this); + d->m_indenter->reindent(doc, cursor, this); } BaseTextEditor::Link BaseTextEditor::findLinkAt(const QTextCursor &, bool) diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index ba2c4d1d4c4..b17199d9726 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -225,6 +225,7 @@ public: QRegion translatedLineRegion(int lineStart, int lineEnd) const; void setIndenter(Indenter *indenter); + Indenter *indenter() const; void setAutoCompleter(AutoCompleter *autoCompleter); AutoCompleter *autoCompleter() const; @@ -424,14 +425,8 @@ public: virtual int paragraphSeparatorAboutToBeInserted(QTextCursor &cursor); void indentInsertedText(const QTextCursor &tc); - // Returns true if key triggers an indent. - virtual bool isElectricCharacter(QChar ch) const; - // Indent a text block based on previous line. Default does nothing - virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar); - // Indent at cursor. Calls indentBlock for selection or current line. - virtual void indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar); - // Reindent at cursor. Selection will be adjusted according to the indentation change of the first block - virtual void reindent(QTextDocument *doc, const QTextCursor &cursor); + void indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar); + void reindent(QTextDocument *doc, const QTextCursor &cursor); protected: static void countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen); diff --git a/src/plugins/texteditor/indenter.cpp b/src/plugins/texteditor/indenter.cpp index 8007b4209e2..97ae9ebf17c 100644 --- a/src/plugins/texteditor/indenter.cpp +++ b/src/plugins/texteditor/indenter.cpp @@ -70,6 +70,17 @@ bool Indenter::doIsElectricalCharacter(const QChar &) const return false; } +void Indenter::doIndentBlock(QTextDocument *doc, + const QTextBlock &block, + const QChar &typedChar, + BaseTextEditor *editor) +{ + Q_UNUSED(doc); + Q_UNUSED(block); + Q_UNUSED(typedChar); + Q_UNUSED(editor); +} + void Indenter::doIndent(QTextDocument *doc, const QTextCursor &cursor, const QChar &typedChar, BaseTextEditor *editor) { if (cursor.hasSelection()) { diff --git a/src/plugins/texteditor/indenter.h b/src/plugins/texteditor/indenter.h index 3a58f569125..22a95b888ed 100644 --- a/src/plugins/texteditor/indenter.h +++ b/src/plugins/texteditor/indenter.h @@ -50,15 +50,20 @@ public: Indenter(); virtual ~Indenter(); + // Returns true if key triggers an indent. bool isElectricCharacter(const QChar &ch) const; + // Indent a text block based on previous line. Default does nothing void indentBlock(QTextDocument *doc, const QTextBlock &block, const QChar &typedChar, BaseTextEditor *editor); + // Indent at cursor. Calls indentBlock for selection or current line. void indent(QTextDocument *doc, const QTextCursor &cursor, const QChar &typedChar, BaseTextEditor *editor); + // Reindent at cursor. Selection will be adjusted according to the indentation + // change of the first block. void reindent(QTextDocument *doc, const QTextCursor &cursor, BaseTextEditor *editor); private: @@ -66,7 +71,7 @@ private: virtual void doIndentBlock(QTextDocument *doc, const QTextBlock &block, const QChar &typedChar, - BaseTextEditor *editor) = 0; + BaseTextEditor *editor); virtual void doIndent(QTextDocument *doc, const QTextCursor &cursor, const QChar &typedChar, -- GitLab