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 <christian.stenger@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
David Schulz
2022-01-13 09:57:03 +01:00
parent 348652dc26
commit 08e375d6f7
2 changed files with 20 additions and 0 deletions

View File

@@ -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);

View File

@@ -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.