matching brackets highlight in editors

This commit is contained in:
Vadim Lopatin 2015-02-11 15:55:07 +03:00
parent 579b297147
commit f703b74408
2 changed files with 50 additions and 0 deletions

View File

@ -647,6 +647,22 @@ class EditableContent {
handleContentChange(new EditOperation(EditAction.SaveContent), rangeBefore, rangeAfter, this);
}
bool findMatchedBraces(TextPosition p, out TextRange range) {
if (!_syntaxHighlighter)
return false;
TextPosition p2 = _syntaxHighlighter.findPairedBracket(p);
if (p == p2)
return false;
if (p < p2) {
range.start = p;
range.end = p2;
} else {
range.start = p2;
range.end = p;
}
return true;
}
protected void updateTokenProps(int startLine, int endLine) {
clearTokenProps(startLine, endLine);
if (_syntaxHighlighter) {

View File

@ -1629,6 +1629,7 @@ class EditBox : EditWidgetBase {
new Action(EditorActions.ZoomOut, KeyCode.SUB, KeyFlag.Control),
]);
}
protected uint _matchingBracketHightlightColor = 0xFFF0E0;
protected int _firstVisibleLine;
@ -2109,6 +2110,20 @@ class EditBox : EditWidgetBase {
//measuredContent(parentWidth, parentHeight, textSz.x + vsbwidth, textSz.y + hsbheight);
}
protected void highlightLineRange(DrawBuf buf, Rect lineRect, uint color, TextRange r) {
Rect startrc = textPosToClient(r.start);
Rect endrc = textPosToClient(r.end);
Rect rc = lineRect;
rc.left = _clientRect.left + startrc.left;
rc.right = _clientRect.left + endrc.right;
if (!rc.empty) {
// draw selection rect for matching bracket
Log.d("highlight bracket 1: ", rc);
buf.fillRect(rc, 0xFFDD80);
}
}
/// override to custom highlight of line background
protected void drawLineBackground(DrawBuf buf, int lineIndex, Rect lineRect, Rect visibleRect) {
// highlight odd lines
@ -2130,6 +2145,17 @@ class EditBox : EditWidgetBase {
}
}
if (_matchingBraces.start.line == lineIndex) {
TextRange r = TextRange(_matchingBraces.start, _matchingBraces.start);
r.end.pos++;
highlightLineRange(buf, lineRect, _matchingBracketHightlightColor, r);
}
if (_matchingBraces.end.line == lineIndex) {
TextRange r = TextRange(_matchingBraces.end, _matchingBraces.end);
r.end.pos++;
highlightLineRange(buf, lineRect, _matchingBracketHightlightColor, r);
}
// frame around current line
if (focused && lineIndex == _caretPos.line && _selectionRange.singleLine && _selectionRange.start.line == _caretPos.line) {
buf.drawFrame(visibleRect, 0xA0808080, Rect(1,1,1,1));
@ -2207,7 +2233,15 @@ class EditBox : EditWidgetBase {
return null;
}
TextRange _matchingBraces;
override protected void drawClient(DrawBuf buf) {
// update matched braces
if (!content.findMatchedBraces(_caretPos, _matchingBraces)) {
_matchingBraces.start.line = -1;
_matchingBraces.end.line = -1;
}
Rect rc = _clientRect;
FontRef font = font();