diff options
author | Eike Ziller <eike.ziller@theqtcompany.com> | 2015-02-03 09:18:57 +0100 |
---|---|---|
committer | Eike Ziller <eike.ziller@theqtcompany.com> | 2015-02-16 15:45:36 +0000 |
commit | e7fb05e038c19866f85c1cd2e49105b2f81bea0f (patch) | |
tree | b864d13ddb88c238f315db853584da0fb750c44b /src | |
parent | 2aa315d05c5fdff88490dabf291b32989dd9c7f9 (diff) | |
download | qt-creator-e7fb05e038c19866f85c1cd2e49105b2f81bea0f.tar.gz |
TextEditor: Simplify configuring generic highlighter
* configureMimeType -> configureGenericHighlighter, since that is what
it actually does.
* setupAsPlainEditor -> setupGenericHighlighter, since that is what it
actually does
* avoid multiple highlighting definition lookups
* unify code paths by not separately creating generic highlighters
through the factories
Change-Id: I9579ca5736bbf08c01b8e41b63c6b9f36bdc725e
Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/android/androidmanifesteditorwidget.cpp | 3 | ||||
-rw-r--r-- | src/plugins/android/javaeditor.cpp | 2 | ||||
-rw-r--r-- | src/plugins/cmakeprojectmanager/CMakeProjectManager.mimetypes.xml | 2 | ||||
-rw-r--r-- | src/plugins/cmakeprojectmanager/cmakeeditor.cpp | 2 | ||||
-rw-r--r-- | src/plugins/debugger/disassembleragent.cpp | 2 | ||||
-rw-r--r-- | src/plugins/designer/formeditorw.cpp | 2 | ||||
-rw-r--r-- | src/plugins/qnx/bardescriptoreditorwidget.cpp | 5 | ||||
-rw-r--r-- | src/plugins/texteditor/highlighterutils.cpp | 6 | ||||
-rw-r--r-- | src/plugins/texteditor/highlighterutils.h | 3 | ||||
-rw-r--r-- | src/plugins/texteditor/plaintexteditorfactory.cpp | 6 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditor.cpp | 51 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditor.h | 8 |
12 files changed, 37 insertions, 55 deletions
diff --git a/src/plugins/android/androidmanifesteditorwidget.cpp b/src/plugins/android/androidmanifesteditorwidget.cpp index 2055e53581..c8995f2161 100644 --- a/src/plugins/android/androidmanifesteditorwidget.cpp +++ b/src/plugins/android/androidmanifesteditorwidget.cpp @@ -110,7 +110,6 @@ AndroidManifestEditorWidget::AndroidManifestEditorWidget() m_appNameInStringsXml(false) { m_textEditorWidget = new AndroidManifestTextEditorWidget(this); - m_textEditorWidget->setupAsPlainEditor(); initializePage(); @@ -1439,7 +1438,7 @@ AndroidManifestTextEditorWidget::AndroidManifestTextEditorWidget(AndroidManifest : TextEditor::TextEditorWidget(parent) { setTextDocument(TextEditor::TextDocumentPtr(new AndroidManifestDocument(parent))); - setupAsPlainEditor(); textDocument()->setMimeType(QLatin1String(Constants::ANDROID_MANIFEST_MIME_TYPE)); + setupGenericHighlighter(); } diff --git a/src/plugins/android/javaeditor.cpp b/src/plugins/android/javaeditor.cpp index acd347eb2e..b190c338c5 100644 --- a/src/plugins/android/javaeditor.cpp +++ b/src/plugins/android/javaeditor.cpp @@ -92,7 +92,7 @@ JavaEditorFactory::JavaEditorFactory() setDocumentCreator([]() { return new JavaDocument; }); setAutoCompleterCreator([]() { return new JavaAutoCompleter; }); - setGenericSyntaxHighlighter(QLatin1String(Constants::JAVA_MIMETYPE)); + setUseGenericHighlighter(true); setCommentStyle(Utils::CommentDefinition::CppStyle); setEditorActionHandlers(TextEditor::TextEditorActionHandler::UnCommentSelection); setCompletionAssistProvider(new JavaCompletionAssistProvider); diff --git a/src/plugins/cmakeprojectmanager/CMakeProjectManager.mimetypes.xml b/src/plugins/cmakeprojectmanager/CMakeProjectManager.mimetypes.xml index 832a61732e..315cc3c957 100644 --- a/src/plugins/cmakeprojectmanager/CMakeProjectManager.mimetypes.xml +++ b/src/plugins/cmakeprojectmanager/CMakeProjectManager.mimetypes.xml @@ -6,7 +6,7 @@ <glob pattern="*.cmake"/> </mime-type> <mime-type type="text/x-cmake-project"> - <sub-class-of type="text/plain"/> + <sub-class-of type="text/x-cmake"/> <comment>CMake Project file</comment> <glob pattern="CMakeLists.txt"/> </mime-type> diff --git a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp index d28797c306..09907fff52 100644 --- a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp @@ -274,7 +274,7 @@ CMakeEditorFactory::CMakeEditorFactory(CMakeSettingsPage *settingsPage) setEditorCreator([]() { return new CMakeEditor; }); setEditorWidgetCreator([]() { return new CMakeEditorWidget; }); setDocumentCreator([]() { return new CMakeDocument; }); - setGenericSyntaxHighlighterByName(QLatin1String("CMake")); + setUseGenericHighlighter(true); setCommentStyle(Utils::CommentDefinition::HashStyle); setCodeFoldingSupported(true); diff --git a/src/plugins/debugger/disassembleragent.cpp b/src/plugins/debugger/disassembleragent.cpp index f7a96c3ca5..90f5b8dde3 100644 --- a/src/plugins/debugger/disassembleragent.cpp +++ b/src/plugins/debugger/disassembleragent.cpp @@ -241,7 +241,7 @@ void DisassemblerAgentPrivate::configureMimeType() if (mtype) { foreach (IEditor *editor, DocumentModel::editorsForDocument(document)) if (TextEditorWidget *widget = qobject_cast<TextEditorWidget *>(editor->widget())) - widget->configureMimeType(mtype); + widget->configureGenericHighlighter(); } else { qWarning("Assembler mimetype '%s' not found.", qPrintable(mimeType)); } diff --git a/src/plugins/designer/formeditorw.cpp b/src/plugins/designer/formeditorw.cpp index df5d63ca9b..af42604c14 100644 --- a/src/plugins/designer/formeditorw.cpp +++ b/src/plugins/designer/formeditorw.cpp @@ -120,7 +120,6 @@ public: void finalizeInitialization() { - setupAsPlainEditor(); setReadOnly(true); } }; @@ -133,6 +132,7 @@ public: setId(K_DESIGNER_XML_EDITOR_ID); setEditorCreator([]() { return new FormWindowEditor; }); setEditorWidgetCreator([]() { return new Internal::DesignerXmlEditorWidget; }); + setUseGenericHighlighter(true); setDuplicatedSupported(false); } diff --git a/src/plugins/qnx/bardescriptoreditorwidget.cpp b/src/plugins/qnx/bardescriptoreditorwidget.cpp index e7b7fc43a8..bf8fa3a176 100644 --- a/src/plugins/qnx/bardescriptoreditorwidget.cpp +++ b/src/plugins/qnx/bardescriptoreditorwidget.cpp @@ -172,13 +172,12 @@ void BarDescriptorEditorWidget::initSourcePage() TextDocumentPtr doc(new TextDocument); doc->setId(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); // FIXME: This looks odd. doc->setIndenter(new NormalIndenter); + doc->setMimeType(QLatin1String(Constants::QNX_BAR_DESCRIPTOR_MIME_TYPE)); m_xmlSourceWidget = new TextEditorWidget(this); m_xmlSourceWidget->setTextDocument(doc); - m_xmlSourceWidget->setupAsPlainEditor(); + m_xmlSourceWidget->setupGenericHighlighter(); addWidget(m_xmlSourceWidget); - - m_xmlSourceWidget->configureMimeType(QLatin1String(Constants::QNX_BAR_DESCRIPTOR_MIME_TYPE)); } void BarDescriptorEditorWidget::initPanelSize(ProjectExplorer::PanelsWidget *panelsWidget) diff --git a/src/plugins/texteditor/highlighterutils.cpp b/src/plugins/texteditor/highlighterutils.cpp index fec5ec798f..0e84c347e7 100644 --- a/src/plugins/texteditor/highlighterutils.cpp +++ b/src/plugins/texteditor/highlighterutils.cpp @@ -53,7 +53,8 @@ QString TextEditor::findDefinitionId(const Core::MimeType &mimeType, return definitionId; } -void TextEditor::setMimeTypeForHighlighter(Highlighter *highlighter, const Core::MimeType &mimeType) +void TextEditor::setMimeTypeForHighlighter(Highlighter *highlighter, const Core::MimeType &mimeType, + QString *foundDefinitionId) { const QString type = mimeType.type(); QString definitionId = Manager::instance()->definitionIdByMimeType(type); @@ -66,6 +67,9 @@ void TextEditor::setMimeTypeForHighlighter(Highlighter *highlighter, const Core: if (!definition.isNull() && definition->isValid()) highlighter->setDefaultContext(definition->initialContext()); } + + if (foundDefinitionId) + *foundDefinitionId = definitionId; } SyntaxHighlighter *TextEditor::createGenericSyntaxHighlighter(const Core::MimeType &mimeType) diff --git a/src/plugins/texteditor/highlighterutils.h b/src/plugins/texteditor/highlighterutils.h index 88bb72f1e9..b0e1b70b69 100644 --- a/src/plugins/texteditor/highlighterutils.h +++ b/src/plugins/texteditor/highlighterutils.h @@ -45,7 +45,8 @@ namespace TextEditor { class Highlighter; class SyntaxHighlighter; -void setMimeTypeForHighlighter(Highlighter *highlighter, const Core::MimeType &mimeType); +void setMimeTypeForHighlighter(Highlighter *highlighter, const Core::MimeType &mimeType, + QString *foundDefinitionId = 0); QString findDefinitionId(const Core::MimeType &mimeType, bool considerParents); TEXTEDITOR_EXPORT SyntaxHighlighter *createGenericSyntaxHighlighter(const Core::MimeType &mimeType); diff --git a/src/plugins/texteditor/plaintexteditorfactory.cpp b/src/plugins/texteditor/plaintexteditorfactory.cpp index 055922447e..a06234c53b 100644 --- a/src/plugins/texteditor/plaintexteditorfactory.cpp +++ b/src/plugins/texteditor/plaintexteditorfactory.cpp @@ -51,7 +51,10 @@ class PlainTextEditorWidget : public TextEditorWidget { public: PlainTextEditorWidget() {} - void finalizeInitialization() { setupAsPlainEditor(); } + void finalizeInitialization() + { + textDocument()->setMimeType(QLatin1String(Constants::C_TEXTEDITOR_MIMETYPE_TEXT)); + } }; PlainTextEditorFactory::PlainTextEditorFactory() @@ -65,6 +68,7 @@ PlainTextEditorFactory::PlainTextEditorFactory() setDocumentCreator([]() { return new TextDocument(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); }); setEditorWidgetCreator([]() { return new PlainTextEditorWidget; }); setIndenterCreator([]() { return new NormalIndenter; }); + setUseGenericHighlighter(true); setEditorActionHandlers(TextEditorActionHandler::Format | TextEditorActionHandler::UnCommentSelection | diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 608a84b59a..c9c824f19a 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -2735,7 +2735,8 @@ void TextEditorWidgetPrivate::updateCodeFoldingVisible() void TextEditorWidgetPrivate::reconfigure() { - q->configureMimeType(MimeDatabase::findByFile(m_document->filePath().toString())); + m_document->setMimeType(MimeDatabase::findByFile(m_document->filePath().toString()).type()); + q->configureGenericHighlighter(); } bool TextEditorWidget::codeFoldingVisible() const @@ -7163,12 +7164,7 @@ QString TextEditorWidget::textAt(int from, int to) const return textDocument()->textAt(from, to); } -void TextEditorWidget::configureMimeType(const QString &mimeType) -{ - configureMimeType(MimeDatabase::findByType(mimeType)); -} - -void TextEditorWidget::configureMimeType(const MimeType &mimeType) +void TextEditorWidget::configureGenericHighlighter() { Highlighter *highlighter = new Highlighter(); highlighter->setTabSettings(textDocument()->tabSettings()); @@ -7176,16 +7172,13 @@ void TextEditorWidget::configureMimeType(const MimeType &mimeType) setCodeFoldingSupported(false); + const QString type = textDocument()->mimeType(); + const MimeType mimeType = MimeDatabase::findByType(type); if (!mimeType.isNull()) { d->m_isMissingSyntaxDefinition = true; - setMimeTypeForHighlighter(highlighter, mimeType); - const QString &type = mimeType.type(); - textDocument()->setMimeType(type); - - QString definitionId = Manager::instance()->definitionIdByMimeType(type); - if (definitionId.isEmpty()) - definitionId = findDefinitionId(mimeType, true); + QString definitionId; + setMimeTypeForHighlighter(highlighter, mimeType, &definitionId); if (!definitionId.isEmpty()) { d->m_isMissingSyntaxDefinition = false; @@ -7217,13 +7210,11 @@ bool TextEditorWidget::isMissingSyntaxDefinition() const } // The remnants of PlainTextEditor. -void TextEditorWidget::setupAsPlainEditor() +void TextEditorWidget::setupGenericHighlighter() { setMarksVisible(true); setLineSeparatorsAllowed(true); - textDocument()->setMimeType(QLatin1String(Constants::C_TEXTEDITOR_MIMETYPE_TEXT)); - connect(textDocument(), &IDocument::filePathChanged, d, &TextEditorWidgetPrivate::reconfigure); @@ -7295,6 +7286,7 @@ public: m_editorCreator([]() { return new BaseTextEditor; }), m_commentStyle(CommentDefinition::NoStyle), m_completionAssistProvider(0), + m_useGenericHighlighter(false), m_duplicatedSupported(true), m_codeFoldingSupported(false), m_paranthesesMatchinEnabled(false), @@ -7320,6 +7312,7 @@ public: CommentDefinition::Style m_commentStyle; QList<BaseHoverHandler *> m_hoverHandlers; // owned CompletionAssistProvider * m_completionAssistProvider; // owned + bool m_useGenericHighlighter; bool m_duplicatedSupported; bool m_codeFoldingSupported; bool m_paranthesesMatchinEnabled; @@ -7364,27 +7357,9 @@ void TextEditorFactory::setSyntaxHighlighterCreator(const SyntaxHighLighterCreat d->m_syntaxHighlighterCreator = creator; } -void TextEditorFactory::setGenericSyntaxHighlighter(const QString &mimeType) -{ - d->m_syntaxHighlighterCreator = [this, mimeType]() -> SyntaxHighlighter * { - Highlighter *highlighter = new Highlighter; - setMimeTypeForHighlighter(highlighter, MimeDatabase::findByType(mimeType)); - return highlighter; - }; -} - -void TextEditorFactory::setGenericSyntaxHighlighterByName(const QString &name) +void TextEditorFactory::setUseGenericHighlighter(bool enabled) { - d->m_syntaxHighlighterCreator = [this, name]() -> SyntaxHighlighter * { - TextEditor::Highlighter *highlighter = new TextEditor::Highlighter(); - QString definitionId = Manager::instance()->definitionIdByName(name); - if (!definitionId.isEmpty()) { - const QSharedPointer<HighlightDefinition> &definition = Manager::instance()->definition(definitionId); - if (!definition.isNull() && definition->isValid()) - highlighter->setDefaultContext(definition->initialContext()); - } - return highlighter; - }; + d->m_useGenericHighlighter = enabled; } void TextEditorFactory::setAutoCompleterCreator(const AutoCompleterCreator &creator) @@ -7479,6 +7454,8 @@ BaseTextEditor *TextEditorFactoryPrivate::createEditorHelper(const TextDocumentP QObject::connect(widget, &TextEditorWidget::activateEditor, [editor]() { EditorManager::activateEditor(editor); }); + if (m_useGenericHighlighter) + widget->setupGenericHighlighter(); widget->finalizeInitialization(); editor->finalizeInitialization(); diff --git a/src/plugins/texteditor/texteditor.h b/src/plugins/texteditor/texteditor.h index bdc0e74c41..6634cd2b23 100644 --- a/src/plugins/texteditor/texteditor.h +++ b/src/plugins/texteditor/texteditor.h @@ -468,8 +468,7 @@ public: /// Abort code assistant if it is running. void abortAssist(); - void configureMimeType(const QString &mimeType); - void configureMimeType(const Core::MimeType &mimeType); + void configureGenericHighlighter(); Q_INVOKABLE void inSnippetMode(bool *active); // Used by FakeVim. @@ -559,7 +558,7 @@ public: QString selectedText() const; - void setupAsPlainEditor(); + void setupGenericHighlighter(); void setupFallBackEditor(Core::Id id); void remove(int length); @@ -664,8 +663,7 @@ public: void setEditorCreator(const EditorCreator &creator); void setIndenterCreator(const IndenterCreator &creator); void setSyntaxHighlighterCreator(const SyntaxHighLighterCreator &creator); - void setGenericSyntaxHighlighter(const QString &mimeType); - void setGenericSyntaxHighlighterByName(const QString &name); + void setUseGenericHighlighter(bool enabled); void setAutoCompleterCreator(const AutoCompleterCreator &creator); void setEditorActionHandlers(Core::Id contextId, uint optionalActions); |