8 func sliceStart(slc []byte, index int) []byte {
14 return slc[totalSize:]
17 _, _, size := DecodeCharacter(slc[totalSize:])
22 return slc[totalSize:]
25 func sliceEnd(slc []byte, index int) []byte {
31 return slc[:totalSize]
34 _, _, size := DecodeCharacter(slc[totalSize:])
39 return slc[:totalSize]
42 // RunePos returns the rune index of a given byte index
43 // This could cause problems if the byte index is between code points
44 func runePos(p int, str []byte) int {
49 return CharacterCount(str)
51 return CharacterCount(str[:p])
54 func combineLineMatch(src, dst LineMatch) LineMatch {
55 for k, v := range src {
56 if g, ok := dst[k]; ok {
67 // A State represents the region at the end of a line
70 var EmptyDef = Def{nil, &rules{}}
72 // LineStates is an interface for a buffer-like object which can also store the states and matches for every line
73 type LineStates interface {
74 LineBytes(n int) []byte
76 State(lineN int) State
77 SetState(lineN int, s State)
78 SetMatch(lineN int, m LineMatch)
81 // A Highlighter contains the information needed to highlight a string
82 type Highlighter struct {
87 // NewHighlighter returns a new highlighter from the given syntax definition
88 func NewHighlighter(def *Def) *Highlighter {
94 // LineMatch represents the syntax highlighting matches for one line. Each index where the coloring is changed is marked with that
95 // color's group (represented as one byte)
96 type LineMatch map[int]Group
98 func findIndex(regex *regexp.Regexp, skip *regexp.Regexp, str []byte, canMatchStart, canMatchEnd bool) []int {
99 regexStr := regex.String()
100 if strings.Contains(regexStr, "^") {
105 if strings.Contains(regexStr, "$") {
113 strbytes = skip.ReplaceAllFunc(str, func(match []byte) []byte {
114 res := make([]byte, CharacterCount(match))
121 match := regex.FindIndex(strbytes)
125 // return []int{match.Index, match.Index + match.Length}
126 return []int{runePos(match[0], str), runePos(match[1], str)}
129 func findAllIndex(regex *regexp.Regexp, str []byte, canMatchStart, canMatchEnd bool) [][]int {
130 regexStr := regex.String()
131 if strings.Contains(regexStr, "^") {
136 if strings.Contains(regexStr, "$") {
141 matches := regex.FindAllIndex(str, -1)
142 for i, m := range matches {
143 matches[i][0] = runePos(m[0], str)
144 matches[i][1] = runePos(m[1], str)
149 func (h *Highlighter) highlightRegion(highlights LineMatch, start int, canMatchEnd bool, lineNum int, line []byte, curRegion *region, statesOnly bool) LineMatch {
150 lineLen := CharacterCount(line)
153 if _, ok := highlights[0]; !ok {
154 highlights[0] = curRegion.group
159 loc := findIndex(curRegion.end, curRegion.skip, line, start == 0, canMatchEnd)
162 highlights[start+loc[0]] = curRegion.limitGroup
164 if curRegion.parent == nil {
166 highlights[start+loc[1]] = 0
167 h.highlightRegion(highlights, start, false, lineNum, sliceEnd(line, loc[0]), curRegion, statesOnly)
169 h.highlightEmptyRegion(highlights, start+loc[1], canMatchEnd, lineNum, sliceStart(line, loc[1]), statesOnly)
173 highlights[start+loc[1]] = curRegion.parent.group
174 h.highlightRegion(highlights, start, false, lineNum, sliceEnd(line, loc[0]), curRegion, statesOnly)
176 h.highlightRegion(highlights, start+loc[1], canMatchEnd, lineNum, sliceStart(line, loc[1]), curRegion.parent, statesOnly)
182 h.lastRegion = curRegion
188 firstLoc := []int{lineLen, 0}
190 var firstRegion *region
191 for _, r := range curRegion.rules.regions {
192 loc := findIndex(r.start, nil, line, start == 0, canMatchEnd)
194 if loc[0] < firstLoc[0] {
200 if firstLoc[0] != lineLen {
202 highlights[start+firstLoc[0]] = firstRegion.limitGroup
204 h.highlightRegion(highlights, start, false, lineNum, sliceEnd(line, firstLoc[0]), curRegion, statesOnly)
205 h.highlightRegion(highlights, start+firstLoc[1], canMatchEnd, lineNum, sliceStart(line, firstLoc[1]), firstRegion, statesOnly)
210 fullHighlights := make([]Group, lineLen)
211 for i := 0; i < len(fullHighlights); i++ {
212 fullHighlights[i] = curRegion.group
215 for _, p := range curRegion.rules.patterns {
216 matches := findAllIndex(p.regex, line, start == 0, canMatchEnd)
217 for _, m := range matches {
218 for i := m[0]; i < m[1]; i++ {
219 fullHighlights[i] = p.group
223 for i, h := range fullHighlights {
224 if i == 0 || h != fullHighlights[i-1] {
225 highlights[start+i] = h
231 h.lastRegion = curRegion
237 func (h *Highlighter) highlightEmptyRegion(highlights LineMatch, start int, canMatchEnd bool, lineNum int, line []byte, statesOnly bool) LineMatch {
238 lineLen := CharacterCount(line)
246 firstLoc := []int{lineLen, 0}
247 var firstRegion *region
248 for _, r := range h.Def.rules.regions {
249 loc := findIndex(r.start, nil, line, start == 0, canMatchEnd)
251 if loc[0] < firstLoc[0] {
257 if firstLoc[0] != lineLen {
259 highlights[start+firstLoc[0]] = firstRegion.limitGroup
261 h.highlightEmptyRegion(highlights, start, false, lineNum, sliceEnd(line, firstLoc[0]), statesOnly)
262 h.highlightRegion(highlights, start+firstLoc[1], canMatchEnd, lineNum, sliceStart(line, firstLoc[1]), firstRegion, statesOnly)
274 fullHighlights := make([]Group, len(line))
275 for _, p := range h.Def.rules.patterns {
276 matches := findAllIndex(p.regex, line, start == 0, canMatchEnd)
277 for _, m := range matches {
278 for i := m[0]; i < m[1]; i++ {
279 fullHighlights[i] = p.group
283 for i, h := range fullHighlights {
284 if i == 0 || h != fullHighlights[i-1] {
285 // if _, ok := highlights[start+i]; !ok {
286 highlights[start+i] = h
298 // HighlightString syntax highlights a string
299 // Use this function for simple syntax highlighting and use the other functions for
300 // more advanced syntax highlighting. They are optimized for quick rehighlighting of the same
301 // text with minor changes made
302 func (h *Highlighter) HighlightString(input string) []LineMatch {
303 lines := strings.Split(input, "\n")
304 var lineMatches []LineMatch
306 for i := 0; i < len(lines); i++ {
307 line := []byte(lines[i])
308 highlights := make(LineMatch)
310 if i == 0 || h.lastRegion == nil {
311 lineMatches = append(lineMatches, h.highlightEmptyRegion(highlights, 0, true, i, line, false))
313 lineMatches = append(lineMatches, h.highlightRegion(highlights, 0, true, i, line, h.lastRegion, false))
320 // HighlightStates correctly sets all states for the buffer
321 func (h *Highlighter) HighlightStates(input LineStates) {
322 for i := 0; i < input.LinesNum(); i++ {
323 line := input.LineBytes(i)
324 // highlights := make(LineMatch)
326 if i == 0 || h.lastRegion == nil {
327 h.highlightEmptyRegion(nil, 0, true, i, line, true)
329 h.highlightRegion(nil, 0, true, i, line, h.lastRegion, true)
332 curState := h.lastRegion
334 input.SetState(i, curState)
338 // HighlightMatches sets the matches for each line from startline to endline
339 // It sets all other matches in the buffer to nil to conserve memory
340 // This assumes that all the states are set correctly
341 func (h *Highlighter) HighlightMatches(input LineStates, startline, endline int) {
342 for i := startline; i <= endline; i++ {
343 if i >= input.LinesNum() {
347 line := input.LineBytes(i)
348 highlights := make(LineMatch)
351 if i == 0 || input.State(i-1) == nil {
352 match = h.highlightEmptyRegion(highlights, 0, true, i, line, false)
354 match = h.highlightRegion(highlights, 0, true, i, line, input.State(i-1), false)
357 input.SetMatch(i, match)
361 // ReHighlightStates will scan down from `startline` and set the appropriate end of line state
362 // for each line until it comes across a line whose state does not change
363 // returns the number of the final line
364 func (h *Highlighter) ReHighlightStates(input LineStates, startline int) int {
365 // lines := input.LineData()
369 h.lastRegion = input.State(startline - 1)
371 for i := startline; i < input.LinesNum(); i++ {
372 line := input.LineBytes(i)
373 // highlights := make(LineMatch)
375 // var match LineMatch
376 if i == 0 || h.lastRegion == nil {
377 h.highlightEmptyRegion(nil, 0, true, i, line, true)
379 h.highlightRegion(nil, 0, true, i, line, h.lastRegion, true)
381 curState := h.lastRegion
382 lastState := input.State(i)
384 input.SetState(i, curState)
386 if curState == lastState {
391 return input.LinesNum() - 1
394 // ReHighlightLine will rehighlight the state and match for a single line
395 func (h *Highlighter) ReHighlightLine(input LineStates, lineN int) {
396 line := input.LineBytes(lineN)
397 highlights := make(LineMatch)
401 h.lastRegion = input.State(lineN - 1)
405 if lineN == 0 || h.lastRegion == nil {
406 match = h.highlightEmptyRegion(highlights, 0, true, lineN, line, false)
408 match = h.highlightRegion(highlights, 0, true, lineN, line, h.lastRegion, false)
410 curState := h.lastRegion
412 input.SetMatch(lineN, match)
413 input.SetState(lineN, curState)