From 7fe2b8ef2f4d1a647eb1b4c494a7ced0eea73b94 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 19 Feb 2017 11:16:01 -0500 Subject: [PATCH] Store highlighting matches in each line --- cmd/micro/buffer.go | 3 +-- cmd/micro/cellview.go | 23 +++++++++++++---------- cmd/micro/lineArray.go | 24 +++++++++++++++++++----- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index bf58fe50..83e1787a 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -47,7 +47,6 @@ type Buffer struct { syntaxDef *highlight.Def highlighter *highlight.Highlighter - matches []highlight.LineMatch // Buffer local settings Settings map[string]interface{} @@ -190,7 +189,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) + b.highlighter.Highlight(b, 0) } } diff --git a/cmd/micro/cellview.go b/cmd/micro/cellview.go index 33d8f2cf..83562124 100644 --- a/cmd/micro/cellview.go +++ b/cmd/micro/cellview.go @@ -1,8 +1,6 @@ package main import ( - "time" - "github.com/mattn/go-runewidth" "github.com/zyedidia/tcell" ) @@ -48,13 +46,18 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) { indentchar := []rune(buf.Settings["indentchar"].(string))[0] 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 + // startTime := time.Now() + if start > 0 && buf.lines[start-1].rehighlight { + buf.highlighter.ReHighlightLine(buf, start-1) + buf.lines[start-1].rehighlight = false } - messenger.Message("Rehighlighted ", len(matches), " lines in ", elapsed) + + 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) @@ -90,7 +93,7 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) { if colN >= len(line) { break } - if group, ok := buf.matches[lineN][colN]; ok { + if group, ok := buf.Match(lineN)[colN]; ok { curStyle = GetColor(group) } @@ -124,7 +127,7 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) { } } - if group, ok := buf.matches[lineN][len(line)]; ok { + if group, ok := buf.Match(lineN)[len(line)]; ok { curStyle = GetColor(group) } diff --git a/cmd/micro/lineArray.go b/cmd/micro/lineArray.go index e34d8ae6..b04e3537 100644 --- a/cmd/micro/lineArray.go +++ b/cmd/micro/lineArray.go @@ -32,7 +32,9 @@ func runeToByteIndex(n int, txt []byte) int { type Line struct { data []byte - state highlight.State + state highlight.State + match highlight.LineMatch + rehighlight bool } // A LineArray simply stores and array of lines and makes it easy to insert @@ -51,12 +53,12 @@ func NewLineArray(reader io.Reader) *LineArray { data, err := br.ReadBytes('\n') if err != nil { if err == io.EOF { - la.lines = append(la.lines, Line{data[:len(data)], nil}) + la.lines = append(la.lines, Line{data[:len(data)], nil, nil, false}) } // Last line was read break } else { - la.lines = append(la.lines, Line{data[:len(data)-1], nil}) + la.lines = append(la.lines, Line{data[:len(data)-1], nil, nil, false}) } i++ } @@ -78,9 +80,9 @@ func (la *LineArray) String() string { // NewlineBelow adds a newline below the given line number func (la *LineArray) NewlineBelow(y int) { - la.lines = append(la.lines, Line{[]byte(" "), nil}) + la.lines = append(la.lines, Line{[]byte(" "), nil, nil, false}) copy(la.lines[y+2:], la.lines[y+1:]) - la.lines[y+1] = Line{[]byte(""), nil} + la.lines[y+1] = Line{[]byte(""), nil, nil, false} } // inserts a byte array at a given location @@ -117,6 +119,10 @@ func (la *LineArray) Split(pos Loc) { la.NewlineBelow(pos.Y) la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:]) la.lines[pos.Y+1].state = la.lines[pos.Y].state + la.lines[pos.Y].state = nil + la.lines[pos.Y].match = nil + la.lines[pos.Y+1].match = nil + la.lines[pos.Y].rehighlight = true la.DeleteToEnd(Loc{pos.X, pos.Y}) } @@ -189,3 +195,11 @@ func (la *LineArray) State(lineN int) highlight.State { func (la *LineArray) SetState(lineN int, s highlight.State) { la.lines[lineN].state = s } + +func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) { + la.lines[lineN].match = m +} + +func (la *LineArray) Match(lineN int) highlight.LineMatch { + return la.lines[lineN].match +}