]> git.lizzy.rs Git - micro.git/blob - cmd/micro/highlighter.go
Merge pull request #507 from NicolaiSoeborg/master
[micro.git] / cmd / micro / highlighter.go
1 package main
2
3 import (
4         "regexp"
5         "strings"
6
7         "github.com/zyedidia/tcell"
8 )
9
10 // FileTypeRules represents a complete set of syntax rules for a filetype
11 type FileTypeRules struct {
12         filetype string
13         filename string
14         text     string
15 }
16
17 // SyntaxRule represents a regex to highlight in a certain style
18 type SyntaxRule struct {
19         // What to highlight
20         regex *regexp.Regexp
21         // Any flags
22         flags string
23         // Whether this regex is a start=... end=... regex
24         startend bool
25         // How to highlight it
26         style tcell.Style
27 }
28
29 var syntaxKeys [][2]*regexp.Regexp
30 var syntaxFiles map[[2]*regexp.Regexp]FileTypeRules
31
32 // LoadSyntaxFiles loads the syntax files from the default directory (configDir)
33 func LoadSyntaxFiles() {
34         InitColorscheme()
35         syntaxFiles = make(map[[2]*regexp.Regexp]FileTypeRules)
36         for _, f := range ListRuntimeFiles(RTSyntax) {
37                 data, err := f.Data()
38                 if err != nil {
39                         TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
40                 } else {
41                         LoadSyntaxFile(string(data), f.Name())
42                 }
43         }
44 }
45
46 // JoinRule takes a syntax rule (which can be multiple regular expressions)
47 // and joins it into one regular expression by ORing everything together
48 func JoinRule(rule string) string {
49         split := strings.Split(rule, `" "`)
50         joined := strings.Join(split, ")|(")
51         joined = "(" + joined + ")"
52         return joined
53 }
54
55 // LoadSyntaxFile simply gets the filetype of a the syntax file and the source for the
56 // file and creates FileTypeRules out of it. If this filetype is the one opened by the user
57 // the rules will be loaded and compiled later
58 // In this function we are only concerned with loading the syntax and header regexes
59 func LoadSyntaxFile(text, filename string) {
60         var err error
61         lines := strings.Split(string(text), "\n")
62
63         // Regex for parsing syntax statements
64         syntaxParser := regexp.MustCompile(`syntax "(.*?)"\s+"(.*)"+`)
65         // Regex for parsing header statements
66         headerParser := regexp.MustCompile(`header "(.*)"`)
67
68         // Is there a syntax definition in this file?
69         hasSyntax := syntaxParser.MatchString(text)
70         // Is there a header definition in this file?
71         hasHeader := headerParser.MatchString(text)
72
73         var syntaxRegex *regexp.Regexp
74         var headerRegex *regexp.Regexp
75         var filetype string
76         for lineNum, line := range lines {
77                 if (hasSyntax == (syntaxRegex != nil)) && (hasHeader == (headerRegex != nil)) {
78                         // We found what we we're supposed to find
79                         break
80                 }
81
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                         // Syntax statement
90                         syntaxMatches := syntaxParser.FindSubmatch([]byte(line))
91                         if len(syntaxMatches) == 3 {
92                                 if syntaxRegex != nil {
93                                         TermError(filename, lineNum, "Syntax statement redeclaration")
94                                 }
95
96                                 filetype = string(syntaxMatches[1])
97                                 extensions := JoinRule(string(syntaxMatches[2]))
98
99                                 syntaxRegex, err = regexp.Compile(extensions)
100                                 if err != nil {
101                                         TermError(filename, lineNum, err.Error())
102                                         continue
103                                 }
104                         } else {
105                                 TermError(filename, lineNum, "Syntax statement is not valid: "+line)
106                                 continue
107                         }
108                 } else if strings.HasPrefix(line, "header") {
109                         // Header statement
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                 }
124         }
125         if syntaxRegex != nil {
126                 // Add the current rules to the syntaxFiles variable
127                 regexes := [2]*regexp.Regexp{syntaxRegex, headerRegex}
128                 syntaxKeys = append(syntaxKeys, regexes)
129                 syntaxFiles[regexes] = FileTypeRules{filetype, filename, text}
130         }
131 }
132
133 // LoadRulesFromFile loads just the syntax rules from a given file
134 // Only the necessary rules are loaded when the buffer is opened.
135 // If we load all the rules for every filetype when micro starts, there's a bit of lag
136 // A rule just explains how to color certain regular expressions
137 // Example: color comment "//.*"
138 // This would color all strings that match the regex "//.*" in the comment color defined
139 // by the colorscheme
140 func LoadRulesFromFile(text, filename string) []SyntaxRule {
141         lines := strings.Split(string(text), "\n")
142
143         // Regex for parsing standard syntax rules
144         ruleParser := regexp.MustCompile(`color (.*?)\s+(?:\((.*?)\)\s+)?"(.*)"`)
145         // Regex for parsing syntax rules with start="..." end="..."
146         ruleStartEndParser := regexp.MustCompile(`color (.*?)\s+(?:\((.*?)\)\s+)?start="(.*)"\s+end="(.*)"`)
147
148         var rules []SyntaxRule
149         for lineNum, line := range lines {
150                 if strings.TrimSpace(line) == "" ||
151                         strings.TrimSpace(line)[0] == '#' ||
152                         strings.HasPrefix(line, "syntax") ||
153                         strings.HasPrefix(line, "header") {
154                         // Ignore this line
155                         continue
156                 }
157
158                 // Syntax rule, but it could be standard or start-end
159                 if ruleParser.MatchString(line) {
160                         // Standard syntax rule
161                         // Parse the line
162                         submatch := ruleParser.FindSubmatch([]byte(line))
163                         var color string
164                         var regexStr string
165                         var flags string
166                         if len(submatch) == 4 {
167                                 // If len is 4 then the user specified some additional flags to use
168                                 color = string(submatch[1])
169                                 flags = string(submatch[2])
170                                 regexStr = "(?" + flags + ")" + JoinRule(string(submatch[3]))
171                         } else if len(submatch) == 3 {
172                                 // If len is 3, no additional flags were given
173                                 color = string(submatch[1])
174                                 regexStr = JoinRule(string(submatch[2]))
175                         } else {
176                                 // If len is not 3 or 4 there is a problem
177                                 TermError(filename, lineNum, "Invalid statement: "+line)
178                                 continue
179                         }
180                         // Compile the regex
181                         regex, err := regexp.Compile(regexStr)
182                         if err != nil {
183                                 TermError(filename, lineNum, err.Error())
184                                 continue
185                         }
186
187                         // Get the style
188                         // The user could give us a "color" that is really a part of the colorscheme
189                         // in which case we should look that up in the colorscheme
190                         // They can also just give us a straight up color
191                         st := defStyle
192                         groups := strings.Split(color, ".")
193                         if len(groups) > 1 {
194                                 curGroup := ""
195                                 for i, g := range groups {
196                                         if i != 0 {
197                                                 curGroup += "."
198                                         }
199                                         curGroup += g
200                                         if style, ok := colorscheme[curGroup]; ok {
201                                                 st = style
202                                         }
203                                 }
204                         } else if style, ok := colorscheme[color]; ok {
205                                 st = style
206                         } else {
207                                 st = StringToStyle(color)
208                         }
209                         // Add the regex, flags, and style
210                         // False because this is not start-end
211                         rules = append(rules, SyntaxRule{regex, flags, false, st})
212                 } else if ruleStartEndParser.MatchString(line) {
213                         // Start-end syntax rule
214                         submatch := ruleStartEndParser.FindSubmatch([]byte(line))
215                         var color string
216                         var start string
217                         var end string
218                         // Use m and s flags by default
219                         flags := "ms"
220                         if len(submatch) == 5 {
221                                 // If len is 5 the user provided some additional flags
222                                 color = string(submatch[1])
223                                 flags += string(submatch[2])
224                                 start = string(submatch[3])
225                                 end = string(submatch[4])
226                         } else if len(submatch) == 4 {
227                                 // If len is 4 the user did not provide additional flags
228                                 color = string(submatch[1])
229                                 start = string(submatch[2])
230                                 end = string(submatch[3])
231                         } else {
232                                 // If len is not 4 or 5 there is a problem
233                                 TermError(filename, lineNum, "Invalid statement: "+line)
234                                 continue
235                         }
236
237                         // Compile the regex
238                         regex, err := regexp.Compile("(?" + flags + ")" + "(" + start + ").*?(" + end + ")")
239                         if err != nil {
240                                 TermError(filename, lineNum, err.Error())
241                                 continue
242                         }
243
244                         // Get the style
245                         // The user could give us a "color" that is really a part of the colorscheme
246                         // in which case we should look that up in the colorscheme
247                         // They can also just give us a straight up color
248                         st := defStyle
249                         if _, ok := colorscheme[color]; ok {
250                                 st = colorscheme[color]
251                         } else {
252                                 st = StringToStyle(color)
253                         }
254                         // Add the regex, flags, and style
255                         // True because this is start-end
256                         rules = append(rules, SyntaxRule{regex, flags, true, st})
257                 }
258         }
259         return rules
260 }
261
262 // FindFileType finds the filetype for the given buffer
263 func FindFileType(buf *Buffer) string {
264         for _, r := range syntaxKeys {
265                 if r[1] != nil && r[1].MatchString(buf.Line(0)) {
266                         // The header statement matches the first line
267                         return syntaxFiles[r].filetype
268                 }
269         }
270         for _, r := range syntaxKeys {
271                 if r[0] != nil && r[0].MatchString(buf.Path) {
272                         // The syntax statement matches the extension
273                         return syntaxFiles[r].filetype
274                 }
275         }
276         return "Unknown"
277 }
278
279 // GetRules finds the syntax rules that should be used for the buffer
280 // and returns them. It also returns the filetype of the file
281 func GetRules(buf *Buffer) []SyntaxRule {
282         for _, r := range syntaxKeys {
283                 if syntaxFiles[r].filetype == buf.FileType() {
284                         return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename)
285                 }
286         }
287         return nil
288 }
289
290 // SyntaxMatches is an alias to a map from character numbers to styles,
291 // so map[3] represents the style of the third character
292 type SyntaxMatches [][]tcell.Style
293
294 // Match takes a buffer and returns the syntax matches: a 2d array specifying how it should be syntax highlighted
295 // We match the rules from up `synLinesUp` lines and down `synLinesDown` lines
296 func Match(v *View) SyntaxMatches {
297         buf := v.Buf
298         rules := v.Buf.rules
299
300         viewStart := v.Topline
301         viewEnd := v.Topline + v.Height
302         if viewEnd > buf.NumLines {
303                 viewEnd = buf.NumLines
304         }
305
306         lines := buf.Lines(viewStart, viewEnd)
307         matches := make(SyntaxMatches, len(lines))
308
309         for i, line := range lines {
310                 matches[i] = make([]tcell.Style, len(line)+1)
311                 for j := range matches[i] {
312                         matches[i][j] = defStyle
313                 }
314         }
315
316         // We don't actually check the entire buffer, just from synLinesUp to synLinesDown
317         totalStart := v.Topline - synLinesUp
318         totalEnd := v.Topline + v.Height + synLinesDown
319         if totalStart < 0 {
320                 totalStart = 0
321         }
322         if totalEnd > buf.NumLines {
323                 totalEnd = buf.NumLines
324         }
325
326         str := strings.Join(buf.Lines(totalStart, totalEnd), "\n")
327         startNum := ToCharPos(Loc{0, totalStart}, v.Buf)
328
329         for _, rule := range rules {
330                 if rule.startend {
331                         if indicies := rule.regex.FindAllStringIndex(str, -1); indicies != nil {
332                                 for _, value := range indicies {
333                                         value[0] = runePos(value[0], str) + startNum
334                                         value[1] = runePos(value[1], str) + startNum
335                                         startLoc := FromCharPos(value[0], buf)
336                                         endLoc := FromCharPos(value[1], buf)
337                                         for curLoc := startLoc; curLoc.LessThan(endLoc); curLoc = curLoc.Move(1, buf) {
338                                                 if curLoc.Y < v.Topline {
339                                                         continue
340                                                 }
341                                                 colNum, lineNum := curLoc.X, curLoc.Y
342                                                 if lineNum == -1 || colNum == -1 {
343                                                         continue
344                                                 }
345                                                 lineNum -= viewStart
346                                                 if lineNum >= 0 && lineNum < v.Height {
347                                                         matches[lineNum][colNum] = rule.style
348                                                 }
349                                         }
350                                 }
351                         }
352                 } else {
353                         for lineN, line := range lines {
354                                 if indicies := rule.regex.FindAllStringIndex(line, -1); indicies != nil {
355                                         for _, value := range indicies {
356                                                 start := runePos(value[0], line)
357                                                 end := runePos(value[1], line)
358                                                 for i := start; i < end; i++ {
359                                                         matches[lineN][i] = rule.style
360                                                 }
361                                         }
362                                 }
363                         }
364                 }
365         }
366
367         return matches
368 }