diff options
author | Ken Moore <ken@ixsystems.com> | 2017-06-09 14:04:11 -0400 |
---|---|---|
committer | Ken Moore <ken@ixsystems.com> | 2017-06-09 14:04:11 -0400 |
commit | bbf81abc29e626b654366b7083babe9f355709ea (patch) | |
tree | f62602228809158e4e48d465f58f323b453a673c /src-qt5/desktop-utils/lumina-textedit/syntaxSupport.cpp | |
parent | Get the SyntaxFile loading mechanisms all setup. (diff) | |
download | lumina-bbf81abc29e626b654366b7083babe9f355709ea.tar.gz lumina-bbf81abc29e626b654366b7083babe9f355709ea.tar.bz2 lumina-bbf81abc29e626b654366b7083babe9f355709ea.zip |
Get the new syntax highlighting support framework functional with the C++ rules files as the example (still need to convert the other existing rulesets over to the new format).
Diffstat (limited to 'src-qt5/desktop-utils/lumina-textedit/syntaxSupport.cpp')
-rw-r--r-- | src-qt5/desktop-utils/lumina-textedit/syntaxSupport.cpp | 274 |
1 files changed, 45 insertions, 229 deletions
diff --git a/src-qt5/desktop-utils/lumina-textedit/syntaxSupport.cpp b/src-qt5/desktop-utils/lumina-textedit/syntaxSupport.cpp index a590d3a4..9c264fca 100644 --- a/src-qt5/desktop-utils/lumina-textedit/syntaxSupport.cpp +++ b/src-qt5/desktop-utils/lumina-textedit/syntaxSupport.cpp @@ -27,7 +27,7 @@ QColor SyntaxFile::colorFromOption(QString opt, QSettings *settings){ }else if(opt.startsWith("colors/")){ return QColor(settings->value(opt,"").toString()); } - return QColor(); + return QColor("transparent"); } QString SyntaxFile::name(){ @@ -46,6 +46,13 @@ bool SyntaxFile::highlight_excess_whitespace(){ return formatObj.value("highlight_whitespace_eol").toBool(); } +int SyntaxFile::tab_length(){ + int num = -1; + if(formatObj.contains("tab_width")){ num = formatObj.value("tab_width").toInt(); } + if(num<=0){ num = 8; } //UNIX Standard of 8 characters per tab + return num; +} + void SyntaxFile::SetupDocument(QPlainTextEdit* editor){ if(formatObj.contains("line_wrap")){ editor->setLineWrapMode( formatObj.value("line_wrap").toBool() ? QPlainTextEdit::WidgetWidth : QPlainTextEdit::NoWrap); @@ -54,7 +61,8 @@ void SyntaxFile::SetupDocument(QPlainTextEdit* editor){ QString type = formatObj.value("font_type").toString(); QFont font = editor->document()->defaultFont(); // current font if(type=="monospace"){ - font = QFontDatabase::systemFont(QFontDatabase::FixedFont); //get the default fixed-size font for the system + font = QFont("monospace"); + //font = QFontDatabase::systemFont(QFontDatabase::FixedFont); //get the default fixed-size font for the system } font.setStyle(QFont::StyleNormal); font.setStyleStrategy(QFont::PreferAntialias); @@ -81,8 +89,12 @@ bool SyntaxFile::LoadFile(QString file, QSettings *settings){ if(contents[0].startsWith("{")){ break; } //stop here else{ contents.removeAt(0); } } - QJsonObject obj = QJsonDocument::fromJson(contents.join("\n").simplified().toLocal8Bit()).object(); - if(!obj.contains("meta") || !obj.contains("format") || !obj.contains("rules")){ return false; } //could not get any info + //qDebug() << "Contents:" << contents.join("\n").simplified(); + QJsonParseError err; + QJsonDocument doc = QJsonDocument::fromJson(contents.join("\n").simplified().toLocal8Bit(), &err ); + if(doc.isNull()){ qDebug() << "JSON Syntax Error:" << err.errorString(); } + QJsonObject obj = doc.object(); + if(!obj.contains("meta") || !obj.contains("format") || !obj.contains("rules")){ qDebug() << "Could not read syntax file:" << file; return false; } //could not get any info //Save the raw meta/format objects for later fileLoaded = file; metaObj = obj.value("meta").toObject(); @@ -95,11 +107,14 @@ bool SyntaxFile::LoadFile(QString file, QSettings *settings){ QJsonObject rule = rulesArray[i].toObject(); SyntaxRule tmp; //First load the rule + //qDebug() << "Load Rule:" << rule.keys(); if(rule.contains("words")){} //valid option - handled at the end though else if(rule.contains("regex")){ tmp.pattern = QRegExp(rule.value("regex").toString()); - }else if(rule.contains("regex_start") && rule.contains("regex_stop")){ - + }else if(rule.contains("regex_start") && rule.contains("regex_end")){ + tmp.startPattern = QRegExp(rule.value("regex_start").toString()); + tmp.endPattern = QRegExp(rule.value("regex_end").toString()); + //qDebug() << " -- Multi-line Rule:" << tmp.startPattern << tmp.endPattern; }else{ continue; } //bad rule - missing the actual detection logic //Now load the appearance logic if(rule.contains("foreground")){ tmp.format.setForeground( colorFromOption(rule.value("foreground").toString(), settings) ); } @@ -139,10 +154,12 @@ QList<SyntaxFile> SyntaxFile::availableFiles(QSettings *settings){ for(int i=0; i<paths.length(); i++){ QDir dir(paths[i]+"/lumina-desktop/syntax_rules"); if(!dir.exists()){ continue; } + //qDebug() << "Found directory:" << dir.absolutePath(); QFileInfoList files = dir.entryInfoList(QStringList() << "*.syntax", QDir::Files, QDir::Name); for(int f=0; f<files.length(); f++){ if(paths.contains(files[f].absoluteFilePath()) ){ continue; } //already handled //New Syntax File found + //qDebug() << "Found File:" << files[f].fileName(); SyntaxFile nfile; if( nfile.LoadFile(files[f].absoluteFilePath(), settings) ){ list << nfile; } } @@ -150,12 +167,14 @@ QList<SyntaxFile> SyntaxFile::availableFiles(QSettings *settings){ return list; } -QStringList Custom_Syntax::availableRules(){ +QStringList Custom_Syntax::availableRules(QSettings *settings){ QStringList avail; - avail << "C++"; + QList<SyntaxFile> files = SyntaxFile::availableFiles(settings); + for(int i=0; i<files.length(); i++){ avail << files[i].name(); } + /*avail << "C++"; //avail << "Python"; avail << "Shell"; - avail << "reST"; + avail << "reST";*/ return avail; } @@ -181,232 +200,29 @@ void Custom_Syntax::SetupDefaultColors(QSettings *settings){ if(!settings->contains("colors/preprocessor")){settings->setValue("colors/preprocessor", QColor(Qt::darkYellow).name() ); } } -QString Custom_Syntax::ruleForFile(QString filename){ - QString suffix = filename.section(".",-1); +QString Custom_Syntax::ruleForFile(QString filename, QSettings *settings){ + QList<SyntaxFile> files = SyntaxFile::availableFiles(settings); + for(int i=0; i<files.length(); i++){ + if(files[i].supportsFile(filename)){ return files[i].name(); } + } + + /*QString suffix = filename.section(".",-1); if(suffix=="cpp" || suffix=="hpp" || suffix=="c" || suffix=="h"){ return "C++"; } //else if(suffix=="py" || suffix=="pyc"){ return "Python"; } else if(suffix=="sh"){ return "Shell"; } - else if(suffix=="rst"){ return "reST"; } + else if(suffix=="rst"){ return "reST"; }*/ return ""; } -void Custom_Syntax::loadRules(QString type){ - //NOTE: the "multiLineComment - lasttype = type; - rules.clear(); - splitrules.clear(); - if(type=="C++"){ - //Keywords (standard C/C++/Qt definitions) - QStringList keywords; - keywords << "char" << "class" << "const" << "double" << "enum" << "explicit" << "extern" << "float" << "friend" << "inline" \ - << "int" << "long" << "namespace" << "operator" << "private" << "protected" << "public" \ - << "short" << "signals" << "signed" << "slots" << "static" << "struct" << "template" \ - << "typedef" << "typename" << "union" << "unsigned" << "virtual" << "void" << "volatile" \ - << "true" << "false" << "bool"; - SyntaxRule rule; - rule.format.setForeground( QColor(settings->value("colors/keyword").toString()) ); - 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; - } - //Alternate Keywords (built-in functions) - keywords.clear(); - keywords << "for" << "while" << "switch" << "case" << "if" << "else" << "return" << "exit"; - rule.format.setForeground( QColor(settings->value("colors/altkeyword").toString()) ); - 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( QColor(settings->value("colors/class").toString()) ); - rule.pattern = QRegExp("\\b[A-Za-z0-9_-\\.]+(?=::)\\b"); - rules << rule; - //Quotes - rule.format.setForeground( QColor(settings->value("colors/text").toString()) ); - rule.format.setFontWeight(QFont::Normal); - rule.pattern = QRegExp( "\"[^\"\\\\]*(\\\\(.|\\n)[^\"\\\\]*)*\"|'[^'\\\\]*(\\\\(.|\\n)[^'\\\\]*)*'"); - rules << rule; - //Functions - rule.format.setForeground( QColor(settings->value("colors/function").toString()) ); - rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()"); - rules << rule; - //Proprocessor commands - rule.format.setForeground( QColor(settings->value("colors/preprocessor").toString()) ); - rule.pattern = QRegExp("^[\\s]*#[^\n]*"); - rules << rule; - //Comment (single line) - rule.format.setForeground( QColor(settings->value("colors/comment").toString()) ); - 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; - - }else if(type=="Shell"){ - //Keywords (standard Shell definitions) - QStringList keywords; - keywords << "alias" << "alloc" << "bg" << "bind" << " bindkey" << "break" \ - << "breaksw"<<"builtins"<<"case"<<"cd"<<"chdir"<<"command"<<"complete"<<"continue"<<"default" \ - <<"dirs"<<"do"<<"done"<<"echo"<<"echotc"<<"elif"<<"else"<<"end"<<"endif"<<"endsw"<<"esac"<<"eval" \ - <<"exec"<<"exit"<<"export"<<"false"<<"fc"<<"fg"<<"filetest"<<"fi"<<"for"<<"foreach"<<"getopts" \ - <<"glob"<<"goto"<<"hash"<<"hashstat"<<"history"<<"hup"<<"if"<<"jobid"<<"jobs"<<"kill"<<"limit" \ - <<"local"<<"log"<<"login"<<"logout"<<"ls-F"<<"nice"<<"nohup"<<"notify"<<"onintr"<<"popd" \ - <<"printenv"<<"printf"<<"pushd"<<"pwd"<<"read"<<"readonly"<<"rehash"<<"repeat"<<"return" \ - <<"sched"<<"set"<<"setenv"<<"settc"<<"setty"<<"setvar"<<"shift"<<"source"<<"stop"<<"suspend" \ - <<"switch"<<"telltc"<<"test"<<"then"<<"time"<<"times"<<"trap"<<"true"<<"type"<<"ulimit"<<"umask" \ - <<"unalias"<<"uncomplete"<<"unhash"<<"unlimit"<<"unset"<<"unsetenv"<<"until"<<"wait" \ - <<"where"<<"which"<<"while"; - - SyntaxRule rule; - rule.format.setForeground( QColor(settings->value("colors/keyword").toString()) ); - 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; - } - //Alternate Keywords (built-in functions) - /*keywords.clear(); - keywords << "for" << "while" << "switch" << "case" << "if" << "else" << "return" << "exit"; - rule.format.setForeground( QColor(settings->value("colors/altkeyword").toString()) ); - 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; - }*/ - //Variable Names - rule.format.setForeground( QColor(settings->value("colors/class").toString()) ); - rule.pattern = QRegExp("\\$\\{[^\\n\\}]+\\}"); - rules << rule; - rule.pattern = QRegExp("\\$[^\\s$]+(?=\\s|$)"); - rules << rule; - //Quotes - rule.format.setForeground( QColor(settings->value("colors/text").toString()) ); - rule.format.setFontWeight(QFont::Normal); - rule.pattern = QRegExp( "\"[^\"\\\\]*(\\\\(.|\\n)[^\"\\\\]*)*\"|'[^'\\\\]*(\\\\(.|\\n)[^'\\\\]*)*'"); - rules << rule; - //Functions - rule.format.setForeground( QColor(settings->value("colors/function").toString()) ); - rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()"); - rules << rule; - //Proprocessor commands - rule.format.setForeground( QColor(settings->value("colors/preprocessor").toString()) ); - rule.pattern = QRegExp("^#![^\n]*"); - rules << rule; - //Comment (single line) - rule.format.setForeground( QColor(settings->value("colors/comment").toString()) ); - 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; - - }else if(type=="Python"){ - //Keywords - QStringList keywords; - keywords << "and" << "as" << "assert" << "break" << "class" << "continue" << "def" << "del" \ - << "elif" << "else" << "except" << "exec" << "finally" << "for" << "from" \ - << "global" << "if" << "import" << "in" << "is" << "lambda" << "not" \ - << "or" << "pass" << "print" << "raise" << "return" << "try" << "while" << "with" << "yield"; - - SyntaxRule rule; - rule.format.setForeground( QColor(settings->value("colors/keyword").toString()) ); - 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( QColor(settings->value("colors/text").toString()) ); - rule.format.setFontWeight(QFont::Normal); - rule.pattern = QRegExp( "\"[^\"\\\\]*(\\\\(.|\\n)[^\"\\\\]*)*\"|'[^'\\\\]*(\\\\(.|\\n)[^'\\\\]*)*'"); - rules << rule; - //Functions - rule.format.setForeground( QColor(settings->value("colors/function").toString()) ); - rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()"); - rules << rule; - //Comment (single line) - rule.format.setForeground( QColor(settings->value("colors/comment").toString()) ); - 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; - - }else if(type=="reST"){ - SyntaxRule rule; - // directives - rule.format.setForeground( QColor(settings->value("colors/class").toString()) ); - rule.format.setFontItalic(false); - rule.pattern = QRegExp("(\\s|^):[a-zA-Z0-9 ]*:`[^`]*`"); - rules << rule; - // hyperlinks - rule.format.setFontItalic(true); - rule.format.setFontWeight(QFont::Normal); - rule.pattern = QRegExp("`[^\\<]*\\<[^\\>]*\\>`_"); - rules << rule; - // Code Sample - rule.format.setFontItalic(false); - rule.format.setFontWeight(QFont::Light); - rule.format.setFontFixedPitch(true); - rule.pattern = QRegExp("\\b`{2}.*`{2}\\b"); - rules << rule; - //Quotes - /*rule.format.setForeground( QColor(settings->value("colors/text").toString()) ); - rule.format.setFontWeight(QFont::Normal); - rule.pattern = QRegExp( "\"[^\"\\\\]*(\\\\(.|\\n)[^\"\\\\]*)*\"|'[^'\\\\]*(\\\\(.|\\n)[^'\\\\]*)*'"); - rules << rule;*/ - //TODO - rule = SyntaxRule(); //reset rule - rule.format.setFontWeight( QFont::Bold ); - rule.pattern = QRegExp("^\\.\\.\\sTODO\\b"); - rules << rule; - rule = SyntaxRule(); //reset rule - rule.format.setFontWeight( QFont::Bold ); - rule.pattern = QRegExp("^(\\s*)\\.\\.(\\s*)([a-zA-Z0-9]+)::"); - rules << rule; - //Functions - rule = SyntaxRule(); //reset rule - rule.format.setForeground( QColor(settings->value("colors/preprocessor").toString()) ); - rule.pattern = QRegExp("^(\\s*)\\.\\.(\\s*)\\b_[a-zA-Z0-9 ]*:(\\s|$)"); - rules << rule; - //figures and other properties for them - rule = SyntaxRule(); //reset rule - rule.format.setForeground( QColor(settings->value("colors/keyword").toString()) ); - rule.pattern = QRegExp("^(\\s*)\\.\\.\\sfigure::\\s"); - rules << rule; - rule = SyntaxRule(); //reset rule - rule.format.setForeground( QColor(settings->value("colors/altkeyword").toString()) ); - rule.pattern = QRegExp("^( ){3}:(.)*: "); - rules << rule; - - //Code Blocks - SyntaxRuleSplit srule; - srule.format.setBackground( QColor("lightblue") ); - srule.startPattern = QRegExp("\\:\\:$"); - srule.endPattern = QRegExp("^(?=[^\\s])"); - splitrules << srule; - srule.startPattern = QRegExp("^(\\s*)\\.\\.\\scode-block::\\s"); //alternate start string for the same rule - srule.endPattern = QRegExp("^(?=[^\\s])"); - splitrules << srule; - //Comment (multi-line) - srule = SyntaxRuleSplit(); - srule.format.setForeground( QColor(settings->value("colors/comment").toString()) ); - srule.startPattern = QRegExp("^(\\s*)\\.\\.\\s[^_](?![\\w-_\\.]+::(\\s|$))"); - srule.endPattern = QRegExp("^(?=([^\\s]|$))"); - splitrules << srule; +void Custom_Syntax::loadRules(QString type){ + QList<SyntaxFile> files = SyntaxFile::availableFiles(settings); + for(int i=0; i<files.length(); i++){ + if(files[i].name() == type){ syntax = files[i]; break; } } + return; +} + +void Custom_Syntax::loadRules(SyntaxFile sfile){ + syntax = sfile; } |