- implement: highlight matching brackets

This commit is contained in:
royqh1979@gmail.com 2021-10-12 20:38:39 +08:00
parent 7eda4e35b3
commit 141de5ed74
4 changed files with 89 additions and 7 deletions

View File

@ -5,7 +5,8 @@ Version 0.6.5
- fix: not correctly parse gdb's output
- fix: path not correctly setted for the debugger process
- fix: indent line not correctly drawed
- enhancement: use rainbox color to draw indent guide lines
- implement: highlight matching brackets
Version 0.6.4
- fix: code completion popup not show after '->' inputted

View File

@ -825,6 +825,23 @@ void Editor::onPreparePaintHighlightToken(int line, int aChar, const QString &to
}
}
if (!selAvail() && attr->name() == SYNS_AttrSymbol) {
qDebug()<<line<<":"<<aChar<<" - "<<mHighlightCharPos1.Line<<":"<<mHighlightCharPos1.Char<<" - "<<mHighlightCharPos2.Line<<":"<<mHighlightCharPos2.Char;
if ( (line == mHighlightCharPos1.Line)
&& (aChar == mHighlightCharPos1.Char)) {
foreground = selectedForeground();
background = selectedBackground();
return;
}
if ((line == mHighlightCharPos2.Line)
&& (aChar == mHighlightCharPos2.Char)) {
foreground = selectedForeground();
background = selectedBackground();
return;
}
}
// qDebug()<<token<<"-"<<attr->name()<<" - "<<line<<" : "<<aChar;
if (mParser && (attr == highlighter()->identifierAttribute())) {
@ -1309,6 +1326,8 @@ void Editor::onStatusChanged(SynStatusChanges changes)
if (changes.testFlag(SynStatusChange::scCaretX)
|| changes.testFlag(SynStatusChange::scCaretY)) {
mHighlightCharPos1 = BufferCoord{0,0};
mHighlightCharPos2 = BufferCoord{0,0};
if (mTabStopBegin >=0) {
if (mTabStopY==caretY()) {
if (mLineAfterTabStop.isEmpty()) {
@ -1334,6 +1353,35 @@ void Editor::onStatusChanged(SynStatusChanges changes)
clearUserCodeInTabStops();
}
}
} else if (!selAvail() && highlighter()){
// Is there a bracket char before us?
int lineLength = lineText().length();
int ch = caretX() - 2;
BufferCoord coord;
if (ch>=0 && ch<lineLength && isBraceChar(lineText()[ch]) ) {
coord.Char = ch+1;
coord.Line = caretY();
}
//or after us?
ch = caretX()-1;
if (ch>=0 && ch<lineLength && isBraceChar(lineText()[ch]) ) {
coord.Char = ch+1;
coord.Line = caretY();
}
PSynHighlighterAttribute attr;
QString token;
if (getHighlighterAttriAtRowCol(coord,token,attr)
&& attr == highlighter()->symbolAttribute()) {
BufferCoord complementCharPos = getMatchingBracketEx(coord);
if (!foldHidesLine(coord.Line)
&& !foldHidesLine(complementCharPos.Line)) {
mHighlightCharPos1 = coord;
mHighlightCharPos2 = complementCharPos;
invalidateLine(mHighlightCharPos1.Line);
invalidateLine(mHighlightCharPos2.Line);
}
}
}
}
@ -1433,6 +1481,21 @@ void Editor::onLinesInserted(int first, int count)
}
}
bool Editor::isBraceChar(QChar ch)
{
switch( ch.unicode()) {
case '{':
case '}':
case '[':
case ']':
case '(':
case ')':
return true;
default:
return false;
}
}
void Editor::resetBreakpoints()
{
mBreakpointLines.clear();
@ -3542,13 +3605,13 @@ void Editor::applyColorScheme(const QString& schemeName)
setOptions(options);
highlighterManager.applyColorScheme(highlighter(),schemeName);
if (pSettings->editor().rainbowParenthesis()) {
PSynHighlighterAttribute attr0 =createRainbowAttribute(COLOR_SCHEME_BRACE_1,
PSynHighlighterAttribute attr0 =createRainbowAttribute(SYNS_AttrSymbol,
schemeName,COLOR_SCHEME_BRACE_1);
PSynHighlighterAttribute attr1 =createRainbowAttribute(COLOR_SCHEME_BRACE_2,
PSynHighlighterAttribute attr1 =createRainbowAttribute(SYNS_AttrSymbol,
schemeName,COLOR_SCHEME_BRACE_2);
PSynHighlighterAttribute attr2 =createRainbowAttribute(COLOR_SCHEME_BRACE_3,
PSynHighlighterAttribute attr2 =createRainbowAttribute(SYNS_AttrSymbol,
schemeName,COLOR_SCHEME_BRACE_3);
PSynHighlighterAttribute attr3 =createRainbowAttribute(COLOR_SCHEME_BRACE_4,
PSynHighlighterAttribute attr3 =createRainbowAttribute(SYNS_AttrSymbol,
schemeName,COLOR_SCHEME_BRACE_4);
setRainbowAttrs(attr0,attr1,attr2,attr3);
}

View File

@ -175,6 +175,7 @@ private slots:
void onLinesInserted(int first,int count);
private:
bool isBraceChar(QChar ch);
void resetBreakpoints();
QChar getCurrentChar();
bool handleSymbolCompletion(QChar key);
@ -261,6 +262,8 @@ private:
QString mLineBeforeTabStop;
QString mLineAfterTabStop;
QList<PTabStop> mUserCodeInTabStops;
BufferCoord mHighlightCharPos1;
BufferCoord mHighlightCharPos2;
// QWidget interface
protected:

View File

@ -607,7 +607,6 @@ void SynEditTextPainter::AddHighlightToken(const QString &Token, int ColumnsBefo
Foreground = edit->palette().color(QPalette::Text);
}
//todo : change char(getTokenPos) to column?
edit->onPreparePaintHighlightToken(cLine,edit->mHighlighter->getTokenPos()+1,
Token,p_Attri,Style,Foreground,Background);
@ -672,11 +671,27 @@ void SynEditTextPainter::PaintFoldAttributes()
while (LastNonBlank + 1 < edit->mLines->count() && edit->mLines->getString(LastNonBlank).trimmed().isEmpty())
LastNonBlank++;
LineIndent = edit->getLineIndent(edit->mLines->getString(LastNonBlank));
int braceLevel = edit->mLines->ranges(LastNonBlank).braceLevel;
int indentLevel = braceLevel ;
if (edit->mTabWidth>0)
indentLevel = LineIndent / edit->mTabWidth;
int levelDiff = std::max(0,braceLevel - indentLevel);
// Step horizontal coord
TabSteps = edit->mTabWidth;
//TabSteps = edit->mTabWidth;
TabSteps = 0;
indentLevel = 0;
while (TabSteps < LineIndent) {
X = TabSteps * edit->mCharWidth + edit->textOffset() - 2;
TabSteps+=edit->mTabWidth;
indentLevel++ ;
if (edit->mHighlighter) {
PSynHighlighterAttribute attr = edit->mHighlighter->symbolAttribute();
GetBraceColorAttr(indentLevel,attr);
if (attr!=edit->mHighlighter->symbolAttribute()) {
dottedPen.setColor(attr->foreground());
painter->setPen(dottedPen);
}
}
// Move to top of vertical line
painter->drawLine(X,Y,X,Y+edit->mTextHeight);