Commit 4bbf6b63 authored by hjk's avatar hjk

CMakeProjectManager: Use a BaseEditorFactory derived class

Change-Id: Ia5f2b789d82c63552ec7abe5ec22bbc29100a59a
Reviewed-by: default avatarChristian Stenger <christian.stenger@digia.com>
parent 6431ab2c
......@@ -34,14 +34,10 @@
#include "cmakeproject.h"
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/icore.h>
#include <coreplugin/infobar.h>
#include <coreplugin/mimedatabase.h>
#include <extensionsystem/pluginmanager.h>
......@@ -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<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())
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
......@@ -32,8 +32,6 @@
#include <texteditor/basetextdocument.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/codeassist/completionassistprovider.h>
#include <coreplugin/editormanager/ieditorfactory.h>
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
......
......@@ -7316,6 +7316,9 @@ BaseTextEditor *BaseTextEditorFactory::createEditorHelper(const BaseTextDocument
if (m_autoCompleterCreator)
widget->setAutoCompleter(m_autoCompleterCreator());
widget->finalizeInitialization();
editor->finalizeInitialization();
return editor;
}
......
......@@ -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<SyntaxHighlighter *()> SyntaxHighLighterCreator;
typedef std::function<Indenter *()> IndenterCreator;
typedef std::function<AutoCompleter *()> 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<SyntaxHighlighter *()> SyntaxHighLighterCreator;
typedef std::function<Indenter *()> IndenterCreator;
typedef std::function<AutoCompleter *()> 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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment