7 "github.com/zyedidia/tcell"
10 // FileTypeRules represents a complete set of syntax rules for a filetype
11 type FileTypeRules struct {
17 // SyntaxRule represents a regex to highlight in a certain style
18 type SyntaxRule struct {
23 // Whether this regex is a start=... end=... regex
25 // How to highlight it
29 var syntaxKeys [][2]*regexp.Regexp
30 var syntaxFiles map[[2]*regexp.Regexp]FileTypeRules
32 // LoadSyntaxFiles loads the syntax files from the default directory (configDir)
33 func LoadSyntaxFiles() {
35 syntaxFiles = make(map[[2]*regexp.Regexp]FileTypeRules)
36 for _, f := range ListRuntimeFiles(RTSyntax) {
39 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
41 LoadSyntaxFile(string(data), f.Name())
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 + ")"
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) {
61 lines := strings.Split(string(text), "\n")
63 // Regex for parsing syntax statements
64 syntaxParser := regexp.MustCompile(`syntax "(.*?)"\s+"(.*)"+`)
65 // Regex for parsing header statements
66 headerParser := regexp.MustCompile(`header "(.*)"`)
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)
73 var syntaxRegex *regexp.Regexp
74 var headerRegex *regexp.Regexp
76 for lineNum, line := range lines {
77 if (hasSyntax == (syntaxRegex != nil)) && (hasHeader == (headerRegex != nil)) {
78 // We found what we we're supposed to find
82 if strings.TrimSpace(line) == "" ||
83 strings.TrimSpace(line)[0] == '#' {
88 if strings.HasPrefix(line, "syntax") {
90 syntaxMatches := syntaxParser.FindSubmatch([]byte(line))
91 if len(syntaxMatches) == 3 {
92 if syntaxRegex != nil {
93 TermError(filename, lineNum, "Syntax statement redeclaration")
96 filetype = string(syntaxMatches[1])
97 extensions := JoinRule(string(syntaxMatches[2]))
99 syntaxRegex, err = regexp.Compile(extensions)
101 TermError(filename, lineNum, err.Error())
105 TermError(filename, lineNum, "Syntax statement is not valid: "+line)
108 } 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)
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}
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")
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="(.*)"`)
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") {
158 // Syntax rule, but it could be standard or start-end
159 if ruleParser.MatchString(line) {
160 // Standard syntax rule
162 submatch := ruleParser.FindSubmatch([]byte(line))
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]))
176 // If len is not 3 or 4 there is a problem
177 TermError(filename, lineNum, "Invalid statement: "+line)
181 regex, err := regexp.Compile(regexStr)
183 TermError(filename, lineNum, err.Error())
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
192 groups := strings.Split(color, ".")
195 for i, g := range groups {
200 if style, ok := colorscheme[curGroup]; ok {
204 } else if style, ok := colorscheme[color]; ok {
207 st = StringToStyle(color)
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))
218 // Use m and s flags by default
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])
232 // If len is not 4 or 5 there is a problem
233 TermError(filename, lineNum, "Invalid statement: "+line)
238 regex, err := regexp.Compile("(?" + flags + ")" + "(" + start + ").*?(" + end + ")")
240 TermError(filename, lineNum, err.Error())
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
249 if _, ok := colorscheme[color]; ok {
250 st = colorscheme[color]
252 st = StringToStyle(color)
254 // Add the regex, flags, and style
255 // True because this is start-end
256 rules = append(rules, SyntaxRule{regex, flags, true, st})
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
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
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)
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
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 {
300 viewStart := v.Topline
301 viewEnd := v.Topline + v.Height
302 if viewEnd > buf.NumLines {
303 viewEnd = buf.NumLines
306 lines := buf.Lines(viewStart, viewEnd)
307 matches := make(SyntaxMatches, len(lines))
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
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
322 if totalEnd > buf.NumLines {
323 totalEnd = buf.NumLines
326 str := strings.Join(buf.Lines(totalStart, totalEnd), "\n")
327 startNum := ToCharPos(Loc{0, totalStart}, v.Buf)
329 for _, rule := range rules {
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 {
341 colNum, lineNum := curLoc.X, curLoc.Y
342 if lineNum == -1 || colNum == -1 {
346 if lineNum >= 0 && lineNum < v.Height {
347 matches[lineNum][colNum] = rule.style
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