Skip to content

Commit

Permalink
Datafilter syntax highlighter and "()[]{}"
Browse files Browse the repository at this point in the history
.. bracket matching code for ( and ) fixed up as well as adding
   support for matching [ and ].

.. helps to highlight errors in code when it starts to get a
   little complex.
  • Loading branch information
liversedge committed Oct 1, 2021
1 parent 7be34c1 commit aecc1c9
Showing 1 changed file with 92 additions and 2 deletions.
94 changes: 92 additions & 2 deletions src/Core/DataFilter.cpp
Expand Up @@ -801,6 +801,7 @@ DataFilter::colorSyntax(QTextDocument *document, int pos)
int symbolstart=0;
int brace=0;
int brack=0;
int sbrack=0;

for(int i=0; i<string.length(); i++) {

Expand Down Expand Up @@ -1031,6 +1032,78 @@ DataFilter::colorSyntax(QTextDocument *document, int pos)
}
}

// are the brackets balanced [ ] ?
if (!instring && !incomment && string[i]=='[') {
sbrack++;

// match close/open if over cursor
if (i==pos-1) {
cursor.setPosition(i, QTextCursor::MoveAnchor);
cursor.selectionStart();
cursor.setPosition(i+1, QTextCursor::KeepAnchor);
cursor.selectionEnd();
cursor.mergeCharFormat(cyanbg);

// run forward looking for match
int bb=0;
for(int j=i; j<string.length(); j++) {
if (string[j]=='[') bb++;
if (string[j]==']') {
bb--;
if (bb == 0) {
bpos = j; // matched brack here, don't change color!

cursor.setPosition(j, QTextCursor::MoveAnchor);
cursor.selectionStart();
cursor.setPosition(j+1, QTextCursor::KeepAnchor);
cursor.selectionEnd();
cursor.mergeCharFormat(cyanbg);
break;
}
}
}
}
}
if (!instring && !incomment && string[i]==']') {
sbrack--;

if (i==pos-1) {

cursor.setPosition(i, QTextCursor::MoveAnchor);
cursor.selectionStart();
cursor.setPosition(i+1, QTextCursor::KeepAnchor);
cursor.selectionEnd();
cursor.mergeCharFormat(cyanbg);

// run backward looking for match
int bb=0;
for(int j=i; j>=0; j--) {
if (string[j]==']') bb++;
if (string[j]=='[') {
bb--;
if (bb == 0) {
bpos = j; // matched brack 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 (sbrack < 0 && i != bpos-1) {

cursor.setPosition(i, QTextCursor::MoveAnchor);
cursor.selectionStart();
cursor.setPosition(i+1, QTextCursor::KeepAnchor);
cursor.selectionEnd();
cursor.mergeCharFormat(redbg);
}
}

// are the braces balanced ( ) ?
if (!instring && !incomment && string[i]=='{') {
brace++;
Expand Down Expand Up @@ -1127,8 +1200,8 @@ DataFilter::colorSyntax(QTextDocument *document, int pos)
brack = 0;
for(int i=string.length(); i>=0; i--) {

if (string[i] == ')') brace++;
if (string[i] == '(') brace--;
if (string[i] == ')') brack++;
if (string[i] == '(') brack--;

if (brack < 0 && string[i] == '(' && i != pos-1 && i != bpos-1) {
cursor.setPosition(i, QTextCursor::MoveAnchor);
Expand All @@ -1140,6 +1213,23 @@ DataFilter::colorSyntax(QTextDocument *document, int pos)
}
}

if (sbrack > 0) {
sbrack = 0;
for(int i=string.length(); i>=0; i--) {

if (string[i] == ']') sbrack++;
if (string[i] == '[') sbrack--;

if (sbrack < 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
if(treeRoot) treeRoot->color(treeRoot, document);
}
Expand Down

0 comments on commit aecc1c9

Please sign in to comment.