2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-08-16 09:47:54 +02:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
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
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
2014-10-01 13:21:18 +02:00
|
|
|
** conditions see http://www.qt.io/licensing. For further information
|
|
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2011-08-16 09:47:54 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-08-16 09:47:54 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
#include "semantichighlighter.h"
|
|
|
|
|
|
|
|
|
|
#include "syntaxhighlighter.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextDocument>
|
|
|
|
|
#include <QTextBlock>
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
using namespace TextEditor::SemanticHighlighter;
|
|
|
|
|
|
|
|
|
|
void TextEditor::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,
|
|
|
|
|
const QHash<int, QTextCharFormat> &kindToFormat)
|
|
|
|
|
{
|
2011-08-30 10:52:41 +02:00
|
|
|
if (to <= from)
|
2011-08-16 09:47:54 +02:00
|
|
|
return;
|
|
|
|
|
|
2011-08-30 10:52:41 +02:00
|
|
|
const int firstResultBlockNumber = 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);
|
2011-08-16 09:47:54 +02:00
|
|
|
const int blockNumber = result.line - 1;
|
|
|
|
|
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);
|
|
|
|
|
QTextBlock b = doc->findBlockByNumber(currentBlockNumber);
|
|
|
|
|
|
2013-04-16 16:48:10 +02:00
|
|
|
HighlightingResult result = future.resultAt(from);
|
2011-08-30 10:52:41 +02:00
|
|
|
for (int i = from; i < to && b.isValid(); ) {
|
|
|
|
|
const int blockNumber = result.line - 1;
|
2011-08-16 09:47:54 +02:00
|
|
|
QTC_ASSERT(blockNumber < doc->blockCount(), return);
|
|
|
|
|
|
|
|
|
|
// clear formats of blocks until blockNumber
|
|
|
|
|
while (currentBlockNumber < blockNumber) {
|
2013-02-26 11:17:13 +01:00
|
|
|
QList<QTextLayout::FormatRange> noFormats;
|
|
|
|
|
highlighter->setExtraAdditionalFormats(b, noFormats);
|
2011-08-16 09:47:54 +02:00
|
|
|
b = b.next();
|
|
|
|
|
++currentBlockNumber;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-30 10:52:41 +02:00
|
|
|
// collect all the formats for the current line
|
2011-08-16 09:47:54 +02:00
|
|
|
QList<QTextLayout::FormatRange> formats;
|
2013-02-26 11:17:13 +01:00
|
|
|
formats.reserve(to - from);
|
2011-08-30 10:52:41 +02:00
|
|
|
forever {
|
2011-08-16 09:47:54 +02:00
|
|
|
QTextLayout::FormatRange formatRange;
|
|
|
|
|
|
|
|
|
|
formatRange.format = kindToFormat.value(result.kind);
|
2011-08-30 10:52:41 +02:00
|
|
|
if (formatRange.format.isValid()) {
|
|
|
|
|
formatRange.start = result.column - 1;
|
|
|
|
|
formatRange.length = result.length;
|
|
|
|
|
formats.append(formatRange);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++i;
|
|
|
|
|
if (i >= to)
|
|
|
|
|
break;
|
|
|
|
|
result = future.resultAt(i);
|
|
|
|
|
const int nextBlockNumber = result.line - 1;
|
|
|
|
|
if (nextBlockNumber != blockNumber)
|
|
|
|
|
break;
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
|
|
|
|
highlighter->setExtraAdditionalFormats(b, formats);
|
|
|
|
|
b = b.next();
|
|
|
|
|
++currentBlockNumber;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextEditor::SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd(
|
|
|
|
|
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) {
|
|
|
|
|
lastBlockNumber = result.line - 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextDocument *doc = highlighter->document();
|
2011-09-16 09:50:17 +02:00
|
|
|
|
|
|
|
|
const int firstBlockToClear = lastBlockNumber + 1;
|
|
|
|
|
if (firstBlockToClear == doc->blockCount())
|
|
|
|
|
return;
|
|
|
|
|
QTC_ASSERT(firstBlockToClear < doc->blockCount(), return);
|
|
|
|
|
|
|
|
|
|
QTextBlock b = doc->findBlockByNumber(firstBlockToClear);
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
while (b.isValid()) {
|
2013-02-26 11:17:13 +01:00
|
|
|
QList<QTextLayout::FormatRange> noFormats;
|
|
|
|
|
highlighter->setExtraAdditionalFormats(b, noFormats);
|
2011-08-16 09:47:54 +02:00
|
|
|
b = b.next();
|
|
|
|
|
}
|
|
|
|
|
}
|