diff --git a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp index d0e34e06ff5..1257d936eff 100644 --- a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp @@ -34,14 +34,10 @@ #include "cmakeproject.h" #include -#include -#include #include #include #include -#include #include -#include #include @@ -59,7 +55,6 @@ using namespace Core; using namespace TextEditor; -using namespace CMakeProjectManager::Constants; namespace CMakeProjectManager { namespace Internal { @@ -74,29 +69,24 @@ CMakeEditor::CMakeEditor() setDuplicateSupported(true); setCommentStyle(Utils::CommentDefinition::HashStyle); setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject()); - setEditorCreator([]() { return new CMakeEditor; }); - setWidgetCreator([]() { return new CMakeEditorWidget; }); - - setDocumentCreator([this]() -> BaseTextDocument * { - auto doc = new CMakeDocument; - connect(doc, &IDocument::changed, this, &CMakeEditor::markAsChanged); - return doc; - }); } -void CMakeEditor::markAsChanged() +void CMakeEditor::finalizeInitialization() { - if (!document()->isModified()) - return; - InfoBar *infoBar = document()->infoBar(); - Id infoRunCmake("CMakeEditor.RunCMake"); - if (!infoBar->canInfoBeAdded(infoRunCmake)) - return; - InfoBarEntry info(infoRunCmake, - tr("Changes to cmake files are shown in the project tree after building."), - InfoBarEntry::GlobalSuppressionEnabled); - info.setCustomButtonInfo(tr("Build now"), this, SLOT(build())); - infoBar->addInfo(info); + connect(document(), &IDocument::changed, [this]() { + BaseTextDocument *document = textDocument(); + if (!document->isModified()) + return; + InfoBar *infoBar = document->infoBar(); + Id infoRunCmake("CMakeEditor.RunCMake"); + if (!infoBar->canInfoBeAdded(infoRunCmake)) + return; + InfoBarEntry info(infoRunCmake, + tr("Changes to cmake files are shown in the project tree after building."), + InfoBarEntry::GlobalSuppressionEnabled); + info.setCustomButtonInfo(tr("Build now"), this, SLOT(build())); + infoBar->addInfo(info); + }); } void CMakeEditor::build() @@ -158,6 +148,18 @@ QString CMakeEditor::contextHelpId() const // CMakeEditorWidget // +class CMakeEditorWidget : public BaseTextEditorWidget +{ +public: + CMakeEditorWidget(); + +private: + bool save(const QString &fileName = QString()); + Link findLinkAt(const QTextCursor &cursor, bool resolveTarget = true, bool inNextSplit = false); + BaseTextEditor *createEditor(); + void contextMenuEvent(QContextMenuEvent *e); +}; + CMakeEditorWidget::CMakeEditorWidget() { setCodeFoldingSupported(true); @@ -251,13 +253,19 @@ CMakeEditorWidget::Link CMakeEditorWidget::findLinkAt(const QTextCursor &cursor, // CMakeDocument // +class CMakeDocument : public BaseTextDocument +{ +public: + CMakeDocument(); + + QString defaultPath() const; + QString suggestedFileName() const; +}; + CMakeDocument::CMakeDocument() { setId(Constants::CMAKE_EDITOR_ID); setMimeType(QLatin1String(Constants::CMAKEMIMETYPE)); - - MimeType mimeType = MimeDatabase::findByType(QLatin1String(Constants::CMAKEMIMETYPE)); - setSyntaxHighlighter(TextEditor::createGenericSyntaxHighlighter(mimeType)); } QString CMakeDocument::defaultPath() const @@ -283,20 +291,20 @@ CMakeEditorFactory::CMakeEditorFactory() addMimeType(Constants::CMAKEMIMETYPE); addMimeType(Constants::CMAKEPROJECTMIMETYPE); - new TextEditorActionHandler(this, Constants::C_CMAKEEDITOR, + setEditorCreator([]() { return new CMakeEditor; }); + setEditorWidgetCreator([]() { return new CMakeEditorWidget; }); + setDocumentCreator([]() { return new CMakeDocument; }); + setGenericSyntaxHighlighter(QLatin1String(Constants::CMAKEMIMETYPE)); + + setEditorActionHandlers(Constants::C_CMAKEEDITOR, TextEditorActionHandler::UnCommentSelection | TextEditorActionHandler::JumpToFileUnderCursor); ActionContainer *contextMenu = ActionManager::createMenu(Constants::M_CONTEXT); contextMenu->addAction(ActionManager::command(TextEditor::Constants::JUMP_TO_FILE_UNDER_CURSOR)); - contextMenu->addSeparator(Context(C_CMAKEEDITOR)); + contextMenu->addSeparator(Context(Constants::C_CMAKEEDITOR)); contextMenu->addAction(ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION)); } -IEditor *CMakeEditorFactory::createEditor() -{ - return new CMakeEditor; -} - } // namespace Internal } // namespace CMakeProjectManager diff --git a/src/plugins/cmakeprojectmanager/cmakeeditor.h b/src/plugins/cmakeprojectmanager/cmakeeditor.h index 5d97257ac4f..999dcf6d130 100644 --- a/src/plugins/cmakeprojectmanager/cmakeeditor.h +++ b/src/plugins/cmakeprojectmanager/cmakeeditor.h @@ -32,8 +32,6 @@ #include #include -#include -#include namespace CMakeProjectManager { namespace Internal { @@ -47,47 +45,21 @@ class CMakeEditor : public TextEditor::BaseTextEditor public: CMakeEditor(); + void finalizeInitialization(); QString contextHelpId() const; friend class CMakeEditorWidget; -private slots: - void markAsChanged(); +public slots: void build(); }; -class CMakeEditorWidget : public TextEditor::BaseTextEditorWidget -{ - Q_OBJECT - -public: - CMakeEditorWidget(); - -private: - bool save(const QString &fileName = QString()); - Link findLinkAt(const QTextCursor &cursor, bool resolveTarget = true, bool inNextSplit = false); - TextEditor::BaseTextEditor *createEditor(); - void contextMenuEvent(QContextMenuEvent *e); -}; - -class CMakeDocument : public TextEditor::BaseTextDocument -{ - Q_OBJECT - -public: - CMakeDocument(); - - QString defaultPath() const; - QString suggestedFileName() const; -}; - -class CMakeEditorFactory : public Core::IEditorFactory +class CMakeEditorFactory : public TextEditor::BaseTextEditorFactory { Q_OBJECT public: CMakeEditorFactory(); - Core::IEditor *createEditor(); }; } // namespace Internal diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 69254299ec4..1c7e9d346fc 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -7316,6 +7316,9 @@ BaseTextEditor *BaseTextEditorFactory::createEditorHelper(const BaseTextDocument if (m_autoCompleterCreator) widget->setAutoCompleter(m_autoCompleterCreator()); + widget->finalizeInitialization(); + editor->finalizeInitialization(); + return editor; } diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index 4a9ec08e078..60c64ec73b9 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -129,6 +129,8 @@ public: BaseTextEditor(); ~BaseTextEditor(); + virtual void finalizeInitialization() {} + void setEditorCreator(const BaseTextEditorCreator &creator); void setDocumentCreator(const BaseTextDocumentCreator &creator); void setWidgetCreator(const BaseTextEditorWidgetCreator &creator); @@ -545,6 +547,7 @@ protected: virtual void onRefactorMarkerClicked(const RefactorMarker &) {} void showDefaultContextMenu(QContextMenuEvent *e, Core::Id menuContextId); + virtual void finalizeInitialization() {} public: struct Link @@ -621,10 +624,6 @@ private: friend class RefactorOverlay; }; -typedef std::function SyntaxHighLighterCreator; -typedef std::function IndenterCreator; -typedef std::function AutoCompleterCreator; - class TEXTEDITOR_EXPORT BaseTextEditorFactory : public Core::IEditorFactory { Q_OBJECT @@ -632,6 +631,10 @@ class TEXTEDITOR_EXPORT BaseTextEditorFactory : public Core::IEditorFactory public: BaseTextEditorFactory(QObject *parent = 0); + typedef std::function SyntaxHighLighterCreator; + typedef std::function IndenterCreator; + typedef std::function AutoCompleterCreator; + void setDocumentCreator(const BaseTextDocumentCreator &creator); void setEditorWidgetCreator(const BaseTextEditorWidgetCreator &creator); void setEditorCreator(const BaseTextEditorCreator &creator); @@ -643,10 +646,12 @@ public: void setEditorActionHandlers(Core::Id contextId, uint optionalActions); void setEditorActionHandlers(uint optionalActions); - BaseTextEditor *duplicateTextEditor(BaseTextEditor *); private: + friend class BaseTextEditor; + Core::IEditor *createEditor(); BaseTextEditor *createEditorHelper(const BaseTextDocumentPtr &doc); + BaseTextEditor *duplicateTextEditor(BaseTextEditor *); BaseTextDocumentCreator m_documentCreator; BaseTextEditorWidgetCreator m_widgetCreator;