9 // RunePos returns the rune index of a given byte index
10 // This could cause problems if the byte index is between code points
11 func runePos(p int, str string) int {
16 return utf8.RuneCountInString(str)
18 return utf8.RuneCountInString(str[:p])
21 func combineLineMatch(src, dst LineMatch) LineMatch {
22 for k, v := range src {
23 if g, ok := dst[k]; ok {
34 // A State represents the region at the end of a line
37 // LineStates is an interface for a buffer-like object which can also store the states and matches for every line
38 type LineStates interface {
41 State(lineN int) State
42 SetState(lineN int, s State)
43 SetMatch(lineN int, m LineMatch)
46 // A Highlighter contains the information needed to highlight a string
47 type Highlighter struct {
52 // NewHighlighter returns a new highlighter from the given syntax definition
53 func NewHighlighter(def *Def) *Highlighter {
59 // LineMatch represents the syntax highlighting matches for one line. Each index where the coloring is changed is marked with that
60 // color's group (represented as one byte)
61 type LineMatch map[int]Group
63 func findIndex(regex *regexp.Regexp, skip *regexp.Regexp, str []rune, canMatchStart, canMatchEnd bool) []int {
64 regexStr := regex.String()
65 if strings.Contains(regexStr, "^") {
70 if strings.Contains(regexStr, "$") {
78 strbytes = skip.ReplaceAllFunc(strbytes, func(match []byte) []byte {
79 res := make([]byte, utf8.RuneCount(match))
83 strbytes = []byte(string(str))
86 match := regex.FindIndex(strbytes)
90 // return []int{match.Index, match.Index + match.Length}
91 return []int{runePos(match[0], string(str)), runePos(match[1], string(str))}
94 func findAllIndex(regex *regexp.Regexp, str []rune, canMatchStart, canMatchEnd bool) [][]int {
95 regexStr := regex.String()
96 if strings.Contains(regexStr, "^") {
101 if strings.Contains(regexStr, "$") {
106 matches := regex.FindAllIndex([]byte(string(str)), -1)
107 for i, m := range matches {
108 matches[i][0] = runePos(m[0], string(str))
109 matches[i][1] = runePos(m[1], string(str))
114 func (h *Highlighter) highlightRegion(highlights LineMatch, start int, canMatchEnd bool, lineNum int, line []rune, curRegion *region, statesOnly bool) LineMatch {
115 // highlights := make(LineMatch)
119 highlights[0] = curRegion.group
123 loc := findIndex(curRegion.end, curRegion.skip, line, start == 0, canMatchEnd)
126 highlights[start+loc[1]-1] = curRegion.group
128 if curRegion.parent == nil {
130 highlights[start+loc[1]] = 0
131 h.highlightRegion(highlights, start, false, lineNum, line[:loc[0]], curRegion, statesOnly)
133 h.highlightEmptyRegion(highlights, start+loc[1], canMatchEnd, lineNum, line[loc[1]:], statesOnly)
137 highlights[start+loc[1]] = curRegion.parent.group
138 h.highlightRegion(highlights, start, false, lineNum, line[:loc[0]], curRegion, statesOnly)
140 h.highlightRegion(highlights, start+loc[1], canMatchEnd, lineNum, line[loc[1]:], curRegion.parent, statesOnly)
144 if len(line) == 0 || statesOnly {
146 h.lastRegion = curRegion
152 firstLoc := []int{len(line), 0}
154 var firstRegion *region
155 for _, r := range curRegion.rules.regions {
156 loc := findIndex(r.start, nil, line, start == 0, canMatchEnd)
158 if loc[0] < firstLoc[0] {
164 if firstLoc[0] != len(line) {
165 highlights[start+firstLoc[0]] = firstRegion.group
166 h.highlightRegion(highlights, start, false, lineNum, line[:firstLoc[0]], curRegion, statesOnly)
167 h.highlightRegion(highlights, start+firstLoc[1], canMatchEnd, lineNum, line[firstLoc[1]:], firstRegion, statesOnly)
171 fullHighlights := make([]Group, len([]rune(string(line))))
172 for i := 0; i < len(fullHighlights); i++ {
173 fullHighlights[i] = curRegion.group
176 for _, p := range curRegion.rules.patterns {
177 matches := findAllIndex(p.regex, line, start == 0, canMatchEnd)
178 for _, m := range matches {
179 for i := m[0]; i < m[1]; i++ {
180 fullHighlights[i] = p.group
184 for i, h := range fullHighlights {
185 if i == 0 || h != fullHighlights[i-1] {
186 // if _, ok := highlights[start+i]; !ok {
187 highlights[start+i] = h
193 h.lastRegion = curRegion
199 func (h *Highlighter) highlightEmptyRegion(highlights LineMatch, start int, canMatchEnd bool, lineNum int, line []rune, statesOnly bool) LineMatch {
207 firstLoc := []int{len(line), 0}
208 var firstRegion *region
209 for _, r := range h.Def.rules.regions {
210 loc := findIndex(r.start, nil, line, start == 0, canMatchEnd)
212 if loc[0] < firstLoc[0] {
218 if firstLoc[0] != len(line) {
220 highlights[start+firstLoc[0]] = firstRegion.group
222 h.highlightEmptyRegion(highlights, start, false, lineNum, line[:firstLoc[0]], statesOnly)
223 h.highlightRegion(highlights, start+firstLoc[1], canMatchEnd, lineNum, line[firstLoc[1]:], firstRegion, statesOnly)
235 fullHighlights := make([]Group, len(line))
236 for _, p := range h.Def.rules.patterns {
237 matches := findAllIndex(p.regex, line, start == 0, canMatchEnd)
238 for _, m := range matches {
239 for i := m[0]; i < m[1]; i++ {
240 fullHighlights[i] = p.group
244 for i, h := range fullHighlights {
245 if i == 0 || h != fullHighlights[i-1] {
246 // if _, ok := highlights[start+i]; !ok {
247 highlights[start+i] = h
259 // HighlightString syntax highlights a string
260 // Use this function for simple syntax highlighting and use the other functions for
261 // more advanced syntax highlighting. They are optimized for quick rehighlighting of the same
262 // text with minor changes made
263 func (h *Highlighter) HighlightString(input string) []LineMatch {
264 lines := strings.Split(input, "\n")
265 var lineMatches []LineMatch
267 for i := 0; i < len(lines); i++ {
268 line := []rune(lines[i])
269 highlights := make(LineMatch)
271 if i == 0 || h.lastRegion == nil {
272 lineMatches = append(lineMatches, h.highlightEmptyRegion(highlights, 0, true, i, line, false))
274 lineMatches = append(lineMatches, h.highlightRegion(highlights, 0, true, i, line, h.lastRegion, false))
281 // HighlightStates correctly sets all states for the buffer
282 func (h *Highlighter) HighlightStates(input LineStates) {
283 for i := 0; i < input.LinesNum(); i++ {
284 line := []rune(input.Line(i))
285 // highlights := make(LineMatch)
287 if i == 0 || h.lastRegion == nil {
288 h.highlightEmptyRegion(nil, 0, true, i, line, true)
290 h.highlightRegion(nil, 0, true, i, line, h.lastRegion, true)
293 curState := h.lastRegion
295 input.SetState(i, curState)
299 // HighlightMatches sets the matches for each line in between startline and endline
300 // It sets all other matches in the buffer to nil to conserve memory
301 // This assumes that all the states are set correctly
302 func (h *Highlighter) HighlightMatches(input LineStates, startline, endline int) {
303 for i := startline; i < endline; i++ {
304 if i >= input.LinesNum() {
308 line := []rune(input.Line(i))
309 highlights := make(LineMatch)
312 if i == 0 || input.State(i-1) == nil {
313 match = h.highlightEmptyRegion(highlights, 0, true, i, line, false)
315 match = h.highlightRegion(highlights, 0, true, i, line, input.State(i-1), false)
318 input.SetMatch(i, match)
322 // ReHighlightStates will scan down from `startline` and set the appropriate end of line state
323 // for each line until it comes across the same state in two consecutive lines
324 func (h *Highlighter) ReHighlightStates(input LineStates, startline int) {
325 // lines := input.LineData()
329 h.lastRegion = input.State(startline - 1)
331 for i := startline; i < input.LinesNum(); i++ {
332 line := []rune(input.Line(i))
333 // highlights := make(LineMatch)
335 // var match LineMatch
336 if i == 0 || h.lastRegion == nil {
337 h.highlightEmptyRegion(nil, 0, true, i, line, true)
339 h.highlightRegion(nil, 0, true, i, line, h.lastRegion, true)
341 curState := h.lastRegion
342 lastState := input.State(i)
344 input.SetState(i, curState)
346 if curState == lastState {
352 // ReHighlightLine will rehighlight the state and match for a single line
353 func (h *Highlighter) ReHighlightLine(input LineStates, lineN int) {
354 line := []rune(input.Line(lineN))
355 highlights := make(LineMatch)
359 h.lastRegion = input.State(lineN - 1)
363 if lineN == 0 || h.lastRegion == nil {
364 match = h.highlightEmptyRegion(highlights, 0, true, lineN, line, false)
366 match = h.highlightRegion(highlights, 0, true, lineN, line, h.lastRegion, false)
368 curState := h.lastRegion
370 input.SetMatch(lineN, match)
371 input.SetState(lineN, curState)