]> git.lizzy.rs Git - micro.git/blob - src/highlighter.go
Reorganize
[micro.git] / src / highlighter.go
1 package main
2
3 import (
4         "fmt"
5         "github.com/gdamore/tcell"
6         "io/ioutil"
7         "os/user"
8         "path/filepath"
9         "regexp"
10         "strings"
11 )
12
13 var syntaxFiles map[[2]*regexp.Regexp][2]string
14
15 // LoadSyntaxFiles loads the syntax files from the default directory ~/.micro
16 func LoadSyntaxFiles() {
17         usr, _ := user.Current()
18         dir := usr.HomeDir
19         LoadSyntaxFilesFromDir(dir + "/.micro/syntax")
20 }
21
22 // JoinRule takes a syntax rule (which can be multiple regular expressions)
23 // and joins it into one regular expression by ORing everything together
24 func JoinRule(rule string) string {
25         split := strings.Split(rule, `" "`)
26         joined := strings.Join(split, ")|(")
27         joined = "(" + joined + ")"
28         return joined
29 }
30
31 // LoadSyntaxFilesFromDir loads the syntax files from a specified directory
32 // To load the syntax files, we must fill the `syntaxFiles` map
33 // This involves finding the regex for syntax and if it exists, the regex
34 // for the header. Then we must get the text for the file and the filetype.
35 func LoadSyntaxFilesFromDir(dir string) {
36         syntaxFiles = make(map[[2]*regexp.Regexp][2]string)
37         files, _ := ioutil.ReadDir(dir)
38         for _, f := range files {
39                 if filepath.Ext(f.Name()) == ".micro" {
40                         text, err := ioutil.ReadFile(dir + "/" + f.Name())
41
42                         if err != nil {
43                                 fmt.Println("Error loading syntax files:", err)
44                                 continue
45                         }
46                         lines := strings.Split(string(text), "\n")
47
48                         syntaxParser := regexp.MustCompile(`syntax "(.*?)"\s+"(.*)"+`)
49                         headerParser := regexp.MustCompile(`header "(.*)"`)
50
51                         var syntaxRegex *regexp.Regexp
52                         var headerRegex *regexp.Regexp
53                         var filetype string
54                         var rules string
55                         for _, line := range lines {
56                                 if strings.TrimSpace(line) == "" ||
57                                         strings.TrimSpace(line)[0] == '#' {
58                                         // Ignore this line
59                                         continue
60                                 }
61
62                                 if strings.HasPrefix(line, "syntax") {
63                                         syntaxMatches := syntaxParser.FindSubmatch([]byte(line))
64                                         if len(syntaxMatches) == 3 {
65                                                 if syntaxRegex != nil {
66                                                         regexes := [2]*regexp.Regexp{syntaxRegex, headerRegex}
67                                                         syntaxFiles[regexes] = [2]string{rules, filetype}
68                                                 }
69
70                                                 filetype = string(syntaxMatches[1])
71                                                 extensions := JoinRule(string(syntaxMatches[2]))
72
73                                                 syntaxRegex, err = regexp.Compile(extensions)
74                                                 if err != nil {
75                                                         fmt.Println("Regex error:", err)
76                                                         continue
77                                                 }
78                                         } else {
79                                                 fmt.Println("Syntax statement is not valid:", line)
80                                                 continue
81                                         }
82                                 } else if strings.HasPrefix(line, "header") {
83                                         headerMatches := headerParser.FindSubmatch([]byte(line))
84                                         if len(headerMatches) == 2 {
85                                                 header := JoinRule(string(headerMatches[1]))
86
87                                                 headerRegex, err = regexp.Compile(header)
88                                                 if err != nil {
89                                                         fmt.Println("Regex error:", err)
90                                                         continue
91                                                 }
92                                         } else {
93                                                 fmt.Println("Header statement is not valid:", line)
94                                                 continue
95                                         }
96                                 } else {
97                                         rules += line + "\n"
98                                 }
99                         }
100                         if syntaxRegex != nil {
101                                 regexes := [2]*regexp.Regexp{syntaxRegex, headerRegex}
102                                 syntaxFiles[regexes] = [2]string{rules, filetype}
103                         }
104                 }
105         }
106 }
107
108 // GetRules finds the syntax rules that should be used for the buffer
109 // and returns them. It also returns the filetype of the file
110 func GetRules(buf *Buffer) (string, string) {
111         for r := range syntaxFiles {
112                 if r[0] != nil && r[0].MatchString(buf.path) {
113                         return syntaxFiles[r][0], syntaxFiles[r][1]
114                 } else if r[1] != nil && r[1].MatchString(buf.lines[0]) {
115                         return syntaxFiles[r][0], syntaxFiles[r][1]
116                 }
117         }
118         return "", "Unknown"
119 }
120
121 // Match takes a buffer and returns a map specifying how it should be syntax highlighted
122 // The map is from character numbers to styles, so map[3] represents the style change
123 // at the third character in the buffer
124 // Note that this map only stores changes in styles, not each character's style
125 func Match(rules string, buf *Buffer, v *View) map[int]tcell.Style {
126         start := v.topline - synLinesUp
127         end := v.topline + v.height + synLinesDown
128         if start < 0 {
129                 start = 0
130         }
131         if end > len(buf.lines) {
132                 end = len(buf.lines)
133         }
134         str := strings.Join(buf.lines[start:end], "\n")
135         startNum := v.cursor.loc + v.cursor.Distance(0, start)
136         toplineNum := v.cursor.loc + v.cursor.Distance(0, v.topline)
137
138         lines := strings.Split(rules, "\n")
139         m := make(map[int]tcell.Style)
140         parser := regexp.MustCompile(`color (.*?)\s+"(.*)"`)
141         for _, line := range lines {
142                 if strings.TrimSpace(line) == "" {
143                         // Ignore this line
144                         continue
145                 }
146                 submatch := parser.FindSubmatch([]byte(line))
147                 color := string(submatch[1])
148                 regex, err := regexp.Compile(string(submatch[2]))
149                 if err != nil {
150                         // Error with the regex!
151                         continue
152                 }
153                 st := StringToStyle(color)
154
155                 if regex.MatchString(str) {
156                         indicies := regex.FindAllStringIndex(str, -1)
157                         for _, value := range indicies {
158                                 value[0] += startNum
159                                 value[1] += startNum
160                                 for i := value[0] + 1; i < value[1]; i++ {
161                                         if _, exists := m[i]; exists {
162                                                 delete(m, i)
163                                         }
164                                 }
165
166                                 if value[0] < toplineNum && value[1] > toplineNum {
167                                         m[toplineNum] = st
168                                 }
169
170                                 if value[0] >= toplineNum {
171                                         m[value[0]] = st
172                                 }
173                                 if value[1] >= toplineNum {
174                                         if _, exists := m[value[1]]; !exists {
175                                                 m[value[1]] = tcell.StyleDefault
176                                         }
177                                 }
178                         }
179                 }
180         }
181
182         return m
183 }
184
185 // StringToStyle returns a style from a string
186 func StringToStyle(str string) tcell.Style {
187         var fg string
188         var bg string
189         split := strings.Split(str, ",")
190         if len(split) > 1 {
191                 fg, bg = split[0], split[1]
192         } else {
193                 fg = split[0]
194         }
195
196         return tcell.StyleDefault.Foreground(StringToColor(fg)).Background(StringToColor(bg))
197 }
198
199 // StringToColor returns a tcell color from a string representation of a color
200 func StringToColor(str string) tcell.Color {
201         switch str {
202         case "black":
203                 return tcell.ColorBlack
204         case "red":
205                 return tcell.ColorMaroon
206         case "green":
207                 return tcell.ColorGreen
208         case "yellow":
209                 return tcell.ColorOlive
210         case "blue":
211                 return tcell.ColorNavy
212         case "magenta":
213                 return tcell.ColorPurple
214         case "cyan":
215                 return tcell.ColorTeal
216         case "white":
217                 return tcell.ColorSilver
218         case "brightblack":
219                 return tcell.ColorGray
220         case "brightred":
221                 return tcell.ColorRed
222         case "brightgreen":
223                 return tcell.ColorLime
224         case "brightyellow":
225                 return tcell.ColorYellow
226         case "brightblue":
227                 return tcell.ColorBlue
228         case "brightmagenta":
229                 return tcell.ColorFuchsia
230         case "brightcyan":
231                 return tcell.ColorAqua
232         case "brightwhite":
233                 return tcell.ColorWhite
234         default:
235                 return tcell.ColorDefault
236         }
237 }