X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2Fhighlighter.go;h=c71870e50f4ae7da7e653e3ddf1f100d21d399ef;hb=3ecdd96931ac75039105b5a4ac2fdd3b5526dbf6;hp=8f6a6c3824e4d5afcfb7b4bba92db21fed1cee95;hpb=c679500d06429aea0061437e672e1807268cdf9b;p=micro.git diff --git a/cmd/micro/highlighter.go b/cmd/micro/highlighter.go index 8f6a6c38..c71870e5 100644 --- a/cmd/micro/highlighter.go +++ b/cmd/micro/highlighter.go @@ -1,11 +1,10 @@ package main import ( - "github.com/zyedidia/tcell" - "io/ioutil" - "path/filepath" "regexp" "strings" + + "github.com/zyedidia/tcell" ) // FileTypeRules represents a complete set of syntax rules for a filetype @@ -29,150 +28,16 @@ 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", - "arduino", - "asciidoc", - "asm", - "awk", - "c", - "caddyfile", - "cmake", - "coffeescript", - "colortest", - "conf", - "conky", - "csharp", - "css", - "cython", - "d", - "dart", - "dot", - "erb", - "fish", - "fortran", - "gdscript", - "gentoo-ebuild", - "gentoo-etc-portage", - "git-commit", - "git-config", - "git-rebase-todo", - "glsl", - "go", - "golo", - "groff", - "haml", - "haskell", - "html", - "ini", - "inputrc", - "java", - "javascript", - "json", - "keymap", - "kickstart", - "ledger", - "lilypond", - "lisp", - "lua", - "makefile", - "man", - "markdown", - "mpdconf", - "micro", - "nanorc", - "nginx", - "ocaml", - "patch", - "peg", - "perl", - "perl6", - "php", - "pkg-config", - "pkgbuild", - "po", - "pov", - "privoxy-action", - "privoxy-config", - "privoxy-filter", - "puppet", - "python", - "r", - "reST", - "rpmspec", - "ruby", - "rust", - "scala", - "sed", - "sh", - "sls", - "sql", - "swift", - "systemd", - "tcl", - "tex", - "vala", - "vi", - "xml", - "xresources", - "yaml", - "yum", - "zsh", -} - // 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 { - TermMessage("Unable to load pre-installed syntax file " + filetype) - continue - } - - LoadSyntaxFile(string(data), filetype+".micro") - } -} - -// LoadSyntaxFilesFromDir loads the syntax files from a specified directory -// To load the syntax files, we must fill the `syntaxFiles` map -// 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 { - if filepath.Ext(f.Name()) == ".micro" { - filename := dir + "/" + f.Name() - text, err := ioutil.ReadFile(filename) - - if err != nil { - TermMessage("Error loading syntax file " + filename + ": " + err.Error()) - return - } - LoadSyntaxFile(string(text), filename) + for _, f := range ListRuntimeFiles(RTSyntax) { + data, err := f.Data() + if err != nil { + TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error()) + } else { + LoadSyntaxFile(string(data), f.Name()) } } } @@ -394,13 +259,16 @@ func LoadRulesFromFile(text, filename string) []SyntaxRule { // FindFileType finds the filetype for the given buffer func FindFileType(buf *Buffer) string { + for r := range syntaxFiles { + if r[1] != nil && r[1].MatchString(buf.Line(0)) { + // The header statement matches the first line + return syntaxFiles[r].filetype + } + } for r := range syntaxFiles { if r[0] != nil && r[0].MatchString(buf.Path) { // The syntax statement matches the extension return syntaxFiles[r].filetype - } else if r[1] != nil && r[1].MatchString(buf.Line(0)) { - // The header statement matches the first line - return syntaxFiles[r].filetype } } return "Unknown"