Thursday, 15 September 2011

regex - QSyntaxHighlighter multiline comment in document not appearing correctly -



regex - QSyntaxHighlighter multiline comment in document not appearing correctly -

referring syntax highlighter example provided qt official website, tried implement (actually can phone call re-create paste) same logic multiline comment in application. reference code multiline comment highlighting:

inside constructor:

quotationformat.setforeground(qcolor(164, 14, 14)); rule.pattern = qregexp("\".*\""); rule.format = quotationformat; highlightingrules.append(rule); charformat.setforeground(qcolor(164, 14, 14)); rule.pattern = qregexp("\'.*\'"); rule.format = charformat; highlightingrules.append(rule); singlelinecommentformat.setforeground(qt::darkgreen); rule.pattern = qregexp("//[^\n]*"); rule.format = singlelinecommentformat; highlightingrules.append(rule); multilinecommentformat.setforeground(qt::darkgreen); commentstartexpression = qregexp("/\\*"); commentendexpression = qregexp("\\*/");

inside highlightblock() function:

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); int startindex = 0; if (previousblockstate() != 1) startindex = commentstartexpression.indexin(text); while (startindex >= 0) { int endindex = commentendexpression.indexin(text, startindex); int commentlength; if (endindex == -1) { setcurrentblockstate(1); commentlength = text.length() - startindex; } else { commentlength = endindex - startindex + commentendexpression.matchedlength(); } setformat(startindex, commentlength, multilinecommentformat); startindex = commentstartexpression.indexin(text, startindex + commentlength); }

but still there problem facing when /* appears within quotes. appears multiline comment color (green color) till end ( /*.png" ] } ) see next sample document content reference:

{ "code": [ "./code/main.js" ], "textures": [ "./content/*.png" ] }

i not master of regular expression, guess there wrong regex.

with luck , effort have found workaround (still it'll not cover cases)

int mysyntaxhighlighter::getcommentstartindex(const qstring &text, const int offset) { int startindex = -1; qdebug() << "offset: " << offset; if(text.length() > 1 && offset < (text.length() - 1)) { int commentstartindex = commentstartexpression.indexin(text, offset); qdebug() << "commentstartindex: " << commentstartindex; qregexp quotationexpression(quotationrule.pattern); int quotationstartindex = quotationexpression.indexin(text, offset); qdebug() << "quotationstartindex: " << quotationstartindex; if (quotationstartindex >= 0) { int quotationlength = quotationexpression.matchedlength(); qdebug() << "quotationlength: " << quotationlength; if(commentstartindex > quotationstartindex && commentstartindex < (quotationstartindex + quotationlength)) { startindex = getcommentstartindex(text, (commentstartindex + 2)); } else { startindex = commentstartindex; } } else if(commentstartindex >= 0) { startindex = commentstartindex; } else { startindex = -1; } } qdebug() << "startindex: " << startindex; homecoming startindex; } void mysyntaxhighlighter::highlightblock(const qstring &text) { setcurrentblockstate(0); qdebug() << "text: " << text; qdebug() << "previousblockstate(): " << previousblockstate(); int startindex = 0; if (previousblockstate() != 1) { // startindex = commentendexpression.indexin(text); startindex = getcommentstartindex(text, 0); } while (startindex >= 0) { int endindex = commentendexpression.indexin(text, startindex); int commentlength; if (endindex == -1) { setcurrentblockstate(1); commentlength = text.length() - startindex; } else { commentlength = endindex - startindex + commentendexpression.matchedlength(); } setformat(startindex, commentlength, multilinecommentformat); // startindex = commentstartexpression.indexin(text, startindex + commentlength); startindex = getcommentstartindex(text, startindex + commentlength); } }

if finds improve solution please share me.

regex qt qt4 syntax-highlighting

No comments:

Post a Comment