Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ void Highlighter::highlightBlock(const QString &text)
m_currentContext->definition());

ProgressData progress;
m_ruleProgressOffset = -1;
m_ruleProgressCount = 0;
const int length = text.length();
m_lastRegionChanged = -1;
while (progress.offset() < length)
Expand All @@ -125,6 +127,7 @@ void Highlighter::highlightBlock(const QString &text)
}
} catch (const HighlighterException &) {
m_isBroken = true;
m_contexts.clear();
}
}

Expand Down Expand Up @@ -205,12 +208,37 @@ void Highlighter::setupFromPersistent()
setCurrentBlockState(previousBlockState());
}

void Highlighter::checkRuleProgress(const ProgressData *progress)
{
// Look-ahead and empty matches may legitimately change contexts without
// consuming text. Bound that work per offset, including recursive calls.
if (progress->offset() > m_ruleProgressOffset) {
m_ruleProgressOffset = progress->offset();
m_ruleProgressCount = 0;
}
if (++m_ruleProgressCount > 1024) {
qWarning() << "Syntax highlighter stopped: rules made no progress at block"
<< currentBlock().blockNumber() << "offset" << progress->offset()
<< "context" << m_currentContext->name();
throw HighlighterException();
}
}

void Highlighter::iterateThroughRules(const QString &text,
const int length,
ProgressData *progress,
const bool childRule,
const QList<QSharedPointer<Rule> > &rules)
{
const QList<QSharedPointer<Rule> > &rules,
const int depth)
{
// Fallthrough and child rules can recurse before returning to the block loop.
if (depth > 128) {
qWarning() << "Syntax highlighter stopped: rule recursion limit at block"
<< currentBlock().blockNumber() << "offset" << progress->offset()
<< "context" << m_currentContext->name();
throw HighlighterException();
}
checkRuleProgress(progress);
typedef QList<QSharedPointer<Rule> >::const_iterator RuleIterator;

bool contextChanged = false;
Expand All @@ -231,6 +259,7 @@ void Highlighter::iterateThroughRules(const QString &text,
}

if (rule->matchSucceed(text, length, progress)) {
checkRuleProgress(progress);
atLeastOneMatch = true;
if (!m_stringOrComment) {
if (!rule->beginRegion().isEmpty()) {
Expand Down Expand Up @@ -277,7 +306,7 @@ void Highlighter::iterateThroughRules(const QString &text,
progress->setWillContinueLine(false);
} else {
if (rule->hasChildren())
iterateThroughRules(text, length, progress, true, rule->children());
iterateThroughRules(text, length, progress, true, rule->children(), depth + 1);

if (!rule->context().isEmpty() && contextChangeRequired(rule->context())) {
m_currentCaptures = progress->captures();
Expand Down Expand Up @@ -313,7 +342,7 @@ void Highlighter::iterateThroughRules(const QString &text,
if (m_currentContext->isFallthrough()) {
handleContextChange(m_currentContext->fallthroughContext(),
m_currentContext->definition());
iterateThroughRules(text, length, progress, false, m_currentContext->rules());
iterateThroughRules(text, length, progress, false, m_currentContext->rules(), depth + 1);
} else {
applyFormat(progress->offset(), 1, m_currentContext->itemData(),
m_currentContext->definition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ class Highlighter : public TextEditor::SyntaxHighlighter
void setupFromWillContinue();
void setupFromContinued();
void setupFromPersistent();
void checkRuleProgress(const ProgressData *progress);

void iterateThroughRules(const QString &text,
const int length,
ProgressData *progress,
const bool childRule,
const QList<QSharedPointer<Rule> > &rules);
const QList<QSharedPointer<Rule> > &rules,
const int depth = 0);

void assignCurrentContext();
bool contextChangeRequired(const QString &contextName) const;
Expand Down Expand Up @@ -166,6 +168,8 @@ class Highlighter : public TextEditor::SyntaxHighlighter
int m_dynamicContextsCounter;

bool m_isBroken;
int m_ruleProgressOffset;
int m_ruleProgressCount;
bool m_stringOrComment;
TextFormatId m_currentFormat;

Expand Down
15 changes: 15 additions & 0 deletions liteidex/src/3rdparty/qtc_texteditor/tests/highlighter.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
TEMPLATE = app
TARGET = tst_highlighter
QT += widgets testlib printsupport
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
CONFIG += testcase console c++11
CONFIG -= app_bundle

INCLUDEPATH += .. ../../../utils ../../../api
SOURCES += tst_highlighter.cpp ../syntaxhighlighter.cpp ../basetextdocumentlayout.cpp ../tabsettings.cpp
HEADERS += ../syntaxhighlighter.h ../basetextdocumentlayout.h
HEADERS += ../generichighlighter/highlighter.h

for(source, $$list(highlighter context highlightdefinition rule specificrules progressdata dynamicrule keywordlist itemdata includerulesinstruction)) {
SOURCES += ../generichighlighter/$${source}.cpp
}
125 changes: 125 additions & 0 deletions liteidex/src/3rdparty/qtc_texteditor/tests/tst_highlighter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <QtTest>
#include <QTextDocument>
#include "generichighlighter/highlighter.h"
#include "generichighlighter/highlightdefinition.h"
#include "generichighlighter/specificrules.h"
#include "generichighlighter/itemdata.h"

using namespace TextEditor::Internal;

class TestHighlighter : public Highlighter
{
public:
TestHighlighter(QTextDocument *document) : Highlighter(document) {}
bool isBroken() const { return m_isBroken; }
bool hasContexts() const { return !m_contexts.isEmpty(); }
};

static QSharedPointer<Context> addContext(const QSharedPointer<HighlightDefinition> &definition,
const QString &name, bool initial = false)
{
QSharedPointer<Context> context = definition->createContext(name, initial);
context->setDefinition(definition);
context->setItemData("Normal");
context->setLineEndContext("#stay");
return context;
}

static QSharedPointer<DetectCharRule> addRule(const QSharedPointer<Context> &context,
const QString &target, bool lookAhead = true)
{
QSharedPointer<DetectCharRule> rule(new DetectCharRule);
rule->setChar("x");
rule->setContext(target);
rule->setLookAhead(lookAhead ? "true" : "false");
rule->setItemData("Normal");
rule->setDefinition(context->definition());
context->addRule(rule);
return rule;
}

class HighlighterTest : public QObject
{
Q_OBJECT
private slots:
void ruleLoops_data()
{
QTest::addColumn<QString>("mode");
QTest::addColumn<bool>("broken");
QTest::newRow("lookahead-stay") << QString("stay") << true;
QTest::newRow("lookahead-base-pop") << QString("pop") << true;
QTest::newRow("empty-string-match") << QString("empty") << true;
QTest::newRow("lookahead-context-cycle") << QString("cycle") << true;
QTest::newRow("fallthrough-stay") << QString("fallthrough") << true;
QTest::newRow("fallthrough-context-cycle") << QString("fallthrough-cycle") << true;
QTest::newRow("recursive-children") << QString("children") << true;
QTest::newRow("consuming-rule") << QString("consume") << false;
QTest::newRow("valid-lookahead") << QString("valid") << false;
}

void ruleLoops()
{
QFETCH(QString, mode);
QFETCH(bool, broken);
QSharedPointer<HighlightDefinition> definition(new HighlightDefinition);
definition->createItemData("Normal")->setStyle("dsNormal");
definition->itemData("Normal")->setColor("#ff0000");
QSharedPointer<Context> base = addContext(definition, "base", true);
if (mode == "fallthrough" || mode == "fallthrough-cycle") {
base->setFallthrough("true");
base->setFallthroughContext(mode == "fallthrough" ? "#stay" : "other");
if (mode == "fallthrough-cycle") {
QSharedPointer<Context> other = addContext(definition, "other");
other->setFallthrough("true");
other->setFallthroughContext("#pop");
}
} else if (mode == "cycle" || mode == "valid") {
QSharedPointer<Context> other = addContext(definition, "other");
addRule(base, "other");
addRule(other, "#pop", mode == "cycle");
} else if (mode == "empty") {
QSharedPointer<StringDetectRule> rule(new StringDetectRule);
rule->setString("");
rule->setInsensitive("false");
rule->setContext("#stay");
rule->setDefinition(definition);
base->addRule(rule);
} else if (mode == "children") {
QSharedPointer<DetectCharRule> parent = addRule(base, "#stay");
for (int i = 0; i < 140; ++i) {
QSharedPointer<DetectCharRule> child(new DetectCharRule);
child->setChar("x");
child->setLookAhead("true");
child->setDefinition(definition);
parent->addChild(child);
parent = child;
}
} else {
addRule(base, mode == "pop" ? "#pop" : "#stay", mode != "consume");
}

QTextDocument document;
// More characters than the no-progress budget verifies that consuming
// matches reset the budget rather than disabling normal long lines.
const QString text = QString(4096, QLatin1Char('x')) + "\nx";
document.setPlainText(text);
TestHighlighter highlighter(&document);
highlighter.setDefaultContext(base);
highlighter.rehighlight();
QCOMPARE(highlighter.isBroken(), broken);
QVERIFY(!highlighter.hasContexts());
QCOMPARE(document.toPlainText(), text);
if (!broken) {
const auto formats = document.begin().layout()->formats();
QVERIFY(!formats.isEmpty());
QCOMPARE(formats.first().start, 0);
QCOMPARE(formats.first().length, 4096);
QCOMPARE(formats.first().format.foreground().color(), QColor(Qt::red));
}
highlighter.rehighlight();
QCOMPARE(highlighter.isBroken(), broken);
}
};

QTEST_MAIN(HighlighterTest)
#include "tst_highlighter.moc"
Loading