CMakeProjectManager: Use a BaseEditorFactory derived class

Change-Id: Ia5f2b789d82c63552ec7abe5ec22bbc29100a59a
Reviewed-by: Christian Stenger <christian.stenger@digia.com>
This commit is contained in:
hjk
2014-08-22 16:38:38 +02:00
parent 6431ab2c79
commit 4bbf6b63f1
4 changed files with 59 additions and 71 deletions

View File

@@ -34,14 +34,10 @@
#include "cmakeproject.h" #include "cmakeproject.h"
#include <coreplugin/actionmanager/actioncontainer.h> #include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/icore.h>
#include <coreplugin/infobar.h> #include <coreplugin/infobar.h>
#include <coreplugin/mimedatabase.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
@@ -59,7 +55,6 @@
using namespace Core; using namespace Core;
using namespace TextEditor; using namespace TextEditor;
using namespace CMakeProjectManager::Constants;
namespace CMakeProjectManager { namespace CMakeProjectManager {
namespace Internal { namespace Internal {
@@ -74,29 +69,24 @@ CMakeEditor::CMakeEditor()
setDuplicateSupported(true); setDuplicateSupported(true);
setCommentStyle(Utils::CommentDefinition::HashStyle); setCommentStyle(Utils::CommentDefinition::HashStyle);
setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<CMakeFileCompletionAssistProvider>()); setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<CMakeFileCompletionAssistProvider>());
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()) connect(document(), &IDocument::changed, [this]() {
return; BaseTextDocument *document = textDocument();
InfoBar *infoBar = document()->infoBar(); if (!document->isModified())
Id infoRunCmake("CMakeEditor.RunCMake"); return;
if (!infoBar->canInfoBeAdded(infoRunCmake)) InfoBar *infoBar = document->infoBar();
return; Id infoRunCmake("CMakeEditor.RunCMake");
InfoBarEntry info(infoRunCmake, if (!infoBar->canInfoBeAdded(infoRunCmake))
tr("Changes to cmake files are shown in the project tree after building."), return;
InfoBarEntry::GlobalSuppressionEnabled); InfoBarEntry info(infoRunCmake,
info.setCustomButtonInfo(tr("Build now"), this, SLOT(build())); tr("Changes to cmake files are shown in the project tree after building."),
infoBar->addInfo(info); InfoBarEntry::GlobalSuppressionEnabled);
info.setCustomButtonInfo(tr("Build now"), this, SLOT(build()));
infoBar->addInfo(info);
});
} }
void CMakeEditor::build() void CMakeEditor::build()
@@ -158,6 +148,18 @@ QString CMakeEditor::contextHelpId() const
// CMakeEditorWidget // 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() CMakeEditorWidget::CMakeEditorWidget()
{ {
setCodeFoldingSupported(true); setCodeFoldingSupported(true);
@@ -251,13 +253,19 @@ CMakeEditorWidget::Link CMakeEditorWidget::findLinkAt(const QTextCursor &cursor,
// CMakeDocument // CMakeDocument
// //
class CMakeDocument : public BaseTextDocument
{
public:
CMakeDocument();
QString defaultPath() const;
QString suggestedFileName() const;
};
CMakeDocument::CMakeDocument() CMakeDocument::CMakeDocument()
{ {
setId(Constants::CMAKE_EDITOR_ID); setId(Constants::CMAKE_EDITOR_ID);
setMimeType(QLatin1String(Constants::CMAKEMIMETYPE)); setMimeType(QLatin1String(Constants::CMAKEMIMETYPE));
MimeType mimeType = MimeDatabase::findByType(QLatin1String(Constants::CMAKEMIMETYPE));
setSyntaxHighlighter(TextEditor::createGenericSyntaxHighlighter(mimeType));
} }
QString CMakeDocument::defaultPath() const QString CMakeDocument::defaultPath() const
@@ -283,20 +291,20 @@ CMakeEditorFactory::CMakeEditorFactory()
addMimeType(Constants::CMAKEMIMETYPE); addMimeType(Constants::CMAKEMIMETYPE);
addMimeType(Constants::CMAKEPROJECTMIMETYPE); 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::UnCommentSelection
| TextEditorActionHandler::JumpToFileUnderCursor); | TextEditorActionHandler::JumpToFileUnderCursor);
ActionContainer *contextMenu = ActionManager::createMenu(Constants::M_CONTEXT); ActionContainer *contextMenu = ActionManager::createMenu(Constants::M_CONTEXT);
contextMenu->addAction(ActionManager::command(TextEditor::Constants::JUMP_TO_FILE_UNDER_CURSOR)); 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)); contextMenu->addAction(ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION));
} }
IEditor *CMakeEditorFactory::createEditor()
{
return new CMakeEditor;
}
} // namespace Internal } // namespace Internal
} // namespace CMakeProjectManager } // namespace CMakeProjectManager

View File

@@ -32,8 +32,6 @@
#include <texteditor/basetextdocument.h> #include <texteditor/basetextdocument.h>
#include <texteditor/basetexteditor.h> #include <texteditor/basetexteditor.h>
#include <texteditor/codeassist/completionassistprovider.h>
#include <coreplugin/editormanager/ieditorfactory.h>
namespace CMakeProjectManager { namespace CMakeProjectManager {
namespace Internal { namespace Internal {
@@ -47,47 +45,21 @@ class CMakeEditor : public TextEditor::BaseTextEditor
public: public:
CMakeEditor(); CMakeEditor();
void finalizeInitialization();
QString contextHelpId() const; QString contextHelpId() const;
friend class CMakeEditorWidget; friend class CMakeEditorWidget;
private slots: public slots:
void markAsChanged();
void build(); void build();
}; };
class CMakeEditorWidget : public TextEditor::BaseTextEditorWidget class CMakeEditorFactory : public TextEditor::BaseTextEditorFactory
{
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
{ {
Q_OBJECT Q_OBJECT
public: public:
CMakeEditorFactory(); CMakeEditorFactory();
Core::IEditor *createEditor();
}; };
} // namespace Internal } // namespace Internal

View File

@@ -7316,6 +7316,9 @@ BaseTextEditor *BaseTextEditorFactory::createEditorHelper(const BaseTextDocument
if (m_autoCompleterCreator) if (m_autoCompleterCreator)
widget->setAutoCompleter(m_autoCompleterCreator()); widget->setAutoCompleter(m_autoCompleterCreator());
widget->finalizeInitialization();
editor->finalizeInitialization();
return editor; return editor;
} }

View File

@@ -129,6 +129,8 @@ public:
BaseTextEditor(); BaseTextEditor();
~BaseTextEditor(); ~BaseTextEditor();
virtual void finalizeInitialization() {}
void setEditorCreator(const BaseTextEditorCreator &creator); void setEditorCreator(const BaseTextEditorCreator &creator);
void setDocumentCreator(const BaseTextDocumentCreator &creator); void setDocumentCreator(const BaseTextDocumentCreator &creator);
void setWidgetCreator(const BaseTextEditorWidgetCreator &creator); void setWidgetCreator(const BaseTextEditorWidgetCreator &creator);
@@ -545,6 +547,7 @@ protected:
virtual void onRefactorMarkerClicked(const RefactorMarker &) {} virtual void onRefactorMarkerClicked(const RefactorMarker &) {}
void showDefaultContextMenu(QContextMenuEvent *e, Core::Id menuContextId); void showDefaultContextMenu(QContextMenuEvent *e, Core::Id menuContextId);
virtual void finalizeInitialization() {}
public: public:
struct Link struct Link
@@ -621,10 +624,6 @@ private:
friend class RefactorOverlay; friend class RefactorOverlay;
}; };
typedef std::function<SyntaxHighlighter *()> SyntaxHighLighterCreator;
typedef std::function<Indenter *()> IndenterCreator;
typedef std::function<AutoCompleter *()> AutoCompleterCreator;
class TEXTEDITOR_EXPORT BaseTextEditorFactory : public Core::IEditorFactory class TEXTEDITOR_EXPORT BaseTextEditorFactory : public Core::IEditorFactory
{ {
Q_OBJECT Q_OBJECT
@@ -632,6 +631,10 @@ class TEXTEDITOR_EXPORT BaseTextEditorFactory : public Core::IEditorFactory
public: public:
BaseTextEditorFactory(QObject *parent = 0); BaseTextEditorFactory(QObject *parent = 0);
typedef std::function<SyntaxHighlighter *()> SyntaxHighLighterCreator;
typedef std::function<Indenter *()> IndenterCreator;
typedef std::function<AutoCompleter *()> AutoCompleterCreator;
void setDocumentCreator(const BaseTextDocumentCreator &creator); void setDocumentCreator(const BaseTextDocumentCreator &creator);
void setEditorWidgetCreator(const BaseTextEditorWidgetCreator &creator); void setEditorWidgetCreator(const BaseTextEditorWidgetCreator &creator);
void setEditorCreator(const BaseTextEditorCreator &creator); void setEditorCreator(const BaseTextEditorCreator &creator);
@@ -643,10 +646,12 @@ public:
void setEditorActionHandlers(Core::Id contextId, uint optionalActions); void setEditorActionHandlers(Core::Id contextId, uint optionalActions);
void setEditorActionHandlers(uint optionalActions); void setEditorActionHandlers(uint optionalActions);
BaseTextEditor *duplicateTextEditor(BaseTextEditor *);
private: private:
friend class BaseTextEditor;
Core::IEditor *createEditor(); Core::IEditor *createEditor();
BaseTextEditor *createEditorHelper(const BaseTextDocumentPtr &doc); BaseTextEditor *createEditorHelper(const BaseTextDocumentPtr &doc);
BaseTextEditor *duplicateTextEditor(BaseTextEditor *);
BaseTextDocumentCreator m_documentCreator; BaseTextDocumentCreator m_documentCreator;
BaseTextEditorWidgetCreator m_widgetCreator; BaseTextEditorWidgetCreator m_widgetCreator;