8 func combineLineMatch(src, dst LineMatch) LineMatch {
9 for k, v := range src {
10 if g, ok := dst[k]; ok {
23 type LineStates interface {
25 State(lineN int) State
26 SetState(lineN int, s State)
27 SetMatch(lineN int, m LineMatch)
30 type Highlighter struct {
35 func NewHighlighter(def *Def) *Highlighter {
41 type LineMatch map[int]string
43 func FindIndex(regex *regexp.Regexp, str []byte, canMatchStart, canMatchEnd bool) []int {
44 regexStr := regex.String()
45 if strings.Contains(regexStr, "^") {
50 if strings.Contains(regexStr, "$") {
55 return regex.FindIndex(str)
58 func FindAllIndex(regex *regexp.Regexp, str []byte, canMatchStart, canMatchEnd bool) [][]int {
59 regexStr := regex.String()
60 if strings.Contains(regexStr, "^") {
65 if strings.Contains(regexStr, "$") {
70 return regex.FindAllIndex(str, -1)
73 func (h *Highlighter) highlightRegion(start int, canMatchEnd bool, lineNum int, line []byte, region *Region) LineMatch {
74 highlights := make(LineMatch)
76 loc := FindIndex(region.end, line, start == 0, canMatchEnd)
78 if region.parent == nil {
79 highlights[start+loc[1]] = ""
80 return combineLineMatch(highlights,
81 combineLineMatch(h.highlightRegion(start, false, lineNum, line[:loc[0]], region),
82 h.highlightEmptyRegion(start+loc[1], canMatchEnd, lineNum, line[loc[1]:])))
84 highlights[start+loc[1]] = region.parent.group
85 return combineLineMatch(highlights,
86 combineLineMatch(h.highlightRegion(start, false, lineNum, line[:loc[0]], region),
87 h.highlightRegion(start+loc[1], canMatchEnd, lineNum, line[loc[1]:], region.parent)))
98 firstLoc := []int{len(line), 0}
99 var firstRegion *Region
100 for _, r := range region.rules.regions {
101 loc := FindIndex(r.start, line, start == 0, canMatchEnd)
103 if loc[0] < firstLoc[0] {
109 if firstLoc[0] != len(line) {
110 highlights[start+firstLoc[0]] = firstRegion.group
111 return combineLineMatch(highlights,
112 combineLineMatch(h.highlightRegion(start, false, lineNum, line[:firstLoc[0]], region),
113 h.highlightRegion(start+firstLoc[1], canMatchEnd, lineNum, line[firstLoc[1]:], firstRegion)))
116 for _, p := range region.rules.patterns {
117 matches := FindAllIndex(p.regex, line, start == 0, canMatchEnd)
118 for _, m := range matches {
119 highlights[start+m[0]] = p.group
120 if _, ok := highlights[start+m[1]]; !ok {
121 highlights[start+m[1]] = region.group
127 h.lastRegion = region
133 func (h *Highlighter) highlightEmptyRegion(start int, canMatchEnd bool, lineNum int, line []byte) LineMatch {
134 highlights := make(LineMatch)
142 firstLoc := []int{len(line), 0}
143 var firstRegion *Region
144 for _, r := range h.def.rules.regions {
145 loc := FindIndex(r.start, line, start == 0, canMatchEnd)
147 if loc[0] < firstLoc[0] {
153 if firstLoc[0] != len(line) {
154 highlights[start+firstLoc[0]] = firstRegion.group
155 return combineLineMatch(highlights,
156 combineLineMatch(h.highlightEmptyRegion(start, false, lineNum, line[:firstLoc[0]]),
157 h.highlightRegion(start+firstLoc[1], canMatchEnd, lineNum, line[firstLoc[1]:], firstRegion)))
160 for _, p := range h.def.rules.patterns {
161 matches := FindAllIndex(p.regex, line, start == 0, canMatchEnd)
162 for _, m := range matches {
163 highlights[start+m[0]] = p.group
164 if _, ok := highlights[start+m[1]]; !ok {
165 highlights[start+m[1]] = ""
177 func (h *Highlighter) HighlightString(input string) []LineMatch {
178 lines := strings.Split(input, "\n")
179 var lineMatches []LineMatch
181 for i := 0; i < len(lines); i++ {
182 line := []byte(lines[i])
184 if i == 0 || h.lastRegion == nil {
185 lineMatches = append(lineMatches, h.highlightEmptyRegion(0, true, i, line))
187 lineMatches = append(lineMatches, h.highlightRegion(0, true, i, line, h.lastRegion))
194 func (h *Highlighter) Highlight(input LineStates, startline int) {
195 lines := input.LineData()
197 for i := startline; i < len(lines); i++ {
198 line := []byte(lines[i])
201 if i == 0 || h.lastRegion == nil {
202 match = h.highlightEmptyRegion(0, true, i, line)
204 match = h.highlightRegion(0, true, i, line, h.lastRegion)
207 curState := h.lastRegion
209 input.SetMatch(i, match)
210 input.SetState(i, curState)
214 func (h *Highlighter) ReHighlightLine(input LineStates, lineN int) {
215 lines := input.LineData()
217 line := []byte(lines[lineN])
221 h.lastRegion = input.State(lineN - 1)
225 if lineN == 0 || h.lastRegion == nil {
226 match = h.highlightEmptyRegion(0, true, lineN, line)
228 match = h.highlightRegion(0, true, lineN, line, h.lastRegion)
230 curState := h.lastRegion
232 input.SetMatch(lineN, match)
233 input.SetState(lineN, curState)
236 func (h *Highlighter) ReHighlight(input LineStates, startline int) {
237 lines := input.LineData()
241 h.lastRegion = input.State(startline - 1)
243 for i := startline; i < len(lines); i++ {
244 line := []byte(lines[i])
247 if i == 0 || h.lastRegion == nil {
248 match = h.highlightEmptyRegion(0, true, i, line)
250 match = h.highlightRegion(0, true, i, line, h.lastRegion)
252 curState := h.lastRegion
253 lastState := input.State(i)
255 input.SetMatch(i, match)
256 input.SetState(i, curState)
258 if curState == lastState {