]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/highlighter.go
Fix draw ordering
[micro.git] / cmd / micro / highlighter.go
index 7f75ad6917e5642390e03d31453fb81bdc20206a..32fe656324b4f54739f2c724f10a61ea709f1bd4 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",
@@ -50,6 +51,7 @@ var preInstalledSynFiles = []string{
        "erb",
        "fish",
        "fortran",
+       "gdscript",
        "gentoo-ebuild",
        "gentoo-etc-portage",
        "git-commit",
@@ -92,6 +94,7 @@ var preInstalledSynFiles = []string{
        "privoxy-filter",
        "puppet",
        "python",
+       "r",
        "reST",
        "rpmspec",
        "ruby",
@@ -116,8 +119,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 {
@@ -134,8 +139,23 @@ func LoadSyntaxFiles() {
 // This involves finding the regex for syntax and if it exists, the regex
 // for the header. Then we must get the text for the file and the filetype.
 func LoadSyntaxFilesFromDir(dir string) {
+       colorscheme = make(Colorscheme)
        InitColorscheme()
 
+       // Default style
+       defStyle = tcell.StyleDefault.
+               Foreground(tcell.ColorDefault).
+               Background(tcell.ColorDefault)
+
+       // There may be another default style defined in the colorscheme
+       // In that case we should use that one
+       if style, ok := colorscheme["default"]; ok {
+               defStyle = style
+       }
+       if screen != nil {
+               screen.SetStyle(defStyle)
+       }
+
        syntaxFiles = make(map[[2]*regexp.Regexp]FileTypeRules)
        files, _ := ioutil.ReadDir(dir)
        for _, f := range files {
@@ -161,12 +181,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 +194,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 +258,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")
 
@@ -282,8 +317,20 @@ func LoadRulesFromFile(text, filename string) []SyntaxRule {
                        // in which case we should look that up in the colorscheme
                        // They can also just give us a straight up color
                        st := defStyle
-                       if _, ok := colorscheme[color]; ok {
-                               st = colorscheme[color]
+                       groups := strings.Split(color, ".")
+                       if len(groups) > 1 {
+                               curGroup := ""
+                               for i, g := range groups {
+                                       if i != 0 {
+                                               curGroup += "."
+                                       }
+                                       curGroup += g
+                                       if style, ok := colorscheme[curGroup]; ok {
+                                               st = style
+                                       }
+                               }
+                       } else if style, ok := colorscheme[color]; ok {
+                               st = style
                        } else {
                                st = StringToStyle(color)
                        }
@@ -344,10 +391,10 @@ func LoadRulesFromFile(text, filename string) []SyntaxRule {
 // and returns them. It also returns the filetype of the file
 func GetRules(buf *Buffer) ([]SyntaxRule, string) {
        for r := range syntaxFiles {
-               if r[0] != nil && r[0].MatchString(buf.path) {
+               if r[0] != nil && r[0].MatchString(buf.Path) {
                        // Check if the syntax statement matches the extension
                        return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename), syntaxFiles[r].filetype
-               } else if r[1] != nil && r[1].MatchString(buf.lines[0]) {
+               } else if r[1] != nil && r[1].MatchString(buf.Line(0)) {
                        // Check if the header statement matches the first line
                        return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename), syntaxFiles[r].filetype
                }
@@ -359,62 +406,55 @@ 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
+       buf := v.Buf
+       rules := v.Buf.rules
 
-       viewStart := v.topline
-       viewEnd := v.topline + v.height
-       if viewEnd > len(buf.lines) {
-               viewEnd = len(buf.lines)
+       viewStart := v.Topline
+       viewEnd := v.Topline + v.height
+       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]
+       lines := buf.Lines(viewStart, viewEnd)
        matches := make(SyntaxMatches, len(lines))
 
        for i, line := range lines {
                matches[i] = make([]tcell.Style, len(line)+1)
+               for j, _ := range matches[i] {
+                       matches[i][j] = defStyle
+               }
        }
 
        // We don't actually check the entire buffer, just from synLinesUp to synLinesDown
-       totalStart := v.topline - synLinesUp
-       totalEnd := v.topline + v.height + synLinesDown
+       totalStart := v.Topline - synLinesUp
+       totalEnd := v.Topline + v.height + synLinesDown
        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")
-       startNum := ToCharPos(0, totalStart, v.buf)
+       str := strings.Join(buf.Lines(totalStart, totalEnd), "\n")
+       startNum := ToCharPos(Loc{0, totalStart}, v.Buf)
 
-       toplineNum := ToCharPos(0, v.topline, v.buf)
+       toplineNum := ToCharPos(Loc{0, v.Topline}, v.Buf)
 
        for _, rule := range rules {
                if rule.startend {
                        if indicies := rule.regex.FindAllStringIndex(str, -1); indicies != nil {
                                for _, value := range indicies {
-                                       value[0] += startNum
-                                       value[1] += startNum
+                                       value[0] = runePos(value[0], str) + startNum
+                                       value[1] = runePos(value[1], str) + startNum
                                        for i := value[0]; i < value[1]; i++ {
                                                if i < toplineNum {
                                                        continue
                                                }
-                                               colNum, lineNum := FromCharPosStart(toplineNum, 0, v.topline, i, buf)
+                                               loc := FromCharPos(i, buf)
+                                               colNum, lineNum := loc.X, loc.Y
                                                if lineNum == -1 || colNum == -1 {
                                                        continue
                                                }
@@ -429,8 +469,9 @@ func Match(v *View) SyntaxMatches {
                        for lineN, line := range lines {
                                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
+                                               start := runePos(value[0], line)
+                                               end := runePos(value[1], line)
+                                               for i := start; i < end; i++ {
                                                        matches[lineN][i] = rule.style
                                                }
                                        }