2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
#include "semantichighlighter.h"
|
|
|
|
|
|
|
|
|
|
#include "syntaxhighlighter.h"
|
2016-03-07 12:31:08 +01:00
|
|
|
#include "texteditorsettings.h"
|
2023-05-08 17:11:54 +02:00
|
|
|
#include "syntaxhighlighterrunner.h"
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextBlock>
|
2020-10-19 14:49:06 +02:00
|
|
|
#include <QTextDocument>
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
using namespace TextEditor::SemanticHighlighter;
|
|
|
|
|
|
2016-03-07 12:31:08 +01:00
|
|
|
namespace {
|
|
|
|
|
|
2020-10-19 14:49:06 +02:00
|
|
|
class Range {
|
|
|
|
|
public:
|
2019-06-13 10:21:04 +02:00
|
|
|
QTextLayout::FormatRange formatRange;
|
2020-10-19 14:49:06 +02:00
|
|
|
QTextBlock block;
|
|
|
|
|
};
|
|
|
|
|
using Ranges = QVector<Range>;
|
2019-06-13 10:21:04 +02:00
|
|
|
|
2020-10-19 18:03:31 +02:00
|
|
|
const Ranges rangesForResult(const HighlightingResult &result, const QTextBlock &startBlock,
|
|
|
|
|
const QHash<int, QTextCharFormat> &kindToFormat)
|
2020-10-19 14:49:06 +02:00
|
|
|
{
|
|
|
|
|
const QTextCharFormat format = result.useTextSyles
|
2019-06-13 10:21:04 +02:00
|
|
|
? TextEditorSettings::fontSettings().toTextCharFormat(result.textStyles)
|
|
|
|
|
: kindToFormat.value(result.kind);
|
2020-10-19 14:49:06 +02:00
|
|
|
if (!format.isValid())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
HighlightingResult curResult = result;
|
2020-10-19 18:03:31 +02:00
|
|
|
QTextBlock curBlock = startBlock;
|
2020-10-19 14:49:06 +02:00
|
|
|
Ranges ranges;
|
|
|
|
|
while (curBlock.isValid()) {
|
|
|
|
|
Range range;
|
|
|
|
|
range.block = curBlock;
|
|
|
|
|
range.formatRange.format = format;
|
|
|
|
|
range.formatRange.start = curResult.column - 1;
|
|
|
|
|
range.formatRange.length = std::min(curResult.length,
|
|
|
|
|
curBlock.length() - range.formatRange.start);
|
|
|
|
|
ranges << range;
|
|
|
|
|
if (range.formatRange.length == curResult.length)
|
|
|
|
|
break;
|
|
|
|
|
curBlock = curBlock.next();
|
|
|
|
|
curResult.column = 1;
|
|
|
|
|
curResult.length -= range.formatRange.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ranges;
|
2016-03-07 12:31:08 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-19 18:03:31 +02:00
|
|
|
const Ranges rangesForResult(
|
|
|
|
|
const HighlightingResult &result,
|
|
|
|
|
QTextDocument *doc,
|
|
|
|
|
const QHash<int, QTextCharFormat> &kindToFormat,
|
|
|
|
|
const Splitter &splitter = {})
|
|
|
|
|
{
|
|
|
|
|
const QTextBlock startBlock = doc->findBlockByNumber(result.line - 1);
|
|
|
|
|
if (splitter) {
|
|
|
|
|
Ranges ranges;
|
|
|
|
|
for (const auto &[newResult, newBlock] : splitter(result, startBlock))
|
|
|
|
|
ranges << rangesForResult(newResult, newBlock, kindToFormat);
|
|
|
|
|
return ranges;
|
|
|
|
|
}
|
|
|
|
|
return rangesForResult(result, startBlock, kindToFormat);
|
2016-03-07 12:31:08 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-19 18:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-08 17:11:54 +02:00
|
|
|
void SemanticHighlighter::incrementalApplyExtraAdditionalFormats(
|
2024-01-04 14:50:45 +01:00
|
|
|
SyntaxHighlighterRunner *highlighter,
|
2023-05-08 17:11:54 +02:00
|
|
|
const QFuture<HighlightingResult> &future,
|
|
|
|
|
int from,
|
|
|
|
|
int to,
|
|
|
|
|
const QHash<int, QTextCharFormat> &kindToFormat,
|
|
|
|
|
const Splitter &splitter)
|
2011-08-16 09:47:54 +02:00
|
|
|
{
|
2011-08-30 10:52:41 +02:00
|
|
|
if (to <= from)
|
2011-08-16 09:47:54 +02:00
|
|
|
return;
|
|
|
|
|
|
2023-05-30 14:20:58 +02:00
|
|
|
const int resultStartLine = future.resultAt(from).line;
|
|
|
|
|
int formattingStartLine = 1;
|
|
|
|
|
|
|
|
|
|
// Find the line on which to start formatting, where "formatting" means to either
|
|
|
|
|
// clear out formats from outdated document versions (if there is no current result
|
|
|
|
|
// on that line), or apply the format corresponding to the respective result.
|
|
|
|
|
// Note that if there are earlier results on the same line, we have to make sure they
|
|
|
|
|
// get re-applied by adapting the from variable accordingly.
|
2011-08-16 09:47:54 +02:00
|
|
|
for (int i = from - 1; i >= 0; --i) {
|
2013-04-16 16:48:10 +02:00
|
|
|
const HighlightingResult &result = future.resultAt(i);
|
2023-05-30 14:20:58 +02:00
|
|
|
if (result.line == resultStartLine) {
|
|
|
|
|
from = i;
|
|
|
|
|
} else if (result.line < resultStartLine) {
|
|
|
|
|
formattingStartLine = result.line + 1;
|
2011-08-30 10:52:41 +02:00
|
|
|
from = i + 1;
|
|
|
|
|
break;
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextDocument *doc = highlighter->document();
|
2023-05-30 14:20:58 +02:00
|
|
|
QTC_ASSERT(formattingStartLine <= doc->blockCount(), return);
|
|
|
|
|
QTextBlock currentBlock = doc->findBlockByNumber(formattingStartLine - 1);
|
2011-08-16 09:47:54 +02:00
|
|
|
|
2020-10-19 14:49:06 +02:00
|
|
|
std::map<QTextBlock, QVector<QTextLayout::FormatRange>> formatRanges;
|
|
|
|
|
for (int i = from; i < to; ++i) {
|
2020-10-19 18:03:31 +02:00
|
|
|
for (const Range &range : rangesForResult(future.resultAt(i), doc, kindToFormat, splitter))
|
2020-10-19 14:49:06 +02:00
|
|
|
formatRanges[range.block].append(range.formatRange);
|
|
|
|
|
}
|
2011-08-16 09:47:54 +02:00
|
|
|
|
2023-05-08 17:11:54 +02:00
|
|
|
QList<int> clearBlockNumberVector;
|
|
|
|
|
QMap<int, QList<QTextLayout::FormatRange>> blockNumberMap;
|
2020-10-19 14:49:06 +02:00
|
|
|
for (auto &[block, ranges] : formatRanges) {
|
|
|
|
|
while (currentBlock < block) {
|
2023-05-08 17:11:54 +02:00
|
|
|
clearBlockNumberVector.append(currentBlock.blockNumber());
|
2020-10-19 14:49:06 +02:00
|
|
|
currentBlock = currentBlock.next();
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
2023-05-08 17:11:54 +02:00
|
|
|
blockNumberMap[block.blockNumber()] = ranges;
|
2020-10-19 14:49:06 +02:00
|
|
|
currentBlock = block.next();
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
2023-05-08 17:11:54 +02:00
|
|
|
highlighter->clearExtraFormats(clearBlockNumberVector);
|
|
|
|
|
highlighter->setExtraFormats(blockNumberMap);
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-04 14:50:45 +01:00
|
|
|
void SemanticHighlighter::setExtraAdditionalFormats(SyntaxHighlighterRunner *highlighter,
|
2019-06-13 12:21:30 +02:00
|
|
|
const QList<HighlightingResult> &results,
|
|
|
|
|
const QHash<int, QTextCharFormat> &kindToFormat)
|
|
|
|
|
{
|
2019-06-12 12:55:06 +02:00
|
|
|
if (!highlighter)
|
|
|
|
|
return;
|
2019-06-13 12:21:30 +02:00
|
|
|
highlighter->clearAllExtraFormats();
|
|
|
|
|
|
|
|
|
|
QTextDocument *doc = highlighter->document();
|
|
|
|
|
QTC_ASSERT(doc, return );
|
|
|
|
|
|
2023-05-08 17:11:54 +02:00
|
|
|
QMap<int, QList<QTextLayout::FormatRange>> blockNumberMap;
|
2019-06-13 12:21:30 +02:00
|
|
|
|
2023-05-08 17:11:54 +02:00
|
|
|
for (const HighlightingResult &result : results) {
|
|
|
|
|
const Ranges ranges = rangesForResult(result, doc, kindToFormat);
|
|
|
|
|
for (const Range &range : ranges)
|
|
|
|
|
blockNumberMap[range.block.blockNumber()].append(range.formatRange);
|
2019-06-13 12:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-08 17:11:54 +02:00
|
|
|
highlighter->setExtraFormats(blockNumberMap);
|
2019-06-13 12:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-03 23:46:35 +02:00
|
|
|
void SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd(
|
2024-01-04 14:50:45 +01:00
|
|
|
SyntaxHighlighterRunner *highlighter, const QFuture<HighlightingResult> &future)
|
2011-08-16 09:47:54 +02:00
|
|
|
{
|
2021-09-09 16:46:33 +02:00
|
|
|
const QTextDocument * const doc = highlighter->document();
|
|
|
|
|
QTextBlock firstBlockToClear = doc->begin();
|
2011-08-16 09:47:54 +02:00
|
|
|
for (int i = future.resultCount() - 1; i >= 0; --i) {
|
2013-04-16 16:48:10 +02:00
|
|
|
const HighlightingResult &result = future.resultAt(i);
|
2011-08-16 09:47:54 +02:00
|
|
|
if (result.line) {
|
2021-09-09 16:46:33 +02:00
|
|
|
const QTextBlock blockForLine = doc->findBlockByNumber(result.line - 1);
|
|
|
|
|
const QTextBlock lastBlockWithResults = doc->findBlock(
|
|
|
|
|
blockForLine.position() + result.column - 1 + result.length);
|
|
|
|
|
firstBlockToClear = lastBlockWithResults.next();
|
2011-08-16 09:47:54 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 17:11:54 +02:00
|
|
|
QList<int> clearBlockNumberVector;
|
2021-09-09 16:46:33 +02:00
|
|
|
for (QTextBlock b = firstBlockToClear; b.isValid(); b = b.next())
|
2023-05-08 17:11:54 +02:00
|
|
|
clearBlockNumberVector.append(b.blockNumber());
|
|
|
|
|
|
|
|
|
|
highlighter->clearExtraFormats(clearBlockNumberVector);
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|