From 82466375b91cf0767f48de1ad17bae1ddd94dcef Mon Sep 17 00:00:00 2001 From: David Schulz Date: Thu, 7 Feb 2019 08:50:16 +0100 Subject: [PATCH] TextEditor: Show info when multiple highlight definitions are available Show an editor info bar when multiple highlight definitions can be found for a single file or mime type. Add a combo box to the info that allows the user to choose between them. Change-Id: I07278d065e19d4e04fba24a6d789c8b6c9f55d60 Reviewed-by: Eike Ziller --- src/plugins/texteditor/highlighter.cpp | 5 +++ src/plugins/texteditor/highlighter.h | 1 + src/plugins/texteditor/texteditor.cpp | 47 +++++++++++++------- src/plugins/texteditor/texteditorconstants.h | 3 +- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/plugins/texteditor/highlighter.cpp b/src/plugins/texteditor/highlighter.cpp index c88a5aacb59..857c0f88cc7 100644 --- a/src/plugins/texteditor/highlighter.cpp +++ b/src/plugins/texteditor/highlighter.cpp @@ -121,6 +121,11 @@ Highlighter::Definition Highlighter::definitionForFileName(const QString &fileNa return highlightRepository()->definitionForFileName(fileName); } +Highlighter::Definition Highlighter::definitionForName(const QString &name) +{ + return highlightRepository()->definitionForName(name); +} + Highlighter::Definitions Highlighter::definitionsForDocument(const TextDocument *document) { const Utils::MimeType mimeType = Utils::mimeTypeForName(document->mimeType()); diff --git a/src/plugins/texteditor/highlighter.h b/src/plugins/texteditor/highlighter.h index cec2b0fb698..776f92faf35 100644 --- a/src/plugins/texteditor/highlighter.h +++ b/src/plugins/texteditor/highlighter.h @@ -46,6 +46,7 @@ public: static Definition definitionForDocument(const TextDocument *document); static Definition definitionForMimeType(const QString &mimeType); static Definition definitionForFileName(const QString &fileName); + static Definition definitionForName(const QString &name); static Definitions definitionsForDocument(const TextDocument *document); static Definitions definitionsForMimeType(const QString &mimeType); diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 6cf1b701366..51a7da5e31c 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -611,7 +611,7 @@ public: void updateCodeFoldingVisible(); void reconfigure(); - void updateSyntaxInfoBar(bool showInfo); + void updateSyntaxInfoBar(const Highlighter::Definitions &definitions, const QString &fileName); void configureGenericHighlighter(const KSyntaxHighlighting::Definition &definition); public: @@ -3274,27 +3274,43 @@ void TextEditorWidgetPrivate::reconfigure() q->configureGenericHighlighter(); } -void TextEditorWidgetPrivate::updateSyntaxInfoBar(bool showInfo) +void TextEditorWidgetPrivate::updateSyntaxInfoBar(const Highlighter::Definitions &definitions, + const QString &fileName) { - Id id(Constants::INFO_SYNTAX_DEFINITION); + Id missing(Constants::INFO_MISSING_SYNTAX_DEFINITION); + Id multiple(Constants::INFO_MULTIPLE_SYNTAX_DEFINITIONS); InfoBar *infoBar = m_document->infoBar(); - if (showInfo) { - InfoBarEntry info(id, - BaseTextEditor::tr( - "A highlight definition was not found for this file. " - "Would you like to update highlight definition files?"), + if (definitions.isEmpty() && infoBar->canInfoBeAdded(missing) + && !TextEditorSettings::highlighterSettings().isIgnoredFilePattern(fileName)) { + InfoBarEntry info(missing, + BaseTextEditor::tr("A highlight definition was not found for this file. " + "Would you like to update highlight definition files?"), InfoBarEntry::GlobalSuppressionEnabled); - info.setCustomButtonInfo(BaseTextEditor::tr("Update Definitions"), [&]() { - m_document->infoBar()->removeInfo(id); + info.setCustomButtonInfo(BaseTextEditor::tr("Update Definitions"), [missing, this]() { + m_document->infoBar()->removeInfo(missing); Highlighter::updateDefinitions([widget = QPointer(q)]() { if (widget) widget->configureGenericHighlighter(); }); }); + + infoBar->removeInfo(multiple); + infoBar->addInfo(info); + } else if (definitions.size() > 1) { + InfoBarEntry info(multiple, + BaseTextEditor::tr("More than one highlight definition was found for this file. " + "Which one should be used to highlight this file?")); + info.setComboInfo(Utils::transform(definitions, &Highlighter::Definition::name), + [this](const QString &definition) { + this->configureGenericHighlighter(Highlighter::definitionForName(definition)); + }); + + infoBar->removeInfo(missing); infoBar->addInfo(info); } else { - infoBar->removeInfo(id); + infoBar->removeInfo(multiple); + infoBar->removeInfo(missing); } } @@ -8514,11 +8530,10 @@ QString TextEditorWidget::textAt(int from, int to) const void TextEditorWidget::configureGenericHighlighter() { - const Highlighter::Definition definition = Highlighter::definitionForDocument(textDocument()); - d->configureGenericHighlighter(definition); - d->updateSyntaxInfoBar(!definition.isValid() - && !TextEditorSettings::highlighterSettings().isIgnoredFilePattern( - textDocument()->filePath().fileName())); + const Highlighter::Definitions definitions = Highlighter::definitionsForDocument(textDocument()); + d->configureGenericHighlighter(definitions.isEmpty() ? Highlighter::Definition() + : definitions.first()); + d->updateSyntaxInfoBar(definitions, textDocument()->filePath().fileName()); } int TextEditorWidget::blockNumberForVisibleRow(int row) const diff --git a/src/plugins/texteditor/texteditorconstants.h b/src/plugins/texteditor/texteditorconstants.h index 22fa68d83bd..949b4934570 100644 --- a/src/plugins/texteditor/texteditorconstants.h +++ b/src/plugins/texteditor/texteditorconstants.h @@ -188,7 +188,8 @@ const char GOTO_NEXT_WORD_WITH_SELECTION[] = "TextEditor.GotoNextWordWithSelecti const char GOTO_PREVIOUS_WORD_CAMEL_CASE_WITH_SELECTION[] = "TextEditor.GotoPreviousWordCamelCaseWithSelection"; const char GOTO_NEXT_WORD_CAMEL_CASE_WITH_SELECTION[] = "TextEditor.GotoNextWordCamelCaseWithSelection"; const char C_TEXTEDITOR_MIMETYPE_TEXT[] = "text/plain"; -const char INFO_SYNTAX_DEFINITION[] = "TextEditor.InfoSyntaxDefinition"; +const char INFO_MISSING_SYNTAX_DEFINITION[] = "TextEditor.InfoSyntaxDefinition"; +const char INFO_MULTIPLE_SYNTAX_DEFINITIONS[] = "TextEditor.InfoMultipleSyntaxDefinitions"; const char TASK_OPEN_FILE[] = "TextEditor.Task.OpenFile"; const char CIRCULAR_PASTE[] = "TextEditor.CircularPaste"; const char SWITCH_UTF8BOM[] = "TextEditor.SwitchUtf8bom";