4 "github.com/gdamore/tcell"
5 "github.com/mitchellh/go-homedir"
12 // FileTypeRules represents a complete set of syntax rules for a filetype
13 type FileTypeRules struct {
18 // SyntaxRule represents a regex to highlight in a certain style
19 type SyntaxRule struct {
24 // Whether this regex is a start=... end=... regex
26 // How to highlight it
30 var syntaxFiles map[[2]*regexp.Regexp]FileTypeRules
32 // LoadSyntaxFiles loads the syntax files from the default directory ~/.micro
33 func LoadSyntaxFiles() {
34 dir, err := homedir.Dir()
36 TermMessage("Error finding your home directory\nCan't load runtime files")
39 LoadSyntaxFilesFromDir(dir + "/.micro/syntax")
42 // JoinRule takes a syntax rule (which can be multiple regular expressions)
43 // and joins it into one regular expression by ORing everything together
44 func JoinRule(rule string) string {
45 split := strings.Split(rule, `" "`)
46 joined := strings.Join(split, ")|(")
47 joined = "(" + joined + ")"
51 // LoadSyntaxFilesFromDir loads the syntax files from a specified directory
52 // To load the syntax files, we must fill the `syntaxFiles` map
53 // This involves finding the regex for syntax and if it exists, the regex
54 // for the header. Then we must get the text for the file and the filetype.
55 func LoadSyntaxFilesFromDir(dir string) {
58 syntaxFiles = make(map[[2]*regexp.Regexp]FileTypeRules)
59 files, _ := ioutil.ReadDir(dir)
60 for _, f := range files {
61 if filepath.Ext(f.Name()) == ".micro" {
62 text, err := ioutil.ReadFile(dir + "/" + f.Name())
63 filename := dir + "/" + f.Name()
66 TermMessage("Error loading syntax files: " + err.Error())
69 lines := strings.Split(string(text), "\n")
71 syntaxParser := regexp.MustCompile(`syntax "(.*?)"\s+"(.*)"+`)
72 headerParser := regexp.MustCompile(`header "(.*)"`)
74 ruleParser := regexp.MustCompile(`color (.*?)\s+(?:\((.*?)\)\s+)?"(.*)"`)
75 ruleStartEndParser := regexp.MustCompile(`color (.*?)\s+(?:\((.*?)\)\s+)?start="(.*)"\s+end="(.*)"`)
77 var syntaxRegex *regexp.Regexp
78 var headerRegex *regexp.Regexp
80 var rules []SyntaxRule
81 for lineNum, line := range lines {
82 if strings.TrimSpace(line) == "" ||
83 strings.TrimSpace(line)[0] == '#' {
88 if strings.HasPrefix(line, "syntax") {
89 syntaxMatches := syntaxParser.FindSubmatch([]byte(line))
90 if len(syntaxMatches) == 3 {
91 if syntaxRegex != nil {
92 regexes := [2]*regexp.Regexp{syntaxRegex, headerRegex}
93 syntaxFiles[regexes] = FileTypeRules{filetype, rules}
97 filetype = string(syntaxMatches[1])
98 extensions := JoinRule(string(syntaxMatches[2]))
100 syntaxRegex, err = regexp.Compile(extensions)
102 TermError(filename, lineNum, err.Error())
106 TermError(filename, lineNum, "Syntax statement is not valid: "+line)
109 } else if strings.HasPrefix(line, "header") {
110 headerMatches := headerParser.FindSubmatch([]byte(line))
111 if len(headerMatches) == 2 {
112 header := JoinRule(string(headerMatches[1]))
114 headerRegex, err = regexp.Compile(header)
116 TermError(filename, lineNum, "Regex error: "+err.Error())
120 TermError(filename, lineNum, "Header statement is not valid: "+line)
124 if ruleParser.MatchString(line) {
125 submatch := ruleParser.FindSubmatch([]byte(line))
129 if len(submatch) == 4 {
130 color = string(submatch[1])
131 flags = string(submatch[2])
132 regexStr = "(?" + flags + ")" + JoinRule(string(submatch[3]))
133 } else if len(submatch) == 3 {
134 color = string(submatch[1])
135 regexStr = JoinRule(string(submatch[2]))
137 TermError(filename, lineNum, "Invalid statement: "+line)
139 regex, err := regexp.Compile(regexStr)
141 TermError(filename, lineNum, err.Error())
145 st := tcell.StyleDefault
146 if _, ok := colorscheme[color]; ok {
147 st = colorscheme[color]
149 st = StringToStyle(color)
151 rules = append(rules, SyntaxRule{regex, flags, false, st})
152 } else if ruleStartEndParser.MatchString(line) {
153 submatch := ruleStartEndParser.FindSubmatch([]byte(line))
157 // Use m and s flags by default
159 if len(submatch) == 5 {
160 color = string(submatch[1])
161 flags += string(submatch[2])
162 start = string(submatch[3])
163 end = string(submatch[4])
164 } else if len(submatch) == 4 {
165 color = string(submatch[1])
166 start = string(submatch[2])
167 end = string(submatch[3])
169 TermError(filename, lineNum, "Invalid statement: "+line)
172 regex, err := regexp.Compile("(?" + flags + ")" + "(" + start + ").*?(" + end + ")")
174 TermError(filename, lineNum, err.Error())
178 st := tcell.StyleDefault
179 if _, ok := colorscheme[color]; ok {
180 st = colorscheme[color]
182 st = StringToStyle(color)
184 rules = append(rules, SyntaxRule{regex, flags, true, st})
188 if syntaxRegex != nil {
189 regexes := [2]*regexp.Regexp{syntaxRegex, headerRegex}
190 syntaxFiles[regexes] = FileTypeRules{filetype, rules}
196 // GetRules finds the syntax rules that should be used for the buffer
197 // and returns them. It also returns the filetype of the file
198 func GetRules(buf *Buffer) ([]SyntaxRule, string) {
199 for r := range syntaxFiles {
200 if r[0] != nil && r[0].MatchString(buf.path) {
201 return syntaxFiles[r].rules, syntaxFiles[r].filetype
202 } else if r[1] != nil && r[1].MatchString(buf.lines[0]) {
203 return syntaxFiles[r].rules, syntaxFiles[r].filetype
206 return nil, "Unknown"
209 // SyntaxMatches is an alias to a map from character numbers to styles,
210 // so map[3] represents the style of the third character
211 type SyntaxMatches map[int]tcell.Style
213 // Match takes a buffer and returns the syntax matches a map specifying how it should be syntax highlighted
214 func Match(rules []SyntaxRule, buf *Buffer, v *View) SyntaxMatches {
215 m := make(SyntaxMatches)
217 lineStart := v.updateLines[0]
218 lineEnd := v.updateLines[1] + 1
220 // Don't need to update syntax highlighting
224 totalStart := v.topline - synLinesUp
225 totalEnd := v.topline + v.height + synLinesDown
229 if totalEnd > len(buf.lines) {
230 totalEnd = len(buf.lines)
233 if lineEnd > len(buf.lines) {
234 lineEnd = len(buf.lines)
237 lines := buf.lines[lineStart:lineEnd]
238 str := strings.Join(buf.lines[totalStart:totalEnd], "\n")
239 startNum := v.cursor.loc + v.cursor.Distance(0, totalStart)
240 toplineNum := v.cursor.loc + v.cursor.Distance(0, v.topline)
241 for _, rule := range rules {
242 if rule.startend && rule.regex.MatchString(str) {
243 indicies := rule.regex.FindAllStringIndex(str, -1)
244 for _, value := range indicies {
247 for i := value[0]; i < value[1]; i++ {
254 for _, line := range lines {
255 if rule.regex.MatchString(line) {
256 indicies := rule.regex.FindAllStringIndex(str, -1)
257 for _, value := range indicies {
258 value[0] += toplineNum
259 value[1] += toplineNum
260 for i := value[0]; i < value[1]; i++ {