Store highlighting matches in each line

This commit is contained in:
Zachary Yedidia
2017-02-19 11:16:01 -05:00
parent 7bb61307e0
commit 7fe2b8ef2f
3 changed files with 33 additions and 17 deletions

View File

@@ -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)
}
}

View File

@@ -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)
}

View File

@@ -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
}