Start implementing syntax highlighting optimizations

This commit is contained in:
Zachary Yedidia
2017-02-18 21:28:45 -05:00
parent 18c4196354
commit d0057121ef
4 changed files with 30 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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