X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2Fhighlight%2Fhighlighter.go;h=2a7634612a70731538b9046465b1b66141daf01c;hb=342f3c223da0df38d9c4f79f4a8175c5c032adf0;hp=ef59d1f2ea1be02675b0634f62b52f96b037c213;hpb=59bf1a22604bf4a043ee27d02e758cd91eb08cc4;p=micro.git diff --git a/cmd/micro/highlight/highlighter.go b/cmd/micro/highlight/highlighter.go index ef59d1f2..2a763461 100644 --- a/cmd/micro/highlight/highlighter.go +++ b/cmd/micro/highlight/highlighter.go @@ -3,8 +3,21 @@ package highlight import ( "regexp" "strings" + "unicode/utf8" ) +// RunePos returns the rune index of a given byte index +// This could cause problems if the byte index is between code points +func runePos(p int, str string) int { + if p < 0 { + return 0 + } + if p >= len(str) { + return utf8.RuneCountInString(str) + } + return utf8.RuneCountInString(str[:p]) +} + func combineLineMatch(src, dst LineMatch) LineMatch { for k, v := range src { if g, ok := dst[k]; ok { @@ -19,11 +32,12 @@ func combineLineMatch(src, dst LineMatch) LineMatch { } // A State represents the region at the end of a line -type State *Region +type State *region // LineStates is an interface for a buffer-like object which can also store the states and matches for every line type LineStates interface { - LineData() [][]byte + Line(n int) string + LinesNum() int State(lineN int) State SetState(lineN int, s State) SetMatch(lineN int, m LineMatch) @@ -31,22 +45,22 @@ type LineStates interface { // A Highlighter contains the information needed to highlight a string type Highlighter struct { - lastRegion *Region - def *Def + lastRegion *region + Def *Def } // NewHighlighter returns a new highlighter from the given syntax definition func NewHighlighter(def *Def) *Highlighter { h := new(Highlighter) - h.def = def + h.Def = def return h } // LineMatch represents the syntax highlighting matches for one line. Each index where the coloring is changed is marked with that // color's group (represented as one byte) -type LineMatch map[int]uint8 +type LineMatch map[int]Group -func findIndex(regex *regexp.Regexp, str []byte, canMatchStart, canMatchEnd bool) []int { +func findIndex(regex *regexp.Regexp, skip *regexp.Regexp, str []rune, canMatchStart, canMatchEnd bool) []int { regexStr := regex.String() if strings.Contains(regexStr, "^") { if !canMatchStart { @@ -58,10 +72,26 @@ func findIndex(regex *regexp.Regexp, str []byte, canMatchStart, canMatchEnd bool return nil } } - return regex.FindIndex(str) + + var strbytes []byte + if skip != nil { + strbytes = skip.ReplaceAllFunc(strbytes, func(match []byte) []byte { + res := make([]byte, utf8.RuneCount(match)) + return res + }) + } else { + strbytes = []byte(string(str)) + } + + match := regex.FindIndex(strbytes) + if match == nil { + return nil + } + // return []int{match.Index, match.Index + match.Length} + return []int{runePos(match[0], string(str)), runePos(match[1], string(str))} } -func findAllIndex(regex *regexp.Regexp, str []byte, canMatchStart, canMatchEnd bool) [][]int { +func findAllIndex(regex *regexp.Regexp, str []rune, canMatchStart, canMatchEnd bool) [][]int { regexStr := regex.String() if strings.Contains(regexStr, "^") { if !canMatchStart { @@ -73,42 +103,57 @@ func findAllIndex(regex *regexp.Regexp, str []byte, canMatchStart, canMatchEnd b return nil } } - return regex.FindAllIndex(str, -1) + matches := regex.FindAllIndex([]byte(string(str)), -1) + for i, m := range matches { + matches[i][0] = runePos(m[0], string(str)) + matches[i][1] = runePos(m[1], string(str)) + } + return matches } -func (h *Highlighter) highlightRegion(start int, canMatchEnd bool, lineNum int, line []byte, region *Region) LineMatch { - highlights := make(LineMatch) +func (h *Highlighter) highlightRegion(highlights LineMatch, start int, canMatchEnd bool, lineNum int, line []rune, curRegion *region, statesOnly bool) LineMatch { + // highlights := make(LineMatch) if start == 0 { - highlights[0] = region.group + if !statesOnly { + highlights[0] = curRegion.group + } } - loc := findIndex(region.end, line, start == 0, canMatchEnd) + loc := findIndex(curRegion.end, curRegion.skip, line, start == 0, canMatchEnd) if loc != nil { - if region.parent == nil { - highlights[start+loc[1]] = 0 - return combineLineMatch(highlights, - combineLineMatch(h.highlightRegion(start, false, lineNum, line[:loc[0]], region), - h.highlightEmptyRegion(start+loc[1], canMatchEnd, lineNum, line[loc[1]:]))) + if !statesOnly { + highlights[start+loc[1]-1] = curRegion.group } - highlights[start+loc[1]] = region.parent.group - return combineLineMatch(highlights, - combineLineMatch(h.highlightRegion(start, false, lineNum, line[:loc[0]], region), - h.highlightRegion(start+loc[1], canMatchEnd, lineNum, line[loc[1]:], region.parent))) + if curRegion.parent == nil { + if !statesOnly { + highlights[start+loc[1]] = 0 + h.highlightRegion(highlights, start, false, lineNum, line[:loc[0]], curRegion, statesOnly) + } + h.highlightEmptyRegion(highlights, start+loc[1], canMatchEnd, lineNum, line[loc[1]:], statesOnly) + return highlights + } + if !statesOnly { + highlights[start+loc[1]] = curRegion.parent.group + h.highlightRegion(highlights, start, false, lineNum, line[:loc[0]], curRegion, statesOnly) + } + h.highlightRegion(highlights, start+loc[1], canMatchEnd, lineNum, line[loc[1]:], curRegion.parent, statesOnly) + return highlights } - if len(line) == 0 { + if len(line) == 0 || statesOnly { if canMatchEnd { - h.lastRegion = region + h.lastRegion = curRegion } return highlights } firstLoc := []int{len(line), 0} - var firstRegion *Region - for _, r := range region.rules.regions { - loc := findIndex(r.start, line, start == 0, canMatchEnd) + + var firstRegion *region + for _, r := range curRegion.rules.regions { + loc := findIndex(r.start, nil, line, start == 0, canMatchEnd) if loc != nil { if loc[0] < firstLoc[0] { firstLoc = loc @@ -118,30 +163,40 @@ func (h *Highlighter) highlightRegion(start int, canMatchEnd bool, lineNum int, } if firstLoc[0] != len(line) { highlights[start+firstLoc[0]] = firstRegion.group - return combineLineMatch(highlights, - combineLineMatch(h.highlightRegion(start, false, lineNum, line[:firstLoc[0]], region), - h.highlightRegion(start+firstLoc[1], canMatchEnd, lineNum, line[firstLoc[1]:], firstRegion))) + h.highlightRegion(highlights, start, false, lineNum, line[:firstLoc[0]], curRegion, statesOnly) + h.highlightRegion(highlights, start+firstLoc[1], canMatchEnd, lineNum, line[firstLoc[1]:], firstRegion, statesOnly) + return highlights + } + + fullHighlights := make([]Group, len([]rune(string(line)))) + for i := 0; i < len(fullHighlights); i++ { + fullHighlights[i] = curRegion.group } - for _, p := range region.rules.patterns { + for _, p := range curRegion.rules.patterns { matches := findAllIndex(p.regex, line, start == 0, canMatchEnd) for _, m := range matches { - highlights[start+m[0]] = p.group - if _, ok := highlights[start+m[1]]; !ok { - highlights[start+m[1]] = region.group + for i := m[0]; i < m[1]; i++ { + fullHighlights[i] = p.group + } + } + } + for i, h := range fullHighlights { + if i == 0 || h != fullHighlights[i-1] { + if _, ok := highlights[start+i]; !ok { + highlights[start+i] = h } } } if canMatchEnd { - h.lastRegion = region + h.lastRegion = curRegion } return highlights } -func (h *Highlighter) highlightEmptyRegion(start int, canMatchEnd bool, lineNum int, line []byte) LineMatch { - highlights := make(LineMatch) +func (h *Highlighter) highlightEmptyRegion(highlights LineMatch, start int, canMatchEnd bool, lineNum int, line []rune, statesOnly bool) LineMatch { if len(line) == 0 { if canMatchEnd { h.lastRegion = nil @@ -150,9 +205,9 @@ func (h *Highlighter) highlightEmptyRegion(start int, canMatchEnd bool, lineNum } firstLoc := []int{len(line), 0} - var firstRegion *Region - for _, r := range h.def.rules.regions { - loc := findIndex(r.start, line, start == 0, canMatchEnd) + var firstRegion *region + for _, r := range h.Def.rules.regions { + loc := findIndex(r.start, nil, line, start == 0, canMatchEnd) if loc != nil { if loc[0] < firstLoc[0] { firstLoc = loc @@ -161,18 +216,35 @@ func (h *Highlighter) highlightEmptyRegion(start int, canMatchEnd bool, lineNum } } if firstLoc[0] != len(line) { - highlights[start+firstLoc[0]] = firstRegion.group - return combineLineMatch(highlights, - combineLineMatch(h.highlightEmptyRegion(start, false, lineNum, line[:firstLoc[0]]), - h.highlightRegion(start+firstLoc[1], canMatchEnd, lineNum, line[firstLoc[1]:], firstRegion))) + if !statesOnly { + highlights[start+firstLoc[0]] = firstRegion.group + } + h.highlightEmptyRegion(highlights, start, false, lineNum, line[:firstLoc[0]], statesOnly) + h.highlightRegion(highlights, start+firstLoc[1], canMatchEnd, lineNum, line[firstLoc[1]:], firstRegion, statesOnly) + return highlights + } + + if statesOnly { + if canMatchEnd { + h.lastRegion = nil + } + + return highlights } - for _, p := range h.def.rules.patterns { + fullHighlights := make([]Group, len(line)) + for _, p := range h.Def.rules.patterns { matches := findAllIndex(p.regex, line, start == 0, canMatchEnd) for _, m := range matches { - highlights[start+m[0]] = p.group - if _, ok := highlights[start+m[1]]; !ok { - highlights[start+m[1]] = 0 + for i := m[0]; i < m[1]; i++ { + fullHighlights[i] = p.group + } + } + } + for i, h := range fullHighlights { + if i == 0 || h != fullHighlights[i-1] { + if _, ok := highlights[start+i]; !ok { + highlights[start+i] = h } } } @@ -193,12 +265,13 @@ func (h *Highlighter) HighlightString(input string) []LineMatch { var lineMatches []LineMatch for i := 0; i < len(lines); i++ { - line := []byte(lines[i]) + line := []rune(lines[i]) + highlights := make(LineMatch) if i == 0 || h.lastRegion == nil { - lineMatches = append(lineMatches, h.highlightEmptyRegion(0, true, i, line)) + lineMatches = append(lineMatches, h.highlightEmptyRegion(highlights, 0, true, i, line, false)) } else { - lineMatches = append(lineMatches, h.highlightRegion(0, true, i, line, h.lastRegion)) + lineMatches = append(lineMatches, h.highlightRegion(highlights, 0, true, i, line, h.lastRegion, false)) } } @@ -207,15 +280,14 @@ func (h *Highlighter) HighlightString(input string) []LineMatch { // HighlightStates correctly sets all states for the buffer func (h *Highlighter) HighlightStates(input LineStates) { - lines := input.LineData() - - for i := 0; i < len(lines); i++ { - line := []byte(lines[i]) + for i := 0; i < input.LinesNum(); i++ { + line := []rune(input.Line(i)) + // highlights := make(LineMatch) if i == 0 || h.lastRegion == nil { - h.highlightEmptyRegion(0, true, i, line) + h.highlightEmptyRegion(nil, 0, true, i, line, true) } else { - h.highlightRegion(0, true, i, line, h.lastRegion) + h.highlightRegion(nil, 0, true, i, line, h.lastRegion, true) } curState := h.lastRegion @@ -228,50 +300,47 @@ func (h *Highlighter) HighlightStates(input LineStates) { // It sets all other matches in the buffer to nil to conserve memory // This assumes that all the states are set correctly func (h *Highlighter) HighlightMatches(input LineStates, startline, endline int) { - lines := input.LineData() + for i := startline; i < endline; i++ { + if i >= input.LinesNum() { + break + } - for i := 0; i < len(lines); i++ { - if i >= startline && i < endline { - line := []byte(lines[i]) - - var match LineMatch - if i == 0 || input.State(i-1) == nil { - match = h.highlightEmptyRegion(0, true, i, line) - } else { - match = h.highlightRegion(0, true, i, line, input.State(i-1)) - } + line := []rune(input.Line(i)) + highlights := make(LineMatch) - input.SetMatch(i, match) + var match LineMatch + if i == 0 || input.State(i-1) == nil { + match = h.highlightEmptyRegion(highlights, 0, true, i, line, false) } else { - input.SetMatch(i, nil) + match = h.highlightRegion(highlights, 0, true, i, line, input.State(i-1), false) } + + input.SetMatch(i, match) } } // 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) { - lines := input.LineData() + // lines := input.LineData() h.lastRegion = nil if startline > 0 { h.lastRegion = input.State(startline - 1) } - for i := startline; i < len(lines); i++ { - line := []byte(lines[i]) + for i := startline; i < input.LinesNum(); i++ { + line := []rune(input.Line(i)) + // highlights := make(LineMatch) // var match LineMatch if i == 0 || h.lastRegion == nil { - h.highlightEmptyRegion(0, true, i, line) + h.highlightEmptyRegion(nil, 0, true, i, line, true) } else { - h.highlightRegion(0, true, i, line, h.lastRegion) + h.highlightRegion(nil, 0, true, i, line, h.lastRegion, true) } curState := h.lastRegion lastState := input.State(i) - // if i < endline { - // input.SetMatch(i, match) - // } input.SetState(i, curState) if curState == lastState { @@ -282,9 +351,8 @@ func (h *Highlighter) ReHighlightStates(input LineStates, startline int) { // ReHighlightLine will rehighlight the state and match for a single line func (h *Highlighter) ReHighlightLine(input LineStates, lineN int) { - lines := input.LineData() - - line := []byte(lines[lineN]) + line := []rune(input.Line(lineN)) + highlights := make(LineMatch) h.lastRegion = nil if lineN > 0 { @@ -293,9 +361,9 @@ func (h *Highlighter) ReHighlightLine(input LineStates, lineN int) { var match LineMatch if lineN == 0 || h.lastRegion == nil { - match = h.highlightEmptyRegion(0, true, lineN, line) + match = h.highlightEmptyRegion(highlights, 0, true, lineN, line, false) } else { - match = h.highlightRegion(0, true, lineN, line, h.lastRegion) + match = h.highlightRegion(highlights, 0, true, lineN, line, h.lastRegion, false) } curState := h.lastRegion