Commit 14c4be23 authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Add better format for IDs in Code syntax highlighter

parent c334b0f0
......@@ -49,19 +49,21 @@ QmlSyntaxHighlighter::QmlSyntaxHighlighter (QTextDocument * document)
"false",
})
, highlightingRules ({
Rule { "\\b[a-z][a-zA-Z0-9_]*\\b", &memberFormat }, // member
Rule { "\\b[0-9]+\\b", &numberFormat }, // numbers
Rule { "\\b0x[0-9A-Fa-f]+\\b", &numberFormat }, // hexa
Rule { ("\\b(" % keywords.join ('|') % ")\\b"), &keywordFormat }, // keywords
Rule { "\\b[A-Z][a-zA-Z0-9_]*\\b", &objectFormat }, // objects
Rule { "\".*\"", &quotationFormat }, // double quoted string
Rule { "'.*'", &quotationFormat }, // simple quoted string
Rule { "\\/\\/.*", &commentFormat }, // double slash comment
Rule { "\\b[a-z][a-zA-Z0-9_]*\\b", &memberFormat }, // member
Rule { "\\b[0-9]+\\b", &numberFormat }, // numbers
Rule { "\\b0x[0-9A-Fa-f]+\\b", &numberFormat }, // hexa
Rule { ("\\b(" % keywords.join ('|') % ")\\b"), &keywordFormat }, // keywords
Rule { "\\b[A-Z][a-zA-Z0-9_]*\\b", &objectFormat }, // objects
Rule { "\\bid\\s*:\\s*([a-z][a-zA-Z0-9_]*)\\s*;", &identifierFormat }, // id
Rule { "\".*\"", &quotationFormat }, // double quoted string
Rule { "'.*'", &quotationFormat }, // simple quoted string
Rule { "\\/\\/.*", &commentFormat }, // double slash comment
})
, highlightingRulesMultiline ({
RuleMultiline { "/\\*", "\\*/", &commentFormat, Commented }, // block comment
})
{
identifierFormat.setFontItalic (true);
connect (this, &QmlSyntaxHighlighter::useDarkThemeChanged, this, &QmlSyntaxHighlighter::refreshFormats);
emit useDarkThemeChanged (m_useDarkTheme);
}
......@@ -70,8 +72,16 @@ void QmlSyntaxHighlighter::highlightBlock (const QString & text) {
for (const Rule & rule : highlightingRules) {
QRegExp expression (rule.pattern);
int index = expression.indexIn (text);
int length = 0;
while (index >= 0) {
const int length = expression.matchedLength ();
if (expression.captureCount () > 0) {
const int idx = expression.captureCount ();
index = expression.pos (idx);
length = expression.cap (idx).size ();
}
else {
length = expression.matchedLength ();
}
setFormat (index, length, (* rule.format));
index = expression.indexIn (text, index + length);
}
......@@ -101,12 +111,13 @@ void QmlSyntaxHighlighter::highlightBlock (const QString & text) {
}
void QmlSyntaxHighlighter::refreshFormats (void) {
memberFormat.setForeground (QColor (m_useDarkTheme ? "#ff5500" : "#D14500"));
numberFormat.setForeground (QColor (m_useDarkTheme ? "#00aa7f" : "#2377FF"));
keywordFormat.setForeground (QColor (m_useDarkTheme ? "#ffff7f" : "#927F00"));
objectFormat.setForeground (QColor (m_useDarkTheme ? "#aa00ff" : "#A1009A"));
quotationFormat.setForeground (QColor (m_useDarkTheme ? "#00aa00" : "#346A00"));
commentFormat.setForeground (QColor (m_useDarkTheme ? "#808080" : "#808080"));
identifierFormat.setForeground (QColor (m_useDarkTheme ? "#ff5500" : "#D14500"));
memberFormat.setForeground (QColor (m_useDarkTheme ? "#ff5500" : "#D14500"));
numberFormat.setForeground (QColor (m_useDarkTheme ? "#00aa7f" : "#2377FF"));
keywordFormat.setForeground (QColor (m_useDarkTheme ? "#ffff7f" : "#927F00"));
objectFormat.setForeground (QColor (m_useDarkTheme ? "#aa00ff" : "#A1009A"));
quotationFormat.setForeground (QColor (m_useDarkTheme ? "#00aa00" : "#346A00"));
commentFormat.setForeground (QColor (m_useDarkTheme ? "#808080" : "#808080"));
rehighlight ();
}
......
......@@ -54,6 +54,7 @@ private:
QTextCharFormat numberFormat;
QTextCharFormat quotationFormat;
QTextCharFormat objectFormat;
QTextCharFormat identifierFormat;
QTextCharFormat commentFormat;
struct Rule {
QString pattern;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment