diff --git a/src/DataFilter.cpp b/src/DataFilter.cpp index 6cf13a282..39fb20e76 100644 --- a/src/DataFilter.cpp +++ b/src/DataFilter.cpp @@ -171,8 +171,11 @@ Leaf::isDynamic(Leaf *leaf) } void -DataFilter::colorSyntax(QTextDocument *document) +DataFilter::colorSyntax(QTextDocument *document, int pos) { + // matched brace position + int bpos = -1; + // for looking for comments QString string = document->toPlainText(); @@ -182,6 +185,11 @@ DataFilter::colorSyntax(QTextDocument *document) normal.setUnderlineStyle(QTextCharFormat::NoUnderline); normal.setForeground(Qt::black); + QTextCharFormat cyanbg; + cyanbg.setBackground(Qt::cyan); + QTextCharFormat redbg; + redbg.setBackground(QColor(255,153,153)); + QTextCharFormat symbol; symbol.setFontWeight(QFont::Bold); symbol.setUnderlineStyle(QTextCharFormat::NoUnderline); @@ -215,6 +223,7 @@ DataFilter::colorSyntax(QTextDocument *document) int stringstart=0; int numberstart=0; int symbolstart=0; + int brace=0; for(int i=0; i=0; j--) { + if (string[j]==')') bb++; + if (string[j]=='(') { + bb--; + if (bb == 0) { + bpos = j; // matched brace here, don't change color! + + cursor.setPosition(j, QTextCursor::MoveAnchor); + cursor.selectionStart(); + cursor.setPosition(j+1, QTextCursor::KeepAnchor); + cursor.selectionEnd(); + cursor.mergeCharFormat(cyanbg); + break; + } + } + } + + } else if (brace < 0 && i != bpos-1) { + + cursor.setPosition(i, QTextCursor::MoveAnchor); + cursor.selectionStart(); + cursor.setPosition(i+1, QTextCursor::KeepAnchor); + cursor.selectionEnd(); + cursor.mergeCharFormat(redbg); + } + } + } + + // unbalanced braces - do same as above, backwards? + //XXX braces in comments fuck things up ... XXX + if (brace > 0) { + brace = 0; + for(int i=string.length(); i>=0; i--) { + + if (string[i] == ')') brace++; + if (string[i] == '(') brace--; + + if (brace < 0 && string[i] == '(' && i != pos-1 && i != bpos-1) { + cursor.setPosition(i, QTextCursor::MoveAnchor); + cursor.selectionStart(); + cursor.setPosition(i+1, QTextCursor::KeepAnchor); + cursor.selectionEnd(); + cursor.mergeCharFormat(redbg); + } + } } // apply selective coloring to the symbols and expressions diff --git a/src/DataFilter.h b/src/DataFilter.h index 1eedfceee..b058750c2 100644 --- a/src/DataFilter.h +++ b/src/DataFilter.h @@ -110,7 +110,7 @@ class DataFilter : public QObject // when used for formulas Result evaluate(RideItem *rideItem); QStringList getErrors() { return errors; }; - void colorSyntax(QTextDocument *content); + void colorSyntax(QTextDocument *content, int pos); static QStringList functions(); // return list of functions supported diff --git a/src/LTMTool.cpp b/src/LTMTool.cpp index 47a1f4c8c..b65a523c8 100644 --- a/src/LTMTool.cpp +++ b/src/LTMTool.cpp @@ -2375,6 +2375,7 @@ LTMTool::setFilter(QStringList files) DataFilterEdit::DataFilterEdit(QWidget *parent, Context *context) : QTextEdit(parent), context(context), c(0) { + connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(checkErrors())); } DataFilterEdit::~DataFilterEdit() @@ -2387,7 +2388,7 @@ DataFilterEdit::checkErrors() // parse and present errors to user DataFilter checker(this, context); QStringList errors = checker.check(toPlainText()); - checker.colorSyntax(document()); // syntax + error highlighting + checker.colorSyntax(document(), textCursor().position()); // syntax + error highlighting // need to fixup for errors! // XXX next commit