From 08e375d6f75e6636b07dfaf97d3ae2c656995f75 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Thu, 13 Jan 2022 09:57:03 +0100 Subject: [PATCH] TextEditor: add highlighter configure based on mimetype The default implementation configures the generic highlighter based on the documents file name and content. Add a way to force the highlighter to use a highlight definition for a specific mime type. Change-Id: I1c6fca1be9bcb5c4276b5aa2dbb724680fe98ddc Reviewed-by: Christian Stenger Reviewed-by: --- src/plugins/texteditor/texteditor.cpp | 16 ++++++++++++++++ src/plugins/texteditor/texteditor.h | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 731c5ba779e..11caca4d573 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -642,6 +642,7 @@ public: void reconfigure(); void updateSyntaxInfoBar(const Highlighter::Definitions &definitions, const QString &fileName); + void removeSyntaxInfoBar(); void configureGenericHighlighter(const KSyntaxHighlighting::Definition &definition); void rememberCurrentSyntaxDefinition(); void openLinkUnderCursor(bool openInNextSplit); @@ -3263,6 +3264,13 @@ void TextEditorWidgetPrivate::updateSyntaxInfoBar(const Highlighter::Definitions } } +void TextEditorWidgetPrivate::removeSyntaxInfoBar() +{ + InfoBar *infoBar = m_document->infoBar(); + infoBar->removeInfo(Constants::INFO_MISSING_SYNTAX_DEFINITION); + infoBar->removeInfo(Constants::INFO_MULTIPLE_SYNTAX_DEFINITIONS); +} + void TextEditorWidgetPrivate::configureGenericHighlighter( const KSyntaxHighlighting::Definition &definition) { @@ -8163,6 +8171,14 @@ void TextEditorWidget::configureGenericHighlighter() d->updateSyntaxInfoBar(definitions, textDocument()->filePath().fileName()); } +void TextEditorWidget::configureGenericHighlighter(const Utils::MimeType &mimeType) +{ + Highlighter::Definitions definitions = Highlighter::definitionsForMimeType(mimeType.name()); + d->configureGenericHighlighter(definitions.isEmpty() ? Highlighter::Definition() + : definitions.first()); + d->removeSyntaxInfoBar(); +} + int TextEditorWidget::blockNumberForVisibleRow(int row) const { QTextBlock block = blockForVisibleRow(row); diff --git a/src/plugins/texteditor/texteditor.h b/src/plugins/texteditor/texteditor.h index 7daed6de22c..71759cb17b2 100644 --- a/src/plugins/texteditor/texteditor.h +++ b/src/plugins/texteditor/texteditor.h @@ -457,7 +457,11 @@ public: /// Abort code assistant if it is running. void abortAssist(); + /// Overwrite the current highlighter with a new generic highlighter based on the mimetype of + /// the current document void configureGenericHighlighter(); + /// Overwrite the current highlighter with a new generic highlighter based on the given mimetype + void configureGenericHighlighter(const Utils::MimeType &mimeType); Q_INVOKABLE void inSnippetMode(bool *active); // Used by FakeVim.