tweaks to syntax highlighting

This commit is contained in:
Axel Kohlmeyer
2023-08-06 19:46:26 -04:00
parent b81aaebd87
commit 6dcfe130a4

View File

@ -18,18 +18,23 @@ Highlighter::Highlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
HighlightingRule rule;
outputFormat.setForeground(Qt::darkYellow);
outputFormat.setFontWeight(QFont::Bold);
readFormat.setForeground(Qt::magenta);
readFormat.setFontWeight(QFont::Bold);
latticeFormat.setForeground(Qt::darkGreen);
latticeFormat.setFontWeight(QFont::Bold);
particleFormat.setForeground(Qt::darkRed);
particleFormat.setFontWeight(QFont::Bold);
setupFormat.setForeground(Qt::darkCyan);
runFormat.setForeground(Qt::green);
setupFormat.setFontWeight(QFont::Bold);
runFormat.setForeground(Qt::darkBlue);
runFormat.setFontWeight(QFont::Bold);
defineFormat.setForeground(Qt::darkMagenta);
defineFormat.setFontWeight(QFont::Bold);
numberFormat.setForeground(Qt::blue);
commentFormat.setForeground(Qt::red);
stringFormat.setForeground(Qt::cyan);
stringFormat.setForeground(Qt::darkGreen);
stringFormat.setFontWeight(QFont::Normal);
const QString output_keywords[] = {
@ -202,11 +207,44 @@ Highlighter::Highlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
void Highlighter::highlightBlock(const QString &text)
{
auto style = QRegularExpression("^(fix|compute|dump|set)\\s+(\\w+)\\s+(\\S+)\\s+(\\S+)").match(text);
auto force = QRegularExpression("^(atom_style|pair_style|bond_style|angle_style|dihedral_style|improper_style|kspace_style)\\s+(\\S+)").match(text);
auto defs = QRegularExpression("^(group|variable)\\s+(\\S+)\\s+(\\S+)").match(text);
auto undo = QRegularExpression("^(unfix|uncompute|undump)\\s+(\\w+)").match(text);
bool do_style = true;
bool do_force = true;
bool do_defs = true;
bool do_undo = true;
for (const HighlightingRule &rule : qAsConst(highlightingRules)) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
// special treatment for fix/compute/dump styles etc.
if (style.hasMatch() && do_style) {
setFormat(style.capturedStart(1), style.capturedLength(1), defineFormat);
setFormat(style.capturedStart(2), style.capturedLength(2), numberFormat);
setFormat(style.capturedStart(3), style.capturedLength(3), stringFormat);
setFormat(style.capturedStart(4), style.capturedLength(4), runFormat);
do_style = false;
// special treatment for force styles styles
} else if (force.hasMatch() && do_force) {
setFormat(force.capturedStart(1), force.capturedLength(1), particleFormat);
setFormat(force.capturedStart(2), force.capturedLength(2), runFormat);
do_force = false;
// special treatment for undo commands
} else if (undo.hasMatch() && do_undo) {
setFormat(undo.capturedStart(1), undo.capturedLength(1), defineFormat);
setFormat(undo.capturedStart(2), undo.capturedLength(2), stringFormat);
do_undo = false;
// special treatment for some definitions
} else if (defs.hasMatch() && do_defs) {
setFormat(defs.capturedStart(1), defs.capturedLength(1), particleFormat);
setFormat(defs.capturedStart(2), defs.capturedLength(2), stringFormat);
setFormat(defs.capturedStart(3), defs.capturedLength(3), runFormat);
do_defs = false;
} else {
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
}
}