From f703b74408a5787bd85c6cad33a721ab0c003c12 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Wed, 11 Feb 2015 15:55:07 +0300 Subject: [PATCH] matching brackets highlight in editors --- src/dlangui/core/editable.d | 16 ++++++++++++++++ src/dlangui/widgets/editors.d | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/dlangui/core/editable.d b/src/dlangui/core/editable.d index ad278b2a..8cd26912 100644 --- a/src/dlangui/core/editable.d +++ b/src/dlangui/core/editable.d @@ -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) { diff --git a/src/dlangui/widgets/editors.d b/src/dlangui/widgets/editors.d index 63b1eab7..fd51235c 100644 --- a/src/dlangui/widgets/editors.d +++ b/src/dlangui/widgets/editors.d @@ -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();