From ea727e6c821de16964eac715c2db0f7464d56f36 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Thu, 13 Jun 2019 12:21:30 +0200 Subject: [PATCH] TextEditor: add additional formats setter to semantic highlighter The old setter incremental applies formats to the semantic highlighter, in contrast to the new setter, which removes all old additional formats and then applies all passed formats. Change-Id: If8569c180d8eaef7aee99509c84a03ba362c9e4a Reviewed-by: Nikolai Kosjar --- .../texteditor/semantichighlighter.cpp | 26 +++++++++++++++++++ src/plugins/texteditor/semantichighlighter.h | 9 +++++++ 2 files changed, 35 insertions(+) diff --git a/src/plugins/texteditor/semantichighlighter.cpp b/src/plugins/texteditor/semantichighlighter.cpp index 4a9e9b9dd4c..be0e66065ae 100644 --- a/src/plugins/texteditor/semantichighlighter.cpp +++ b/src/plugins/texteditor/semantichighlighter.cpp @@ -117,6 +117,32 @@ void SemanticHighlighter::incrementalApplyExtraAdditionalFormats( } } +void SemanticHighlighter::setExtraAdditionalFormats(SyntaxHighlighter *highlighter, + const QList &results, + const QHash &kindToFormat) +{ + highlighter->clearAllExtraFormats(); + + QTextDocument *doc = highlighter->document(); + QTC_ASSERT(doc, return ); + + QVector> ranges(doc->blockCount()); + + for (auto result : results) { + const QTextLayout::FormatRange formatRange = rangeForResult(result, kindToFormat); + if (formatRange.format.isValid()) + ranges[int(result.line) - 1].append(formatRange); + } + + for (int blockNumber = 0; blockNumber < ranges.count(); ++blockNumber) { + if (!ranges[blockNumber].isEmpty()) { + QTextBlock b = doc->findBlockByNumber(blockNumber); + QTC_ASSERT(b.isValid(), return ); + highlighter->setExtraFormats(b, std::move(ranges[blockNumber])); + } + } +} + void SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd( SyntaxHighlighter *highlighter, const QFuture &future) diff --git a/src/plugins/texteditor/semantichighlighter.h b/src/plugins/texteditor/semantichighlighter.h index f57b037957c..d39ba7b80d0 100644 --- a/src/plugins/texteditor/semantichighlighter.h +++ b/src/plugins/texteditor/semantichighlighter.h @@ -86,6 +86,15 @@ void TEXTEDITOR_EXPORT incrementalApplyExtraAdditionalFormats( int from, int to, const QHash &kindToFormat); +// Clears all extra highlights and applies the extra formats +// indicated by Result::kind and kindToFormat to the correct location using +// SyntaxHighlighter::setExtraFormats. In contrast to +// incrementalApplyExtraAdditionalFormats the results do not have to be ordered by line. +void TEXTEDITOR_EXPORT setExtraAdditionalFormats( + SyntaxHighlighter *highlighter, + const QList &results, + const QHash &kindToFormat); + // Cleans the extra additional formats after the last result of the Future // until the end of the document. // Requires that results of the Future are ordered by line.