Commit 5d414215 authored by hjk's avatar hjk

TextEditor: Introduce a BaseTextEditorFactory

The BaseTextEditorFactory will keep a collection of all
relevant creation functions (document, editor, widget,
indenter, syntax highlighter) and will use them to
create or duplicate base text editors in a uniform way.

Change-Id: I255b519e35cf1c28d5e198242f99316951a0235e
Reviewed-by: default avatarChristian Stenger <christian.stenger@digia.com>
parent bb8d0d21
......@@ -49,6 +49,7 @@
#include "circularclipboardassist.h"
#include "highlighterutils.h"
#include "basetexteditor.h"
#include <texteditor/codeassist/codeassistant.h>
#include <texteditor/codeassist/defaultassistinterface.h>
#include <texteditor/generichighlighter/context.h>
......@@ -227,15 +228,16 @@ private:
class BaseTextEditorPrivate
{
public:
BaseTextEditorPrivate()
{}
BaseTextEditorPrivate() {}
BaseTextEditor::EditorCreator m_editorCreator;
BaseTextEditor::DocumentCreator m_documentCreator;
BaseTextEditor::WidgetCreator m_widgetCreator;
BaseTextDocumentCreator m_documentCreator;
BaseTextEditorWidgetCreator m_editorWidgetCreator;
BaseTextEditorCreator m_editorCreator;
CommentDefinition m_commentDefinition;
std::function<CompletionAssistProvider *()> m_completionAssistProvider;
QPointer<BaseTextEditorFactory> m_origin;
};
class BaseTextEditorWidgetPrivate : public QObject
......@@ -6572,19 +6574,19 @@ BaseTextEditor::~BaseTextEditor()
delete d;
}
void BaseTextEditor::setEditorCreator(const BaseTextEditor::EditorCreator &creator)
void BaseTextEditor::setEditorCreator(const BaseTextEditorCreator &creator)
{
d->m_editorCreator = creator;
}
void BaseTextEditor::setDocumentCreator(const BaseTextEditor::DocumentCreator &creator)
void BaseTextEditor::setDocumentCreator(const BaseTextDocumentCreator &creator)
{
d->m_documentCreator = creator;
}
void BaseTextEditor::setWidgetCreator(const BaseTextEditor::WidgetCreator &creator)
void BaseTextEditor::setWidgetCreator(const BaseTextEditorWidgetCreator &creator)
{
d->m_widgetCreator = creator;
d->m_editorWidgetCreator = creator;
}
BaseTextDocument *BaseTextEditor::textDocument()
......@@ -7163,6 +7165,10 @@ void BaseTextEditorWidget::setupAsPlainEditor()
IEditor *BaseTextEditor::duplicate()
{
// Use new standard setup if that's available.
if (d->m_origin)
return d->m_origin->duplicateTextEditor(this);
// Use standard setup if that's available.
if (d->m_editorCreator) {
BaseTextEditor *editor = d->m_editorCreator();
......@@ -7192,8 +7198,9 @@ QWidget *BaseTextEditor::widget() const
BaseTextEditorWidget *BaseTextEditor::ensureWidget() const
{
if (m_widget.isNull()) {
QTC_ASSERT(d->m_widgetCreator, return 0);
BaseTextEditorWidget *widget = d->m_widgetCreator();
QTC_ASSERT(!d->m_origin, return 0); // New style always sets it.
QTC_ASSERT(d->m_editorWidgetCreator, return 0);
BaseTextEditorWidget *widget = d->m_editorWidgetCreator();
auto that = const_cast<BaseTextEditor *>(this);
widget->d->m_editor = that;
that->m_widget = widget;
......@@ -7206,12 +7213,94 @@ BaseTextDocumentPtr BaseTextEditor::ensureDocument()
{
BaseTextEditorWidget *widget = ensureWidget();
if (widget->d->m_document.isNull()) {
QTC_ASSERT(!d->m_origin, return BaseTextDocumentPtr()); // New style always sets it.
QTC_ASSERT(d->m_documentCreator, return BaseTextDocumentPtr());
widget->setTextDocument(BaseTextDocumentPtr(d->m_documentCreator()));
}
return widget->textDocumentPtr();
}
//
// BaseTextEditorFactory
//
BaseTextEditorFactory::BaseTextEditorFactory(QObject *parent)
: IEditorFactory(parent)
{
}
void BaseTextEditorFactory::setDocumentCreator(const BaseTextDocumentCreator &creator)
{
m_documentCreator = creator;
}
void BaseTextEditorFactory::setEditorWidgetCreator(const BaseTextEditorWidgetCreator &creator)
{
m_widgetCreator = creator;
}
void BaseTextEditorFactory::setEditorCreator(const BaseTextEditorCreator &creator)
{
m_editorCreator = creator;
}
void BaseTextEditorFactory::setIndenterCreator(const IndenterCreator &creator)
{
m_indenterCreator = creator;
}
void BaseTextEditorFactory::setSyntaxHighlighterCreator(const SyntaxHighLighterCreator &creator)
{
m_syntaxHighlighterCreator = creator;
}
void BaseTextEditorFactory::setEditorActionHandlers(Id contextId, uint optionalActions)
{
new TextEditorActionHandler(this, contextId, optionalActions);
}
void BaseTextEditorFactory::setEditorActionHandlers(uint optionalActions)
{
new TextEditorActionHandler(this, id(), optionalActions);
}
BaseTextEditor *BaseTextEditorFactory::duplicateTextEditor(BaseTextEditor *other)
{
return createEditorHelper(other->editorWidget()->textDocumentPtr());
}
IEditor *BaseTextEditorFactory::createEditor()
{
BaseTextDocumentPtr doc(m_documentCreator());
if (m_indenterCreator)
doc->setIndenter(m_indenterCreator());
if (m_syntaxHighlighterCreator) {
SyntaxHighlighter *highlighter = m_syntaxHighlighterCreator();
highlighter->setParent(doc.data());
doc->setSyntaxHighlighter(highlighter);
}
return createEditorHelper(doc);
}
BaseTextEditor *BaseTextEditorFactory::createEditorHelper(const BaseTextDocumentPtr &document)
{
BaseTextEditorWidget *widget = m_widgetCreator();
BaseTextEditor *editor = m_editorCreator();
editor->d->m_origin = this;
widget->d->m_editor = editor;
editor->m_widget = widget;
widget->setTextDocument(document);
widget->d->m_codeAssistant.configure(editor);
return editor;
}
} // namespace TextEditor
#include "basetexteditor.moc"
......@@ -34,9 +34,12 @@
#include "codeassist/assistenums.h"
#include "texteditor_global.h"
#include <texteditor/texteditoractionhandler.h>
#include <coreplugin/textdocument.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/editormanager/ieditorfactory.h>
#include <coreplugin/find/ifindsupport.h>
#include <utils/uncommentselection.h>
......@@ -75,6 +78,7 @@ namespace Internal {
}
class BaseTextEditorWidget;
class BaseTextEditorFactory;
class FontSettings;
class BehaviorSettings;
class CompletionSettings;
......@@ -85,6 +89,7 @@ class StorageSettings;
class Indenter;
class AutoCompleter;
class ExtraEncodingSettings;
class BaseTextEditor;
class TEXTEDITOR_EXPORT BlockRange
{
......@@ -104,6 +109,10 @@ private:
int _last;
};
typedef std::function<BaseTextEditor *()> BaseTextEditorCreator;
typedef std::function<BaseTextDocument *()> BaseTextDocumentCreator;
typedef std::function<BaseTextEditorWidget *()> BaseTextEditorWidgetCreator;
class TEXTEDITOR_EXPORT BaseTextEditor : public Core::IEditor
{
Q_OBJECT
......@@ -120,14 +129,9 @@ public:
BaseTextEditor();
~BaseTextEditor();
typedef std::function<BaseTextEditor *()> EditorCreator;
void setEditorCreator(const EditorCreator &creator);
typedef std::function<BaseTextDocument *()> DocumentCreator;
void setDocumentCreator(const DocumentCreator &creator);
typedef std::function<BaseTextEditorWidget *()> WidgetCreator;
void setWidgetCreator(const WidgetCreator &creator);
void setEditorCreator(const BaseTextEditorCreator &creator);
void setDocumentCreator(const BaseTextDocumentCreator &creator);
void setWidgetCreator(const BaseTextEditorWidgetCreator &creator);
void setEditorWidget(BaseTextEditorWidget *editorWidget);
......@@ -216,6 +220,7 @@ signals:
private:
friend class Internal::BaseTextEditorWidgetPrivate;
friend class BaseTextEditorFactory;
friend class BaseTextEditorWidget;
Internal::BaseTextEditorPrivate *d;
};
......@@ -610,11 +615,44 @@ protected slots:
private:
Internal::BaseTextEditorWidgetPrivate *d;
friend class BaseTextEditor;
friend class BaseTextEditorFactory;
friend class Internal::BaseTextEditorWidgetPrivate;
friend class Internal::TextEditorOverlay;
friend class RefactorOverlay;
};
typedef std::function<SyntaxHighlighter *()> SyntaxHighLighterCreator;
typedef std::function<Indenter *()> IndenterCreator;
class TEXTEDITOR_EXPORT BaseTextEditorFactory : public Core::IEditorFactory
{
Q_OBJECT
public:
BaseTextEditorFactory(QObject *parent = 0);
void setDocumentCreator(const BaseTextDocumentCreator &creator);
void setEditorWidgetCreator(const BaseTextEditorWidgetCreator &creator);
void setEditorCreator(const BaseTextEditorCreator &creator);
void setIndenterCreator(const IndenterCreator &creator);
void setSyntaxHighlighterCreator(const SyntaxHighLighterCreator &creator);
void setEditorActionHandlers(Core::Id contextId, uint optionalActions);
void setEditorActionHandlers(uint optionalActions);
BaseTextEditor *duplicateTextEditor(BaseTextEditor *);
private:
Core::IEditor *createEditor();
BaseTextEditor *createEditorHelper(const BaseTextDocumentPtr &doc);
BaseTextDocumentCreator m_documentCreator;
BaseTextEditorWidgetCreator m_widgetCreator;
BaseTextEditorCreator m_editorCreator;
IndenterCreator m_indenterCreator;
SyntaxHighLighterCreator m_syntaxHighlighterCreator;
};
} // namespace TextEditor
Q_DECLARE_METATYPE(TextEditor::BaseTextEditorWidget::Link)
......
......@@ -57,13 +57,13 @@ public:
const VcsBaseEditorParameters *m_parameters;
QObject *m_describeReceiver;
const char *m_describeSlot;
BaseTextEditor::WidgetCreator m_widgetCreator;
BaseTextEditorWidgetCreator m_widgetCreator;
};
} // namespace Internal
VcsEditorFactory::VcsEditorFactory(const VcsBaseEditorParameters *parameters,
const BaseTextEditor::WidgetCreator &creator,
const BaseTextEditorWidgetCreator &creator,
QObject *describeReceiver, const char *describeSlot)
: d(new Internal::BaseVcsEditorFactoryPrivate)
{
......
......@@ -45,7 +45,7 @@ class VCSBASE_EXPORT VcsEditorFactory : public Core::IEditorFactory
public:
VcsEditorFactory(const VcsBaseEditorParameters *type,
const TextEditor::BaseTextEditor::WidgetCreator &creator,
const TextEditor::BaseTextEditorWidgetCreator &creator,
QObject *describeReceiver,
const char *describeSlot);
~VcsEditorFactory();
......
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