From 98fd59fd091e973e25061d44c0a6eb4b817ee13c Mon Sep 17 00:00:00 2001 From: hjk <hjk121@nokiamail.com> Date: Thu, 31 Jul 2014 14:44:35 +0200 Subject: [PATCH] TextEditor: Start moving data from *EditorWidget to *Editor Goal is to consolidate "controller" related data in the *Editor hierarchy. This patch introduces temporary "cross-Private" accessors dd() to keep the patches small. First item moved is the AutoCompleter, a glimps at long term benefits is the simplification in the JavaEditorWidget which is now essentially a BaseTextEditorWidget, only containing the still-wrong createEditor(). But that can only be move if the *Editors are self-contained, i.e. keep data themselves, not indirectly through the *EditorWidgets. Change-Id: Ia0ab90f0322bb17ac20458e6581069eed30acbaf Reviewed-by: Christian Stenger <christian.stenger@digia.com> Reviewed-by: hjk <hjk121@nokiamail.com> --- src/plugins/android/javaeditor.cpp | 8 +--- src/plugins/android/javaeditor.h | 1 - .../cppdocumentationcommenthelper.cpp | 2 +- src/plugins/cppeditor/cppeditor.cpp | 2 +- src/plugins/cppeditor/cppsnippetprovider.cpp | 2 +- src/plugins/glsleditor/glsleditor.cpp | 2 - src/plugins/glsleditor/glsleditoreditable.cpp | 2 + src/plugins/qmljseditor/qmljseditor.cpp | 2 - .../qmljseditor/qmljseditoreditable.cpp | 2 + .../qmljseditor/qmljssnippetprovider.cpp | 2 +- src/plugins/texteditor/basetexteditor.cpp | 44 ++++++++++++------- src/plugins/texteditor/basetexteditor.h | 10 +++-- 12 files changed, 42 insertions(+), 37 deletions(-) diff --git a/src/plugins/android/javaeditor.cpp b/src/plugins/android/javaeditor.cpp index 163941fcd9b..f9d22aec970 100644 --- a/src/plugins/android/javaeditor.cpp +++ b/src/plugins/android/javaeditor.cpp @@ -57,6 +57,7 @@ JavaEditor::JavaEditor(JavaEditorWidget *editor) setDuplicateSupported(true); setCommentStyle(Utils::CommentDefinition::CppStyle); setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<JavaCompletionAssistProvider>()); + setAutoCompleter(new JavaAutoCompleter); } Core::IEditor *JavaEditor::duplicate() @@ -74,18 +75,11 @@ Core::IEditor *JavaEditor::duplicate() JavaEditorWidget::JavaEditorWidget(QWidget *parent) : BaseTextEditorWidget(new JavaDocument(), parent) { - ctor(); } JavaEditorWidget::JavaEditorWidget(JavaEditorWidget *other) : BaseTextEditorWidget(other) { - ctor(); -} - -void JavaEditorWidget::ctor() -{ - setAutoCompleter(new JavaAutoCompleter); } TextEditor::BaseTextEditor *JavaEditorWidget::createEditor() diff --git a/src/plugins/android/javaeditor.h b/src/plugins/android/javaeditor.h index d6a9fcede64..8e8fc6a5d13 100644 --- a/src/plugins/android/javaeditor.h +++ b/src/plugins/android/javaeditor.h @@ -64,7 +64,6 @@ protected: private: JavaEditorWidget(BaseTextEditorWidget *); // avoid stupidity - void ctor(); }; class JavaDocument : public TextEditor::BaseTextDocument diff --git a/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp b/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp index 2bd2e2bcec9..7373da6055b 100644 --- a/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp +++ b/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp @@ -253,7 +253,7 @@ bool CppDocumentationCommentHelper::handleKeyPressEvent(QKeyEvent *e) const if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) { QTextCursor cursor = m_editorWidget->textCursor(); - if (!m_editorWidget->autoCompleter()->isInComment(cursor)) + if (!m_editorWidget->editor()->autoCompleter()->isInComment(cursor)) return false; // We are interested on two particular cases: diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index c692029fd9c..14435797479 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -117,6 +117,7 @@ CPPEditor::CPPEditor(CppEditorWidget *editor) setCompletionAssistProvider([this] () -> TextEditor::CompletionAssistProvider * { return CppModelManagerInterface::instance()->cppEditorSupport(this)->completionAssistProvider(); }); + setAutoCompleter(new CppAutoCompleter); } Q_GLOBAL_STATIC(CppTools::SymbolFinder, symbolFinder) @@ -197,7 +198,6 @@ void CppEditorWidget::ctor() setParenthesesMatchingEnabled(true); setMarksVisible(true); setCodeFoldingSupported(true); - setAutoCompleter(new CppAutoCompleter); if (d->m_modelManager) { CppEditorSupport *editorSupport = d->m_modelManager->cppEditorSupport(editor()); diff --git a/src/plugins/cppeditor/cppsnippetprovider.cpp b/src/plugins/cppeditor/cppsnippetprovider.cpp index c71c1267620..5212e53e199 100644 --- a/src/plugins/cppeditor/cppsnippetprovider.cpp +++ b/src/plugins/cppeditor/cppsnippetprovider.cpp @@ -64,5 +64,5 @@ void CppSnippetProvider::decorateEditor(TextEditor::SnippetEditorWidget *editor) { editor->setSyntaxHighlighter(new CppHighlighter); editor->baseTextDocument()->setIndenter(new CppTools::CppQtStyleIndenter); - editor->setAutoCompleter(new CppAutoCompleter); + editor->editor()->setAutoCompleter(new CppAutoCompleter); } diff --git a/src/plugins/glsleditor/glsleditor.cpp b/src/plugins/glsleditor/glsleditor.cpp index 3164e41f3e4..a27bdbd8392 100644 --- a/src/plugins/glsleditor/glsleditor.cpp +++ b/src/plugins/glsleditor/glsleditor.cpp @@ -33,7 +33,6 @@ #include "glsleditorplugin.h" #include "glslhighlighter.h" #include "glslautocompleter.h" -#include "glslindenter.h" #include "glslcompletionassist.h" #include <glsl/glsllexer.h> @@ -160,7 +159,6 @@ void GlslEditorWidget::ctor() setParenthesesMatchingEnabled(true); setMarksVisible(true); setCodeFoldingSupported(true); - setAutoCompleter(new GLSLCompleter()); m_updateDocumentTimer = new QTimer(this); m_updateDocumentTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL); diff --git a/src/plugins/glsleditor/glsleditoreditable.cpp b/src/plugins/glsleditor/glsleditoreditable.cpp index e44be24c83a..857af69cc8e 100644 --- a/src/plugins/glsleditor/glsleditoreditable.cpp +++ b/src/plugins/glsleditor/glsleditoreditable.cpp @@ -30,6 +30,7 @@ #include "glsleditoreditable.h" #include "glsleditorconstants.h" #include "glslcompletionassist.h" +#include "glslautocompleter.h" #include <texteditor/texteditorconstants.h> @@ -48,6 +49,7 @@ GlslEditor::GlslEditor(GlslEditorWidget *editor) setDuplicateSupported(true); setCommentStyle(Utils::CommentDefinition::CppStyle); setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<GLSLCompletionAssistProvider>()); + setAutoCompleter(new GLSLCompleter); } } // namespace Internal diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp index dd3738a39a7..deb0681ed75 100644 --- a/src/plugins/qmljseditor/qmljseditor.cpp +++ b/src/plugins/qmljseditor/qmljseditor.cpp @@ -35,7 +35,6 @@ #include "qmljseditorplugin.h" #include "qmloutlinemodel.h" #include "qmljsfindreferences.h" -#include "qmljsautocompleter.h" #include "qmljscompletionassist.h" #include "qmljsquickfixassist.h" @@ -120,7 +119,6 @@ void QmlJSTextEditorWidget::ctor() setParenthesesMatchingEnabled(true); setMarksVisible(true); setCodeFoldingSupported(true); - setAutoCompleter(new AutoCompleter); setLanguageSettingsId(QmlJSTools::Constants::QML_JS_SETTINGS_ID); m_updateUsesTimer = new QTimer(this); diff --git a/src/plugins/qmljseditor/qmljseditoreditable.cpp b/src/plugins/qmljseditor/qmljseditoreditable.cpp index f7a7baf23e6..e2d77d3cf55 100644 --- a/src/plugins/qmljseditor/qmljseditoreditable.cpp +++ b/src/plugins/qmljseditor/qmljseditoreditable.cpp @@ -31,6 +31,7 @@ #include "qmljseditor.h" #include "qmljseditorconstants.h" #include "qmljscompletionassist.h" +#include "qmljsautocompleter.h" #include <qmljstools/qmljstoolsconstants.h> #include <texteditor/texteditorconstants.h> @@ -56,6 +57,7 @@ QmlJSEditor::QmlJSEditor(QmlJSTextEditorWidget *editor) setDuplicateSupported(true); setCommentStyle(Utils::CommentDefinition::CppStyle); setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<Internal::QmlJSCompletionAssistProvider>()); + setAutoCompleter(new AutoCompleter); } bool QmlJSEditor::isDesignModePreferred() const diff --git a/src/plugins/qmljseditor/qmljssnippetprovider.cpp b/src/plugins/qmljseditor/qmljssnippetprovider.cpp index 746765700eb..ccbe1031a84 100644 --- a/src/plugins/qmljseditor/qmljssnippetprovider.cpp +++ b/src/plugins/qmljseditor/qmljssnippetprovider.cpp @@ -66,5 +66,5 @@ void QmlJSSnippetProvider::decorateEditor(TextEditor::SnippetEditorWidget *edito { editor->setSyntaxHighlighter(new Highlighter); editor->baseTextDocument()->setIndenter(new Indenter); - editor->setAutoCompleter(new AutoCompleter); + editor->editor()->setAutoCompleter(new AutoCompleter); } diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 5c4e85988da..cac0518166d 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -188,6 +188,9 @@ private: class BaseTextEditorPrivate { public: + BaseTextEditorPrivate() + {} + // Note: This is always a copy of IContext::m_widget. BaseTextEditorWidget *m_editorWidget; @@ -199,6 +202,7 @@ public: Utils::LineColumnLabel *m_fileEncodingLabel; CommentDefinition m_commentDefinition; std::function<CompletionAssistProvider *()> m_completionAssistProvider; + QScopedPointer<AutoCompleter> m_autoCompleter; }; class BaseTextEditorWidgetPrivate @@ -209,6 +213,9 @@ class BaseTextEditorWidgetPrivate public: BaseTextEditorWidgetPrivate(BaseTextEditorWidget *parent); + // FIXME: Remove after relevant members have been moved to BaseTextEditorPrivate + BaseTextEditorPrivate *dd() { return q->editor()->d; } + void setupDocumentSignals(); void updateLineSelectionColor(); @@ -364,8 +371,6 @@ public: QPoint m_markDragStart; bool m_markDragging; - QScopedPointer<AutoCompleter> m_autoCompleter; - QScopedPointer<Internal::ClipboardAssistProvider> m_clipboardAssistProvider; bool m_isMissingSyntaxDefinition; @@ -1892,7 +1897,7 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e) cursor.beginEditBlock(); int extraBlocks = - d->m_autoCompleter->paragraphSeparatorAboutToBeInserted(cursor, + dd()->m_autoCompleter->paragraphSeparatorAboutToBeInserted(cursor, d->m_document->tabSettings()); QString previousIndentationString; @@ -2092,16 +2097,17 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e) case Qt::Key_Insert: if (ro) break; if (e->modifiers() == Qt::NoModifier) { + AutoCompleter *ac = dd()->m_autoCompleter.data(); if (inOverwriteMode) { - d->m_autoCompleter->setAutoParenthesesEnabled(d->autoParenthesisOverwriteBackup); - d->m_autoCompleter->setSurroundWithEnabled(d->surroundWithEnabledOverwriteBackup); + ac->setAutoParenthesesEnabled(d->autoParenthesisOverwriteBackup); + ac->setSurroundWithEnabled(d->surroundWithEnabledOverwriteBackup); setOverwriteMode(false); viewport()->update(); } else { - d->autoParenthesisOverwriteBackup = d->m_autoCompleter->isAutoParenthesesEnabled(); - d->surroundWithEnabledOverwriteBackup = d->m_autoCompleter->isSurroundWithEnabled(); - d->m_autoCompleter->setAutoParenthesesEnabled(false); - d->m_autoCompleter->setSurroundWithEnabled(false); + d->autoParenthesisOverwriteBackup = ac->isAutoParenthesesEnabled(); + d->surroundWithEnabledOverwriteBackup = ac->isSurroundWithEnabled(); + ac->setAutoParenthesesEnabled(false); + ac->setSurroundWithEnabled(false); setOverwriteMode(true); } e->accept(); @@ -2150,7 +2156,7 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e) // only go here if control is not pressed, except if also alt is pressed // because AltGr maps to Alt + Ctrl QTextCursor cursor = textCursor(); - const QString &autoText = d->m_autoCompleter->autoComplete(cursor, eventText); + const QString &autoText = dd()->m_autoCompleter->autoComplete(cursor, eventText); QChar electricChar; if (d->m_document->typingSettings().m_autoIndent) { @@ -2190,7 +2196,7 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e) //Select the inserted text, to be able to re-indent the inserted text cursor.setPosition(pos, QTextCursor::KeepAnchor); } - if (!electricChar.isNull() && d->m_autoCompleter->contextAllowsElectricCharacters(cursor)) + if (!electricChar.isNull() && dd()->m_autoCompleter->contextAllowsElectricCharacters(cursor)) d->m_document->autoIndent(cursor, electricChar); if (!autoText.isEmpty()) cursor.setPosition(autoText.length() == 1 ? cursor.position() : cursor.anchor()); @@ -2666,12 +2672,12 @@ int BaseTextEditorWidget::visibleWrapColumn() const return d->m_visibleWrapColumn; } -void BaseTextEditorWidget::setAutoCompleter(AutoCompleter *autoCompleter) +void BaseTextEditor::setAutoCompleter(AutoCompleter *autoCompleter) { d->m_autoCompleter.reset(autoCompleter); } -AutoCompleter *BaseTextEditorWidget::autoCompleter() const +AutoCompleter *BaseTextEditor::autoCompleter() const { return d->m_autoCompleter.data(); } @@ -2721,7 +2727,6 @@ BaseTextEditorWidgetPrivate::BaseTextEditorWidgetPrivate(BaseTextEditorWidget *p m_cursorBlockNumber(-1), m_blockCount(0), m_markDragging(false), - m_autoCompleter(new AutoCompleter), m_clipboardAssistProvider(new Internal::ClipboardAssistProvider), m_isMissingSyntaxDefinition(false) { @@ -5060,7 +5065,7 @@ void BaseTextEditorWidgetPrivate::handleBackspaceKey() const TextEditor::TabSettings &tabSettings = m_document->tabSettings(); const TextEditor::TypingSettings &typingSettings = m_document->typingSettings(); - if (typingSettings.m_autoIndent && m_autoCompleter->autoBackspace(cursor)) + if (typingSettings.m_autoIndent && dd()->m_autoCompleter->autoBackspace(cursor)) return; bool handled = false; @@ -6003,8 +6008,8 @@ void BaseTextEditorWidget::setStorageSettings(const StorageSettings &storageSett void BaseTextEditorWidget::setCompletionSettings(const TextEditor::CompletionSettings &completionSettings) { - d->m_autoCompleter->setAutoParenthesesEnabled(completionSettings.m_autoInsertBrackets); - d->m_autoCompleter->setSurroundWithEnabled(completionSettings.m_autoInsertBrackets + dd()->m_autoCompleter->setAutoParenthesesEnabled(completionSettings.m_autoInsertBrackets); + dd()->m_autoCompleter->setSurroundWithEnabled(completionSettings.m_autoInsertBrackets && completionSettings.m_surroundingAutoBrackets); } @@ -6678,6 +6683,11 @@ void BaseTextEditorWidget::doFoo() { #endif } +BaseTextEditorPrivate *BaseTextEditorWidget::dd() const +{ + return editor()->d; +} + BaseTextBlockSelection::BaseTextBlockSelection(const BaseTextBlockSelection &other) { positionBlock = other.positionBlock; diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index f55f745966b..21b1457a03e 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -130,7 +130,6 @@ public: static BaseTextEditor *currentTextEditor(); - friend class BaseTextEditorWidget; BaseTextEditorWidget *editorWidget() const; BaseTextDocument *baseTextDocument(); @@ -195,6 +194,9 @@ public: QObject *fileEncodingLabel() const; // FIXME: Remove + void setAutoCompleter(AutoCompleter *autoCompleter); + AutoCompleter *autoCompleter() const; + signals: void markRequested(TextEditor::BaseTextEditor *editor, int line, TextEditor::BaseTextEditor::MarkRequestKind kind); void markContextMenuRequested(TextEditor::BaseTextEditor *editor, int line, QMenu *menu); @@ -210,6 +212,8 @@ private slots: void setFileEncodingLabelText(const QString &text); private: + friend class Internal::BaseTextEditorWidgetPrivate; + friend class BaseTextEditorWidget; Internal::BaseTextEditorPrivate *d; }; @@ -315,9 +319,6 @@ public: QRegion translatedLineRegion(int lineStart, int lineEnd) const; - void setAutoCompleter(AutoCompleter *autoCompleter); - AutoCompleter *autoCompleter() const; - QPoint toolTipPosition(const QTextCursor &c) const; void invokeAssist(AssistKind assistKind, IAssistProvider *provider = 0); @@ -619,6 +620,7 @@ private slots: private: Internal::BaseTextEditorWidgetPrivate *d; + Internal::BaseTextEditorPrivate *dd() const; friend class Internal::BaseTextEditorWidgetPrivate; friend class Internal::TextEditorOverlay; friend class RefactorOverlay; -- GitLab