]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/highlighter.go
Fix some issues with unicode handling
[micro.git] / cmd / micro / highlighter.go
index 956de1d75b46c219eb6295de81592e57afdba111..f636d87a8e2a4ce92ff1e44dbacabbe69b723bfa 100644 (file)
@@ -51,6 +51,7 @@ var preInstalledSynFiles = []string{
        "erb",
        "fish",
        "fortran",
+       "gdscript",
        "gentoo-ebuild",
        "gentoo-etc-portage",
        "git-commit",
@@ -58,6 +59,7 @@ var preInstalledSynFiles = []string{
        "git-rebase-todo",
        "glsl",
        "go",
+       "golo",
        "groff",
        "haml",
        "haskell",
@@ -70,6 +72,7 @@ var preInstalledSynFiles = []string{
        "keymap",
        "kickstart",
        "ledger",
+       "lilypond",
        "lisp",
        "lua",
        "makefile",
@@ -138,8 +141,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 {
@@ -301,8 +319,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)
                        }
@@ -395,6 +425,9 @@ func Match(v *View) SyntaxMatches {
 
        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
@@ -410,20 +443,19 @@ func Match(v *View) SyntaxMatches {
        str := strings.Join(buf.Lines(totalStart, totalEnd), "\n")
        startNum := ToCharPos(Loc{0, totalStart}, 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] = runePos(value[0], str) + startNum
                                        value[1] = runePos(value[1], str) + startNum
-                                       for i := value[0]; i < value[1]; i++ {
-                                               if i < toplineNum {
+                                       startLoc := FromCharPos(value[0], buf)
+                                       endLoc := FromCharPos(value[1], buf)
+                                       for curLoc := startLoc; curLoc.LessThan(endLoc); curLoc = curLoc.Move(1, buf) {
+                                               if curLoc.Y < v.Topline {
                                                        continue
                                                }
-                                               loc := FromCharPos(i, buf)
-                                               colNum, lineNum := loc.X, loc.Y
+                                               colNum, lineNum := curLoc.X, curLoc.Y
                                                if lineNum == -1 || colNum == -1 {
                                                        continue
                                                }
@@ -440,6 +472,7 @@ func Match(v *View) SyntaxMatches {
                                        for _, value := range indicies {
                                                start := runePos(value[0], line)
                                                end := runePos(value[1], line)
+                                               // messenger.Message(start, " ", end)
                                                for i := start; i < end; i++ {
                                                        matches[lineN][i] = rule.style
                                                }