]> git.lizzy.rs Git - micro.git/blobdiff - pkg/highlight/highlighter.go
Merge branch 'autosu' of https://github.com/seitokaichou/micro into seitokaichou...
[micro.git] / pkg / highlight / highlighter.go
index e736abdbfab486d55793e39fd8c6b92dff01a4da..ae7c71b3d4b841b7574701f1bc0fe01882718eb0 100644 (file)
@@ -68,6 +68,8 @@ func combineLineMatch(src, dst LineMatch) LineMatch {
 // A State represents the region at the end of a line
 type State *region
 
+var EmptyDef = Def{nil, &rules{}}
+
 // LineStates is an interface for a buffer-like object which can also store the states and matches for every line
 type LineStates interface {
        LineBytes(n int) []byte
@@ -176,7 +178,7 @@ func (h *Highlighter) highlightRegion(highlights LineMatch, start int, canMatchE
                return highlights
        }
 
-       if lineLen == 0 || statesOnly {
+       if lineLen == 0 {
                if canMatchEnd {
                        h.lastRegion = curRegion
                }
@@ -197,28 +199,32 @@ func (h *Highlighter) highlightRegion(highlights LineMatch, start int, canMatchE
                }
        }
        if firstLoc[0] != lineLen {
-               highlights[start+firstLoc[0]] = firstRegion.limitGroup
+               if !statesOnly {
+                       highlights[start+firstLoc[0]] = firstRegion.limitGroup
+               }
                h.highlightRegion(highlights, start, false, lineNum, sliceEnd(line, firstLoc[0]), curRegion, statesOnly)
                h.highlightRegion(highlights, start+firstLoc[1], canMatchEnd, lineNum, sliceStart(line, firstLoc[1]), firstRegion, statesOnly)
                return highlights
        }
 
-       fullHighlights := make([]Group, lineLen)
-       for i := 0; i < len(fullHighlights); i++ {
-               fullHighlights[i] = curRegion.group
-       }
+       if !statesOnly {
+               fullHighlights := make([]Group, lineLen)
+               for i := 0; i < len(fullHighlights); i++ {
+                       fullHighlights[i] = curRegion.group
+               }
 
-       for _, p := range curRegion.rules.patterns {
-               matches := findAllIndex(p.regex, line, start == 0, canMatchEnd)
-               for _, m := range matches {
-                       for i := m[0]; i < m[1]; i++ {
-                               fullHighlights[i] = p.group
+               for _, p := range curRegion.rules.patterns {
+                       matches := findAllIndex(p.regex, line, start == 0, canMatchEnd)
+                       for _, m := range matches {
+                               for i := m[0]; i < m[1]; i++ {
+                                       fullHighlights[i] = p.group
+                               }
                        }
                }
-       }
-       for i, h := range fullHighlights {
-               if i == 0 || h != fullHighlights[i-1] {
-                       highlights[start+i] = h
+               for i, h := range fullHighlights {
+                       if i == 0 || h != fullHighlights[i-1] {
+                               highlights[start+i] = h
+                       }
                }
        }
 
@@ -354,8 +360,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
@@ -378,9 +385,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