]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/highlighter.go
Small optimization
[micro.git] / cmd / micro / highlighter.go
index 7f75ad6917e5642390e03d31453fb81bdc20206a..bcf0aabdfcf6f91475072f2abdf35215a9a9ca91 100644 (file)
@@ -1,7 +1,7 @@
 package main
 
 import (
-       "github.com/gdamore/tcell"
+       "github.com/zyedidia/tcell"
        "io/ioutil"
        "path/filepath"
        "regexp"
@@ -29,6 +29,7 @@ type SyntaxRule struct {
 
 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",
@@ -92,6 +93,7 @@ var preInstalledSynFiles = []string{
        "privoxy-filter",
        "puppet",
        "python",
+       "r",
        "reST",
        "rpmspec",
        "ruby",
@@ -116,8 +118,10 @@ var preInstalledSynFiles = []string{
 
 // 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 {
@@ -161,12 +165,10 @@ func JoinRule(rule string) string {
        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")
@@ -176,10 +178,20 @@ func LoadSyntaxFile(text, filename string) {
        // 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
@@ -230,6 +242,13 @@ func LoadSyntaxFile(text, filename string) {
        }
 }
 
+// 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")
 
@@ -359,30 +378,19 @@ func GetRules(buf *Buffer) ([]SyntaxRule, string) {
 // 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 {
@@ -395,8 +403,8 @@ func Match(v *View) SyntaxMatches {
        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")
@@ -430,7 +438,6 @@ func Match(v *View) SyntaxMatches {
                                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
                                                }
                                        }