ich bin gerade dabei, neben dem HTML-Highlighter jetzt einen PHP-Highlighter zu schreiben. Dafür habe ich alle php funktionen, konstanten und ähnliches in verschiedene Text-Dateien gepackt, die ich im Quellcode dann gezielt färben möchte. Wenn ich Funktionen + Kontroll Strukuren färben lasse, funktioniert alles wunderbar. Sobald ich allerdings die Konstanten (nochmal 1000+ Wörter) hinzunehme, hängt sich das Programm sofort auf, wenn ich nur eine Eingabe ins Textfeld mache. Wie kann ich das performancegünstiger machen? Hier der jetzige Quellcode
Code: Alles auswählen
#include "phphighlighter.h"
#include <QFile>
#include <QTextStream>
PhpHighlighter::PhpHighlighter(QTextDocument *document) : QSyntaxHighlighter(document)
{
HighlightingRule rule;
/*
* Control Structures like
* if, else, switch ...
*/
controlStructuresFormat.setForeground(QColor(161, 161, 0));
QFile file(":/Keywords/php_control_structures.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
while(!in.atEnd()) {
rule.pattern = QRegExp("\\b" + in.readLine() + "\\b");
rule.format = controlStructuresFormat;
highlightingRules.append(rule);
}
/*
* All in PHP predeclared functions
*/
functionsFormat.setForeground(QColor(100, 74, 155));
QFile file2(":/Keywords/php_functions.txt");
if (!file2.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in2(&file2);
while(!in2.atEnd()) {
rule.pattern = QRegExp("\\b" + in2.readLine() + "\\b");
rule.format = functionsFormat;
highlightingRules.append(rule);
}
constantsFormat.setFontWeight(QFont::Bold);
QFile file3(":/Keywords/php_constants.txt");
if (!file3.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in3(&file3);
while(!in3.atEnd()) {
rule.pattern = QRegExp("\\b" + in3.readLine() + "\\b");
rule.format = constantsFormat;
highlightingRules.append(rule);
}
}
void PhpHighlighter::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
}Basti
