diff options
-rw-r--r-- | desktop-utilities/lumina-textedit/syntaxSupport.cpp | 39 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/syntaxSupport.h | 42 |
2 files changed, 80 insertions, 1 deletions
diff --git a/desktop-utilities/lumina-textedit/syntaxSupport.cpp b/desktop-utilities/lumina-textedit/syntaxSupport.cpp index 27664c52..38ca4d3f 100644 --- a/desktop-utilities/lumina-textedit/syntaxSupport.cpp +++ b/desktop-utilities/lumina-textedit/syntaxSupport.cpp @@ -5,6 +5,7 @@ // See the LICENSE file for full details //=========================================== #include "syntaxSupport.h" + QStringList Custom_Syntax::availableRules(){ QStringList avail; avail << "C++"; @@ -19,8 +20,46 @@ QString Custom_Syntax::ruleForFile(QString filename){ } void Custom_Syntax::loadRules(QString type){ + //NOTE: the "multiLineComment rules.clear(); + splitrules.clear(); if(type=="C++"){ + //Keywords (standard C/C++/Qt definitions) + QStringList keywords; + keywords << "char" << "class" << "const" << "double" << "enum" << "explicit" << "friend" << "inline" \ + << "int" << "long" << "namespace" << "operator" << "private" << "protected" << "public" \ + << "short" << "signals" << "signed" << "slots" << "static" << "struct" << "template" \ + << "typedef" << "typename" << "union" << "unsigned" << "virtual" << "void" << "volatile"; + SyntaxRule rule; + rule.format.setForeground(Qt::darkBlue); + rule.format.setFontWeight(QFont::Bold); + for(int i=0; i<keywords.length(); i++){ + rule.pattern = QRegExp("\\b"+keywords[i]+"\\b"); //turn each keyword into a QRegExp and insert the rule + rules << rule; + } + //Class Names + rule.format.setForeground(Qt::darkMagenta); + rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b"); + rules << rule; + //Quotes + rule.format.setForeground(Qt::red); + rule.format.setFontWeight(QFont::Normal); + rule.pattern = QRegExp("\".*\""); + rules << rule; + //Functions + rule.format.setForeground(Qt::blue); + rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()"); + rules << rule; + //Comment (single line) + rule.format.setForeground(Qt::green); + rule.pattern = QRegExp("//[^\n]*"); + rules << rule; + //Comment (multi-line) + SyntaxRuleSplit srule; + srule.format = rule.format; //re-use the single-line comment format + srule.startPattern = QRegExp("/\\*"); + srule.endPattern = QRegExp("\\*/"); + splitrules << srule; } }
\ No newline at end of file diff --git a/desktop-utilities/lumina-textedit/syntaxSupport.h b/desktop-utilities/lumina-textedit/syntaxSupport.h index e8944aa4..a4f36822 100644 --- a/desktop-utilities/lumina-textedit/syntaxSupport.h +++ b/desktop-utilities/lumina-textedit/syntaxSupport.h @@ -12,15 +12,23 @@ #include <QTextCharFormat> #include <QString> +//Simple syntax rules struct SyntaxRule{ QRegExp pattern; QTextCharFormat format; }; +//Complicated/multi-line rules +struct SyntaxRuleSplit{ + QRegExp startPattern, endPattern; + QTextCharFormat format; +}; class Custom_Syntax : public QSyntaxHighlighter{ Q_OBJECT private: QVector<SyntaxRule> rules; + QVector<SyntaxRuleSplit> splitrules; + public: Custom_Syntax(QTextDocument *parent = 0) : QSyntaxHighlighter(parent){ } @@ -39,7 +47,39 @@ protected: setFormat(index, len, rules[i].format); index = patt.indexIn(text, index+len); //go to the next match } - } + }//end loop over normal (single-line) patterns + //Now look for any multi-line patterns (starting/continuing/ending) + int start = 0; + int splitactive = previousBlockState(); + if(splitactive>splitrules.length()-1){ splitactive = -1; } //just in case + while(start>=0 && start<text.length()-1){ + //qDebug() << "split check:" << start << splitactive; + if(splitactive>=0){ + //Find the end of the current rule + int end = splitrules[splitactive].endPattern.indexIn(text, start); + if(end==-1){ + //rule did not finish - apply to all + setFormat(start, text.length()-start, splitrules[splitactive].format); + break; //stop looking for more multi-line patterns + }else{ + //Found end point + int len = end-start+splitrules[splitactive].endPattern.matchedLength(); + setFormat(start, len , splitrules[splitactive].format); + start+=len; //move pointer to the end of handled range + splitactive = -1; //done with this rule + } + } //end check for end match + //Look for the start of any new split rule + for(int i=0; i<splitrules.length() && splitactive<0; i++){ + int newstart = splitrules[i].startPattern.indexIn(text,start); + if(newstart>=start){ + splitactive = i; + start = newstart; + } + } + if(splitactive<0){ break; } //no other rules found - go ahead and exit the loop + } + setCurrentBlockState(splitactive); } }; #endif
\ No newline at end of file |