diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 1c7e9d346fc..0f378ea3447 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -533,6 +533,27 @@ BaseTextEditorWidgetPrivate::BaseTextEditorWidgetPrivate(BaseTextEditorWidget *p using namespace Internal; +/*! + * Test if syntax highlighter is available (or unneeded) for \a widget. + * If not found, show a warning with a link to the relevant settings page. + */ +static void updateEditorInfoBar(BaseTextEditorWidget *widget) +{ + Id infoSyntaxDefinition(Constants::INFO_SYNTAX_DEFINITION); + InfoBar *infoBar = widget->textDocument()->infoBar(); + if (!widget->isMissingSyntaxDefinition()) { + infoBar->removeInfo(infoSyntaxDefinition); + } else if (infoBar->canInfoBeAdded(infoSyntaxDefinition)) { + InfoBarEntry info(infoSyntaxDefinition, + BaseTextEditor::tr("A highlight definition was not found for this file. " + "Would you like to try to find one?"), + InfoBarEntry::GlobalSuppressionEnabled); + info.setCustomButtonInfo(BaseTextEditor::tr("Show Highlighter Options..."), + widget, SLOT(acceptMissingSyntaxDefinitionInfo())); + infoBar->addInfo(info); + } +} + QString BaseTextEditorWidget::plainTextFromSelection(const QTextCursor &cursor) const { // Copy the selected text as plain text @@ -6512,11 +6533,9 @@ QColor BaseTextEditorWidget::replacementPenColor(int blockNumber) const BaseTextEditor *BaseTextEditorWidget::createEditor() { + QTC_CHECK("should not happen anymore" && false); auto editor = new BaseTextEditor; editor->setEditorWidget(this); - editor->setContext(Core::Context(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID, - TextEditor::Constants::C_TEXTEDITOR)); - editor->setDuplicateSupported(true); return editor; } @@ -6555,6 +6574,7 @@ BaseTextEditor::BaseTextEditor() { d->m_completionAssistProvider = [] () -> CompletionAssistProvider * { return 0; }; addContext(TextEditor::Constants::C_TEXTEDITOR); + setDuplicateSupported(true); } void BaseTextEditor::setEditorWidget(BaseTextEditorWidget *widget) @@ -7126,7 +7146,7 @@ void BaseTextEditorWidget::configureMimeType(const MimeType &mimeType) textDocument()->setFontSettings(TextEditorSettings::fontSettings()); - emit configured(editor()); + updateEditorInfoBar(this); } bool BaseTextEditorWidget::isMissingSyntaxDefinition() const @@ -7141,26 +7161,27 @@ void BaseTextEditorWidget::acceptMissingSyntaxDefinitionInfo() this); } -void BaseTextEditorWidget::configureMimeType() -{ - MimeType mimeType; - if (textDocument()) - mimeType = MimeDatabase::findByFile(textDocument()->filePath()); - configureMimeType(mimeType); -} - // The remnants of PlainTextEditor. void BaseTextEditorWidget::setupAsPlainEditor() { setRevisionsVisible(true); setMarksVisible(true); setLineSeparatorsAllowed(true); + setLineSeparatorsAllowed(true); textDocument()->setMimeType(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT)); - connect(textDocument(), SIGNAL(filePathChanged(QString,QString)), - this, SLOT(configureMimeType())); - connect(Manager::instance(), SIGNAL(mimeTypesRegistered()), this, SLOT(configureMimeType())); + auto reconf = [this]() { + MimeType mimeType; + if (textDocument()) + mimeType = MimeDatabase::findByFile(textDocument()->filePath()); + configureMimeType(mimeType); + }; + + connect(textDocument(), &IDocument::filePathChanged, reconf); + connect(Manager::instance(), &Manager::mimeTypesRegistered, reconf); + + updateEditorInfoBar(this); } IEditor *BaseTextEditor::duplicate() @@ -7169,7 +7190,7 @@ IEditor *BaseTextEditor::duplicate() if (d->m_origin) return d->m_origin->duplicateTextEditor(this); - // Use standard setup if that's available. + // Use old setup if that's available. if (d->m_editorCreator) { BaseTextEditor *editor = d->m_editorCreator(); BaseTextEditorWidget *widget = editor->ensureWidget(); @@ -7178,16 +7199,9 @@ IEditor *BaseTextEditor::duplicate() return editor; } - // That's a really plain text editor. - auto newWidget = new BaseTextEditorWidget; - newWidget->setTextDocument(editorWidget()->textDocumentPtr()); - newWidget->setupAsPlainEditor(); - auto editor = newWidget->editor(); - editor->addContext(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); - editor->setDuplicateSupported(true); - return editor; - // If neither is sufficient, you need to implement 'YourEditor::duplicate'. + QTC_CHECK(false); + return 0; } QWidget *BaseTextEditor::widget() const @@ -7228,6 +7242,7 @@ BaseTextDocumentPtr BaseTextEditor::ensureDocument() BaseTextEditorFactory::BaseTextEditorFactory(QObject *parent) : IEditorFactory(parent) { + m_editorCreator = []() { return new BaseTextEditor; }; m_widgetCreator = []() { return new BaseTextEditorWidget; }; } diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index 60c64ec73b9..a52d3ce1af8 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -504,8 +504,6 @@ signals: void requestZoomReset(); void requestBlockUpdate(const QTextBlock &); - void configured(Core::IEditor *editor); - protected: bool event(QEvent *e); void inputMethodEvent(QInputMethodEvent *e); @@ -612,7 +610,6 @@ protected slots: virtual void slotCursorPositionChanged(); // Used in VcsBase virtual void slotCodeStyleSettingsChanged(const QVariant &); // Used in CppEditor - void configureMimeType(); void doFoo(); private: diff --git a/src/plugins/texteditor/plaintexteditorfactory.cpp b/src/plugins/texteditor/plaintexteditorfactory.cpp index 40d6e5daa5f..33e8de572e7 100644 --- a/src/plugins/texteditor/plaintexteditorfactory.cpp +++ b/src/plugins/texteditor/plaintexteditorfactory.cpp @@ -38,64 +38,36 @@ #include #include +#include #include #include -using namespace TextEditor; -using namespace TextEditor::Internal; +namespace TextEditor { +namespace Internal { -PlainTextEditorFactory::PlainTextEditorFactory(QObject *parent) - : Core::IEditorFactory(parent) +class PlainTextEditorWidget : public BaseTextEditorWidget +{ +public: + PlainTextEditorWidget() {} + void finalizeInitialization() { setupAsPlainEditor(); } +}; + +PlainTextEditorFactory::PlainTextEditorFactory() { setId(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); setDisplayName(qApp->translate("OpenWith::Editors", Core::Constants::K_DEFAULT_TEXT_EDITOR_DISPLAY_NAME)); addMimeType(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT)); - new TextEditorActionHandler(this, - Core::Constants::K_DEFAULT_TEXT_EDITOR_ID, + setDocumentCreator([]() { return new BaseTextDocument(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); }); + setEditorWidgetCreator([]() { return new PlainTextEditorWidget; }); + setIndenterCreator([]() { return new NormalIndenter; }); + + setEditorActionHandlers(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID, TextEditorActionHandler::Format | TextEditorActionHandler::UnCommentSelection | TextEditorActionHandler::UnCollapseAll); } -Core::IEditor *PlainTextEditorFactory::createEditor() -{ - BaseTextDocumentPtr doc(new BaseTextDocument(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID)); - doc->setIndenter(new NormalIndenter); - auto widget = new BaseTextEditorWidget; - widget->setTextDocument(doc); - widget->setupAsPlainEditor(); - connect(widget, &BaseTextEditorWidget::configured, - this, &PlainTextEditorFactory::updateEditorInfoBar); - updateEditorInfoBar(widget->editor()); - return widget->editor(); -} - -/*! - * Test if syntax highlighter is available (or unneeded) for \a editor. - * If not found, show a warning with a link to the relevant settings page. - */ -void PlainTextEditorFactory::updateEditorInfoBar(Core::IEditor *editor) -{ - BaseTextEditor *textEditor = qobject_cast(editor); - if (textEditor) { - Core::IDocument *file = editor->document(); - if (!file) - return; - BaseTextEditorWidget *widget = textEditor->editorWidget(); - Core::Id infoSyntaxDefinition(Constants::INFO_SYNTAX_DEFINITION); - Core::InfoBar *infoBar = file->infoBar(); - if (!widget->isMissingSyntaxDefinition()) { - infoBar->removeInfo(infoSyntaxDefinition); - } else if (infoBar->canInfoBeAdded(infoSyntaxDefinition)) { - Core::InfoBarEntry info(infoSyntaxDefinition, - tr("A highlight definition was not found for this file. " - "Would you like to try to find one?"), - Core::InfoBarEntry::GlobalSuppressionEnabled); - info.setCustomButtonInfo(tr("Show Highlighter Options..."), - widget, SLOT(acceptMissingSyntaxDefinitionInfo())); - infoBar->addInfo(info); - } - } -} +} // namespace Internal +} // namespace TextEditor diff --git a/src/plugins/texteditor/plaintexteditorfactory.h b/src/plugins/texteditor/plaintexteditorfactory.h index 8633fc89498..3ade4c07d56 100644 --- a/src/plugins/texteditor/plaintexteditorfactory.h +++ b/src/plugins/texteditor/plaintexteditorfactory.h @@ -30,25 +30,17 @@ #ifndef PLAINTEXTEDITORFACTORY_H #define PLAINTEXTEDITORFACTORY_H -#include - -#include +#include namespace TextEditor { namespace Internal { -class PlainTextEditorFactory : public Core::IEditorFactory +class PlainTextEditorFactory : public TextEditor::BaseTextEditorFactory { Q_OBJECT public: - PlainTextEditorFactory(QObject *parent = 0); - - using Core::IEditorFactory::addMimeType; - Core::IEditor *createEditor(); - -private slots: - void updateEditorInfoBar(Core::IEditor *editor); + PlainTextEditorFactory(); }; } // namespace Internal