From d0057121ef8d654577af9dab0205fd3b32711c23 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 18 Feb 2017 21:28:45 -0500 Subject: [PATCH] Start implementing syntax highlighting optimizations --- cmd/micro/buffer.go | 2 ++ cmd/micro/cellview.go | 15 ++++++++++++--- cmd/micro/lineArray.go | 16 ++++++++++++++++ cmd/micro/view.go | 4 ---- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index ed703cf1..bf58fe50 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -47,6 +47,7 @@ type Buffer struct { syntaxDef *highlight.Def highlighter *highlight.Highlighter + matches []highlight.LineMatch // Buffer local settings Settings map[string]interface{} @@ -189,6 +190,7 @@ func (b *Buffer) UpdateRules() { if b.highlighter == nil || b.Settings["filetype"].(string) != b.syntaxDef.FileType { b.Settings["filetype"] = b.syntaxDef.FileType b.highlighter = highlight.NewHighlighter(b.syntaxDef) + b.matches = b.highlighter.Highlight(b, 0) } } diff --git a/cmd/micro/cellview.go b/cmd/micro/cellview.go index 1dae5622..5d03ade3 100644 --- a/cmd/micro/cellview.go +++ b/cmd/micro/cellview.go @@ -1,6 +1,8 @@ package main import ( + "time" + "github.com/mattn/go-runewidth" "github.com/zyedidia/tcell" ) @@ -45,7 +47,14 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) { softwrap := buf.Settings["softwrap"].(bool) indentchar := []rune(buf.Settings["indentchar"].(string))[0] - matches := buf.highlighter.Highlight(buf.String()) + start := buf.Cursor.Y + startTime := time.Now() + matches := buf.highlighter.ReHighlight(buf, start) + elapsed := time.Since(startTime) + for i, m := range matches { + buf.matches[start+i] = m + } + messenger.Message("Rehighlighted ", len(matches), " lines in ", elapsed) c.lines = make([][]*Char, 0) @@ -81,7 +90,7 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) { if colN >= len(line) { break } - if group, ok := matches[lineN][colN]; ok { + if group, ok := buf.matches[lineN][colN]; ok { curStyle = GetColor(group) } @@ -116,7 +125,7 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) { } } - if group, ok := matches[lineN][len(line)]; ok { + if group, ok := buf.matches[lineN][len(line)]; ok { curStyle = GetColor(group) } diff --git a/cmd/micro/lineArray.go b/cmd/micro/lineArray.go index b609837b..e34d8ae6 100644 --- a/cmd/micro/lineArray.go +++ b/cmd/micro/lineArray.go @@ -173,3 +173,19 @@ func (la *LineArray) Substr(start, end Loc) string { str += string(la.lines[end.Y].data[:endX]) return str } + +func (la *LineArray) LineData() [][]byte { + lines := make([][]byte, len(la.lines)) + for i, l := range la.lines { + lines[i] = l.data + } + return lines +} + +func (la *LineArray) State(lineN int) highlight.State { + return la.lines[lineN].state +} + +func (la *LineArray) SetState(lineN int, s highlight.State) { + la.lines[lineN].state = s +} diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 59670b2c..ff7bb666 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -6,7 +6,6 @@ import ( "time" "github.com/mitchellh/go-homedir" - "github.com/zyedidia/highlight" "github.com/zyedidia/tcell" ) @@ -87,9 +86,6 @@ type View struct { // Same here, just to keep track for mouse move events tripleClick bool - // Syntax highlighting matches - matches []highlight.LineMatch - cellview *CellView splitNode *LeafNode