]> git.lizzy.rs Git - micro.git/blob - src/highlighter.go
5fb6dcbb824e26cac7b2acaa05f31c07eab29ca2
[micro.git] / src / highlighter.go
1 package main
2
3 import (
4         "github.com/gdamore/tcell"
5         "github.com/mitchellh/go-homedir"
6         "io/ioutil"
7         "path/filepath"
8         "regexp"
9         "strings"
10 )
11
12 // FileTypeRules represents a complete set of syntax rules for a filetype
13 type FileTypeRules struct {
14         filetype string
15         rules    []SyntaxRule
16 }
17
18 // SyntaxRule represents a regex to highlight in a certain style
19 type SyntaxRule struct {
20         // What to highlight
21         regex *regexp.Regexp
22         // Any flags
23         flags string
24         // Whether this regex is a start=... end=... regex
25         startend bool
26         // How to highlight it
27         style tcell.Style
28 }
29
30 var syntaxFiles map[[2]*regexp.Regexp]FileTypeRules
31
32 // LoadSyntaxFiles loads the syntax files from the default directory ~/.micro
33 func LoadSyntaxFiles() {
34         dir, err := homedir.Dir()
35         if err != nil {
36                 TermMessage("Error finding your home directory\nCan't load runtime files")
37                 return
38         }
39         LoadSyntaxFilesFromDir(dir + "/.micro/syntax")
40 }
41
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 + ")"
48         return joined
49 }
50
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) {
56         InitColorscheme()
57
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()
64
65                         if err != nil {
66                                 TermMessage("Error loading syntax files: " + err.Error())
67                                 continue
68                         }
69                         lines := strings.Split(string(text), "\n")
70
71                         syntaxParser := regexp.MustCompile(`syntax "(.*?)"\s+"(.*)"+`)
72                         headerParser := regexp.MustCompile(`header "(.*)"`)
73
74                         ruleParser := regexp.MustCompile(`color (.*?)\s+(?:\((.*?)\)\s+)?"(.*)"`)
75                         ruleStartEndParser := regexp.MustCompile(`color (.*?)\s+(?:\((.*?)\)\s+)?start="(.*)"\s+end="(.*)"`)
76
77                         var syntaxRegex *regexp.Regexp
78                         var headerRegex *regexp.Regexp
79                         var filetype string
80                         var rules []SyntaxRule
81                         for lineNum, line := range lines {
82                                 if strings.TrimSpace(line) == "" ||
83                                         strings.TrimSpace(line)[0] == '#' {
84                                         // Ignore this line
85                                         continue
86                                 }
87
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}
94                                                 }
95                                                 rules = rules[:0]
96
97                                                 filetype = string(syntaxMatches[1])
98                                                 extensions := JoinRule(string(syntaxMatches[2]))
99
100                                                 syntaxRegex, err = regexp.Compile(extensions)
101                                                 if err != nil {
102                                                         TermError(filename, lineNum, err.Error())
103                                                         continue
104                                                 }
105                                         } else {
106                                                 TermError(filename, lineNum, "Syntax statement is not valid: "+line)
107                                                 continue
108                                         }
109                                 } else if strings.HasPrefix(line, "header") {
110                                         headerMatches := headerParser.FindSubmatch([]byte(line))
111                                         if len(headerMatches) == 2 {
112                                                 header := JoinRule(string(headerMatches[1]))
113
114                                                 headerRegex, err = regexp.Compile(header)
115                                                 if err != nil {
116                                                         TermError(filename, lineNum, "Regex error: "+err.Error())
117                                                         continue
118                                                 }
119                                         } else {
120                                                 TermError(filename, lineNum, "Header statement is not valid: "+line)
121                                                 continue
122                                         }
123                                 } else {
124                                         if ruleParser.MatchString(line) {
125                                                 submatch := ruleParser.FindSubmatch([]byte(line))
126                                                 var color string
127                                                 var regexStr string
128                                                 var flags string
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]))
136                                                 } else {
137                                                         TermError(filename, lineNum, "Invalid statement: "+line)
138                                                 }
139                                                 regex, err := regexp.Compile(regexStr)
140                                                 if err != nil {
141                                                         TermError(filename, lineNum, err.Error())
142                                                         continue
143                                                 }
144
145                                                 st := tcell.StyleDefault
146                                                 if _, ok := colorscheme[color]; ok {
147                                                         st = colorscheme[color]
148                                                 } else {
149                                                         st = StringToStyle(color)
150                                                 }
151                                                 rules = append(rules, SyntaxRule{regex, flags, false, st})
152                                         } else if ruleStartEndParser.MatchString(line) {
153                                                 submatch := ruleStartEndParser.FindSubmatch([]byte(line))
154                                                 var color string
155                                                 var start string
156                                                 var end string
157                                                 // Use m and s flags by default
158                                                 flags := "ms"
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])
168                                                 } else {
169                                                         TermError(filename, lineNum, "Invalid statement: "+line)
170                                                 }
171
172                                                 regex, err := regexp.Compile("(?" + flags + ")" + "(" + start + ").*?(" + end + ")")
173                                                 if err != nil {
174                                                         TermError(filename, lineNum, err.Error())
175                                                         continue
176                                                 }
177
178                                                 st := tcell.StyleDefault
179                                                 if _, ok := colorscheme[color]; ok {
180                                                         st = colorscheme[color]
181                                                 } else {
182                                                         st = StringToStyle(color)
183                                                 }
184                                                 rules = append(rules, SyntaxRule{regex, flags, true, st})
185                                         }
186                                 }
187                         }
188                         if syntaxRegex != nil {
189                                 regexes := [2]*regexp.Regexp{syntaxRegex, headerRegex}
190                                 syntaxFiles[regexes] = FileTypeRules{filetype, rules}
191                         }
192                 }
193         }
194 }
195
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
204                 }
205         }
206         return nil, "Unknown"
207 }
208
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
212
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)
216
217         lineStart := v.updateLines[0]
218         lineEnd := v.updateLines[1] + 1
219         if lineStart < 0 {
220                 // Don't need to update syntax highlighting
221                 return m
222         }
223
224         totalStart := v.topline - synLinesUp
225         totalEnd := v.topline + v.height + synLinesDown
226         if totalStart < 0 {
227                 totalStart = 0
228         }
229         if totalEnd > len(buf.lines) {
230                 totalEnd = len(buf.lines)
231         }
232
233         if lineEnd > len(buf.lines) {
234                 lineEnd = len(buf.lines)
235         }
236
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 {
245                                 value[0] += startNum
246                                 value[1] += startNum
247                                 for i := value[0]; i < value[1]; i++ {
248                                         if i >= toplineNum {
249                                                 m[i] = rule.style
250                                         }
251                                 }
252                         }
253                 } else {
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++ {
261                                                         m[i] = rule.style
262                                                 }
263                                         }
264                                 }
265                         }
266                 }
267         }
268
269         return m
270 }