OSDN Git Service

Editors: Continue refactoring indenters out of the editors.
authorLeandro Melo <leandro.melo@nokia.com>
Tue, 9 Nov 2010 09:36:02 +0000 (10:36 +0100)
committerLeandro Melo <leandro.melo@nokia.com>
Tue, 9 Nov 2010 10:01:48 +0000 (11:01 +0100)
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
src/plugins/texteditor/basetexteditor.cpp
src/plugins/texteditor/basetexteditor.h
src/plugins/texteditor/indenter.cpp
src/plugins/texteditor/indenter.h

index 4810205..80bbef5 100644 (file)
@@ -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();
     }
index f2fe088..d505d1e 100644 (file)
@@ -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)
index ba2c4d1..b17199d 100644 (file)
@@ -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);
index 8007b42..97ae9eb 100644 (file)
@@ -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()) {
index 3a58f56..22a95b8 100644 (file)
@@ -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,