package main
import (
- "github.com/gdamore/tcell"
+ "github.com/zyedidia/tcell"
"io/ioutil"
"path/filepath"
"regexp"
var syntaxFiles map[[2]*regexp.Regexp]FileTypeRules
+// These syntax files are pre installed and embedded in the resulting binary by go-bindata
var preInstalledSynFiles = []string{
"Dockerfile",
"apacheconf",
"privoxy-filter",
"puppet",
"python",
+ "r",
"reST",
"rpmspec",
"ruby",
// LoadSyntaxFiles loads the syntax files from the default directory (configDir)
func LoadSyntaxFiles() {
+ // Load the user's custom syntax files, if there are any
LoadSyntaxFilesFromDir(configDir + "/syntax")
+ // Load the pre-installed syntax files from inside the binary
for _, filetype := range preInstalledSynFiles {
data, err := Asset("runtime/syntax/" + filetype + ".micro")
if err != nil {
return joined
}
-// LoadSyntaxFile loads the specified syntax file
-// A syntax file is a list of syntax rules, explaining how to color certain
-// regular expressions
-// Example: color comment "//.*"
-// This would color all strings that match the regex "//.*" in the comment color defined
-// by the colorscheme
+// LoadSyntaxFile simply gets the filetype of a the syntax file and the source for the
+// file and creates FileTypeRules out of it. If this filetype is the one opened by the user
+// the rules will be loaded and compiled later
+// In this function we are only concerned with loading the syntax and header regexes
func LoadSyntaxFile(text, filename string) {
var err error
lines := strings.Split(string(text), "\n")
// Regex for parsing header statements
headerParser := regexp.MustCompile(`header "(.*)"`)
+ // Is there a syntax definition in this file?
+ hasSyntax := syntaxParser.MatchString(text)
+ // Is there a header definition in this file?
+ hasHeader := headerParser.MatchString(text)
+
var syntaxRegex *regexp.Regexp
var headerRegex *regexp.Regexp
var filetype string
for lineNum, line := range lines {
+ if (hasSyntax == (syntaxRegex != nil)) && (hasHeader == (headerRegex != nil)) {
+ // We found what we we're supposed to find
+ break
+ }
+
if strings.TrimSpace(line) == "" ||
strings.TrimSpace(line)[0] == '#' {
// Ignore this line
}
}
+// LoadRulesFromFile loads just the syntax rules from a given file
+// Only the necessary rules are loaded when the buffer is opened.
+// If we load all the rules for every filetype when micro starts, there's a bit of lag
+// A rule just explains how to color certain regular expressions
+// Example: color comment "//.*"
+// This would color all strings that match the regex "//.*" in the comment color defined
+// by the colorscheme
func LoadRulesFromFile(text, filename string) []SyntaxRule {
lines := strings.Split(string(text), "\n")
// so map[3] represents the style of the third character
type SyntaxMatches [][]tcell.Style
-// Match takes a buffer and returns the syntax matches a map specifying how it should be syntax highlighted
-// We need to check the start-end regexes for the entire buffer every time Match is called, but for the
-// non start-end rules, we only have to update the updateLines provided by the view
+// Match takes a buffer and returns the syntax matches: a 2d array specifying how it should be syntax highlighted
+// We match the rules from up `synLinesUp` lines and down `synLinesDown` lines
func Match(v *View) SyntaxMatches {
buf := v.buf
rules := v.buf.rules
viewStart := v.topline
viewEnd := v.topline + v.height
- if viewEnd > len(buf.lines) {
- viewEnd = len(buf.lines)
+ if viewEnd > buf.numLines {
+ viewEnd = buf.numLines
}
- // updateStart := v.updateLines[0]
- // updateEnd := v.updateLines[1]
- //
- // if updateEnd > len(buf.lines) {
- // updateEnd = len(buf.lines)
- // }
- // if updateStart < 0 {
- // updateStart = 0
- // }
lines := buf.lines[viewStart:viewEnd]
- // updateLines := buf.lines[updateStart:updateEnd]
matches := make(SyntaxMatches, len(lines))
for i, line := range lines {
if totalStart < 0 {
totalStart = 0
}
- if totalEnd > len(buf.lines) {
- totalEnd = len(buf.lines)
+ if totalEnd > buf.numLines {
+ totalEnd = buf.numLines
}
str := strings.Join(buf.lines[totalStart:totalEnd], "\n")
if indicies := rule.regex.FindAllStringIndex(line, -1); indicies != nil {
for _, value := range indicies {
for i := value[0]; i < value[1]; i++ {
- // matches[lineN+updateStart][i] = rule.style
matches[lineN][i] = rule.style
}
}