diff --git a/internal/buffer/buffer.go b/internal/buffer/buffer.go index 22680264..9e3cfcc9 100644 --- a/internal/buffer/buffer.go +++ b/internal/buffer/buffer.go @@ -278,7 +278,7 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT screen.TermMessage(err) } - b.Modifications = make([]Loc, 10) + b.Modifications = make([]Loc, 0, 10) OpenBuffers = append(OpenBuffers, b) @@ -615,6 +615,7 @@ func (b *Buffer) UpdateRules() { b.Highlighter = highlight.NewHighlighter(b.SyntaxDef) if b.Settings["syntax"].(bool) { b.Highlighter.HighlightStates(b) + b.Highlighter.HighlightMatches(b, 0, b.End().Y) } } } diff --git a/internal/display/bufwindow.go b/internal/display/bufwindow.go index 47d9983a..8b90588c 100644 --- a/internal/display/bufwindow.go +++ b/internal/display/bufwindow.go @@ -358,6 +358,10 @@ func (w *BufWindow) showCursor(x, y int, main bool) { func (w *BufWindow) displayBuffer() { b := w.Buf + if w.Height <= 0 || w.Width <= 0 { + return + } + hasMessage := len(b.Messages) > 0 bufHeight := w.Height if w.drawStatus { @@ -371,16 +375,12 @@ func (w *BufWindow) displayBuffer() { if b.Settings["syntax"].(bool) && b.SyntaxDef != nil { for _, r := range b.Modifications { + final := -1 for i := r.X; i <= r.Y; i++ { - if i > 0 && b.Rehighlight(i-1) { - b.Highlighter.ReHighlightLine(b, i-1) - b.SetRehighlight(i-1, false) - } - - b.Highlighter.ReHighlightStates(b, i) + final = util.Max(b.Highlighter.ReHighlightStates(b, i), final) } + b.Highlighter.HighlightMatches(b, r.X, final+1) } - b.Highlighter.HighlightMatches(b, w.StartLine, w.StartLine+bufHeight) b.ClearModifications() } diff --git a/pkg/highlight/highlighter.go b/pkg/highlight/highlighter.go index 7dc471ee..ec8eeaee 100644 --- a/pkg/highlight/highlighter.go +++ b/pkg/highlight/highlighter.go @@ -358,8 +358,9 @@ func (h *Highlighter) HighlightMatches(input LineStates, startline, endline int) } // ReHighlightStates will scan down from `startline` and set the appropriate end of line state -// for each line until it comes across the same state in two consecutive lines -func (h *Highlighter) ReHighlightStates(input LineStates, startline int) { +// for each line until it comes across a line whose state does not change +// returns the number of the final line +func (h *Highlighter) ReHighlightStates(input LineStates, startline int) int { // lines := input.LineData() h.lastRegion = nil @@ -382,9 +383,11 @@ func (h *Highlighter) ReHighlightStates(input LineStates, startline int) { input.SetState(i, curState) if curState == lastState { - break + return i } } + + return input.LinesNum() - 1 } // ReHighlightLine will rehighlight the state and match for a single line