2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-08-16 09:47:54 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-08-16 09:47:54 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-08-16 09:47:54 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2011-08-16 09:47:54 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-08-16 09:47:54 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
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"
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SemanticHighlighter::incrementalApplyExtraAdditionalFormats(SyntaxHighlighter *highlighter,
|
2013-04-16 16:48:10 +02:00
|
|
|
const QFuture<HighlightingResult> &future,
|
2011-08-16 09:47:54 +02:00
|
|
|
int from, int to,
|
2020-10-19 18:03:31 +02:00
|
|
|
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;
|
|
|
|
|
|
2019-06-13 07:16:01 +02:00
|
|
|
const int firstResultBlockNumber = int(future.resultAt(from).line) - 1;
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
// blocks between currentBlockNumber and the last block with results will
|
|
|
|
|
// be cleaned of additional extra formats if they have no results
|
|
|
|
|
int currentBlockNumber = 0;
|
|
|
|
|
for (int i = from - 1; i >= 0; --i) {
|
2013-04-16 16:48:10 +02:00
|
|
|
const HighlightingResult &result = future.resultAt(i);
|
2019-06-13 07:16:01 +02:00
|
|
|
const int blockNumber = int(result.line) - 1;
|
2011-08-16 09:47:54 +02:00
|
|
|
if (blockNumber < firstResultBlockNumber) {
|
|
|
|
|
// stop! found where last format stopped
|
|
|
|
|
currentBlockNumber = blockNumber + 1;
|
|
|
|
|
// add previous results for the same line to avoid undoing their formats
|
2011-08-30 10:52:41 +02:00
|
|
|
from = i + 1;
|
|
|
|
|
break;
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextDocument *doc = highlighter->document();
|
|
|
|
|
QTC_ASSERT(currentBlockNumber < doc->blockCount(), return);
|
2020-10-19 14:49:06 +02:00
|
|
|
QTextBlock currentBlock = doc->findBlockByNumber(currentBlockNumber);
|
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
|
|
|
|
2020-10-19 14:49:06 +02:00
|
|
|
for (auto &[block, ranges] : formatRanges) {
|
|
|
|
|
while (currentBlock < block) {
|
|
|
|
|
highlighter->clearExtraFormats(currentBlock);
|
|
|
|
|
currentBlock = currentBlock.next();
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
2020-10-19 14:49:06 +02:00
|
|
|
highlighter->setExtraFormats(block, std::move(ranges));
|
|
|
|
|
currentBlock = block.next();
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 12:21:30 +02:00
|
|
|
void SemanticHighlighter::setExtraAdditionalFormats(SyntaxHighlighter *highlighter,
|
|
|
|
|
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 );
|
|
|
|
|
|
2020-10-19 14:49:06 +02:00
|
|
|
std::map<QTextBlock, QVector<QTextLayout::FormatRange>> formatRanges;
|
2019-06-13 12:21:30 +02:00
|
|
|
|
|
|
|
|
for (auto result : results) {
|
2020-10-19 18:03:31 +02:00
|
|
|
for (const Range &range : rangesForResult(result, doc, kindToFormat))
|
2020-10-19 14:49:06 +02:00
|
|
|
formatRanges[range.block].append(range.formatRange);
|
2019-06-13 12:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-19 14:49:06 +02:00
|
|
|
for (auto &[block, ranges] : formatRanges)
|
|
|
|
|
highlighter->setExtraFormats(block, std::move(ranges));
|
2019-06-13 12:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-03 23:46:35 +02:00
|
|
|
void SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd(
|
2011-08-16 09:47:54 +02:00
|
|
|
SyntaxHighlighter *highlighter,
|
2013-04-16 16:48:10 +02:00
|
|
|
const QFuture<HighlightingResult> &future)
|
2011-08-16 09:47:54 +02:00
|
|
|
{
|
|
|
|
|
// find block number of last result
|
|
|
|
|
int lastBlockNumber = 0;
|
|
|
|
|
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) {
|
2019-06-13 07:16:01 +02:00
|
|
|
lastBlockNumber = int(result.line) - 1;
|
2011-08-16 09:47:54 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextDocument *doc = highlighter->document();
|
2011-09-16 09:50:17 +02:00
|
|
|
|
|
|
|
|
const int firstBlockToClear = lastBlockNumber + 1;
|
2017-01-13 10:30:41 +01:00
|
|
|
if (firstBlockToClear >= doc->blockCount())
|
2011-09-16 09:50:17 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QTextBlock b = doc->findBlockByNumber(firstBlockToClear);
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
while (b.isValid()) {
|
2019-06-12 13:15:48 +02:00
|
|
|
highlighter->clearExtraFormats(b);
|
2011-08-16 09:47:54 +02:00
|
|
|
b = b.next();
|
|
|
|
|
}
|
|
|
|
|
}
|