diff --git a/cmd/micro/cellview.go b/cmd/micro/cellview.go index 78b547d8..6f8d3926 100644 --- a/cmd/micro/cellview.go +++ b/cmd/micro/cellview.go @@ -2,6 +2,7 @@ package main import ( "github.com/mattn/go-runewidth" + "github.com/zyedidia/micro/cmd/micro/highlight" "github.com/zyedidia/tcell" ) @@ -20,7 +21,7 @@ func visualToCharPos(visualIndex int, lineN int, str string, buf *Buffer, tabsiz width := StringWidth(str[:i], tabsize) if group, ok := buf.Match(lineN)[charPos]; ok { - s := GetColor(group) + s := GetColor(highlight.GetGroup(group)) style = &s } @@ -108,7 +109,7 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) { break } if group, ok := buf.Match(lineN)[colN]; ok { - curStyle = GetColor(group) + curStyle = GetColor(highlight.GetGroup(group)) } char := line[colN] @@ -164,7 +165,7 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) { } if group, ok := buf.Match(lineN)[len(line)]; ok { - curStyle = GetColor(group) + curStyle = GetColor(highlight.GetGroup(group)) } // newline diff --git a/cmd/micro/highlight/highlighter.go b/cmd/micro/highlight/highlighter.go index ad4c756b..8ccefe6a 100644 --- a/cmd/micro/highlight/highlighter.go +++ b/cmd/micro/highlight/highlighter.go @@ -8,7 +8,7 @@ import ( func combineLineMatch(src, dst LineMatch) LineMatch { for k, v := range src { if g, ok := dst[k]; ok { - if g == "" { + if g == 0 { dst[k] = v } } else { @@ -38,7 +38,7 @@ func NewHighlighter(def *Def) *Highlighter { return h } -type LineMatch map[int]string +type LineMatch map[int]uint8 func FindIndex(regex *regexp.Regexp, str []byte, canMatchStart, canMatchEnd bool) []int { regexStr := regex.String() @@ -80,7 +80,7 @@ func (h *Highlighter) highlightRegion(start int, canMatchEnd bool, lineNum int, loc := FindIndex(region.end, line, start == 0, canMatchEnd) if loc != nil { if region.parent == nil { - highlights[start+loc[1]] = "" + 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]:]))) @@ -166,7 +166,7 @@ func (h *Highlighter) highlightEmptyRegion(start int, canMatchEnd bool, lineNum for _, m := range matches { highlights[start+m[0]] = p.group if _, ok := highlights[start+m[1]]; !ok { - highlights[start+m[1]] = "" + highlights[start+m[1]] = 0 } } } diff --git a/cmd/micro/highlight/parser.go b/cmd/micro/highlight/parser.go index 21a905fc..9b3d5d65 100644 --- a/cmd/micro/highlight/parser.go +++ b/cmd/micro/highlight/parser.go @@ -7,6 +7,18 @@ import ( "gopkg.in/yaml.v2" ) +var groups map[string]uint8 +var numGroups uint8 + +func GetGroup(n uint8) string { + for k, v := range groups { + if v == n { + return k + } + } + return "" +} + // A Def is a full syntax definition for a language // It has a filetype, information about how to detect the filetype based // on filename or header (the first line of the file) @@ -21,7 +33,7 @@ type Def struct { // It has a group that the rule belongs to, as well as // the regular expression to match the pattern type Pattern struct { - group string + group uint8 regex *regexp.Regexp } @@ -39,13 +51,17 @@ type Rules struct { // region and also rules from the above region do not match inside this region // Note that a region may contain more regions type Region struct { - group string + group uint8 parent *Region start *regexp.Regexp end *regexp.Regexp rules *Rules } +func init() { + groups = make(map[string]uint8) +} + // ParseDef parses an input syntax file into a highlight Def func ParseDef(input []byte) (s *Def, err error) { // This is just so if we have an error, we can exit cleanly and return the parse error to the user @@ -155,7 +171,13 @@ func parseRules(input []interface{}, curRegion *Region) (*Rules, error) { return nil, err } - rules.patterns = append(rules.patterns, &Pattern{group.(string), r}) + groupStr := group.(string) + if _, ok := groups[groupStr]; !ok { + numGroups++ + groups[groupStr] = numGroups + } + groupNum := groups[groupStr] + rules.patterns = append(rules.patterns, &Pattern{groupNum, r}) } case map[interface{}]interface{}: // Region @@ -177,7 +199,12 @@ func parseRegion(group string, regionInfo map[interface{}]interface{}, prevRegio var err error region := new(Region) - region.group = group + if _, ok := groups[group]; !ok { + numGroups++ + groups[group] = numGroups + } + groupNum := groups[group] + region.group = groupNum region.parent = prevRegion region.start, err = regexp.Compile(regionInfo["start"].(string))