diff options
author | Ken Moore <moorekou@gmail.com> | 2016-04-21 11:19:15 -0400 |
---|---|---|
committer | Ken Moore <moorekou@gmail.com> | 2016-04-21 11:19:15 -0400 |
commit | 648c7eaee1a370b78d0efbfadf3f51ae5829acfc (patch) | |
tree | d16aaa4d6529e82eb2970f2b63e7cc02d44f2bc0 | |
parent | Fix up the syntax rule for highlighting quoted string. Now it will properly w... (diff) | |
download | lumina-648c7eaee1a370b78d0efbfadf3f51ae5829acfc.tar.gz lumina-648c7eaee1a370b78d0efbfadf3f51ae5829acfc.tar.bz2 lumina-648c7eaee1a370b78d0efbfadf3f51ae5829acfc.zip |
Fix up the bracket matching routine to accomodate for nested brackets.
-rw-r--r-- | desktop-utilities/lumina-textedit/PlainTextEditor.cpp | 44 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/PlainTextEditor.h | 2 |
2 files changed, 30 insertions, 16 deletions
diff --git a/desktop-utilities/lumina-textedit/PlainTextEditor.cpp b/desktop-utilities/lumina-textedit/PlainTextEditor.cpp index fb02f905..281c4bec 100644 --- a/desktop-utilities/lumina-textedit/PlainTextEditor.cpp +++ b/desktop-utilities/lumina-textedit/PlainTextEditor.cpp @@ -138,15 +138,29 @@ void PlainTextEditor::clearMatchData(){ } } -void PlainTextEditor::highlightMatch(QChar ch, bool forward, int fromPos){ - if(forward){ - matchleft = fromPos; - QTextCursor cur = this->document()->find(ch, fromPos); - if(!cur.isNull()){ matchright = cur.position(); } - }else{ - matchright = fromPos; - QTextCursor cur = this->document()->find(ch, fromPos, QTextDocument::FindBackward); - if(!cur.isNull()){ matchleft = cur.position(); } +void PlainTextEditor::highlightMatch(QChar ch, bool forward, int fromPos, QChar startch){ + if(forward){ matchleft = fromPos; } + else{ matchright = fromPos; } + + int nested = 1; //always start within the first nest (the primary nest) + int tmpFromPos = fromPos; + QString doc = this->toPlainText(); + while( nested>0 && tmpFromPos<doc.length() && ( (tmpFromPos>=fromPos && forward) || ( tmpFromPos<=fromPos && !forward ) ) ){ + if(forward){ + QTextCursor cur = this->document()->find(ch, tmpFromPos); + if(!cur.isNull()){ + nested += doc.mid(tmpFromPos+1, cur.position()-tmpFromPos).count(startch) -1; + if(nested==0){ matchright = cur.position(); } + else{ tmpFromPos = cur.position(); } + }else{ break; } + }else{ + QTextCursor cur = this->document()->find(ch, tmpFromPos, QTextDocument::FindBackward); + if(!cur.isNull()){ + nested += doc.mid(cur.position(), tmpFromPos-cur.position()).count(startch) -1; + if(nested==0){ matchleft = cur.position(); } + else{ tmpFromPos = cur.position()-1; } + }else{ break; } + } } //Now highlight the two characters @@ -219,12 +233,12 @@ void PlainTextEditor::checkMatchChar(){ bool tryback = true; while(tryback){ tryback = false; - if(ch==QChar('(')){ highlightMatch(QChar(')'),true, pos); } - else if(ch==QChar(')')){ highlightMatch(QChar('('),false, pos); } - else if(ch==QChar('{')){ highlightMatch(QChar('}'),true, pos); } - else if(ch==QChar('}')){ highlightMatch(QChar('{'),false, pos); } - else if(ch==QChar('[')){ highlightMatch(QChar(']'),true, pos); } - else if(ch==QChar(']')){ highlightMatch(QChar('['),false, pos); } + if(ch==QChar('(')){ highlightMatch(QChar(')'),true, pos, QChar('(') ); } + else if(ch==QChar(')')){ highlightMatch(QChar('('),false, pos, QChar(')') ); } + else if(ch==QChar('{')){ highlightMatch(QChar('}'),true, pos, QChar('{') ); } + else if(ch==QChar('}')){ highlightMatch(QChar('{'),false, pos, QChar('}') ); } + else if(ch==QChar('[')){ highlightMatch(QChar(']'),true, pos, QChar('[') ); } + else if(ch==QChar(']')){ highlightMatch(QChar('['),false, pos, QChar(']') ); } else if(pos==this->textCursor().position()){ //Try this one more time - using the previous character instead of the current character tryback = true; diff --git a/desktop-utilities/lumina-textedit/PlainTextEditor.h b/desktop-utilities/lumina-textedit/PlainTextEditor.h index 12c73ab0..8ba1e4bc 100644 --- a/desktop-utilities/lumina-textedit/PlainTextEditor.h +++ b/desktop-utilities/lumina-textedit/PlainTextEditor.h @@ -48,7 +48,7 @@ private: //Bracket/Perentheses matching functions int matchleft, matchright; //positions within the document void clearMatchData(); - void highlightMatch(QChar ch, bool forward, int fromPos); + void highlightMatch(QChar ch, bool forward, int fromPos, QChar startch); //Flags to keep track of changes bool hasChanges; |