compiler: Prefer specific grammar formats (#4012)

* compiler: Simplify the Dockerfile

* compiler: Prefer grammar extensions based on type
This commit is contained in:
Vicent Martí
2018-01-29 14:40:23 +01:00
committed by GitHub
parent 6220286f42
commit a7f835a653
2 changed files with 43 additions and 18 deletions

View File

@@ -6,6 +6,7 @@ import (
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
)
@@ -14,14 +15,43 @@ type fsLoader struct {
abspath string
}
var preferredGrammars = map[string]int{
".tmlanguage": 0,
".cson": 1,
".json": 1,
".plist": 2,
".yaml-tmlanguage": 3,
}
func findPreferredExtension(ext []string) string {
if len(ext) > 1 {
sort.Slice(ext, func(i, j int) bool {
a := strings.ToLower(ext[i])
b := strings.ToLower(ext[j])
return preferredGrammars[a] < preferredGrammars[b]
})
}
return ext[0]
}
func (l *fsLoader) findGrammars() (files []string, err error) {
grammars := make(map[string][]string)
err = filepath.Walk(l.abspath,
func(path string, info os.FileInfo, err error) error {
if err == nil && isValidGrammar(path, info) {
files = append(files, path)
ext := filepath.Ext(path)
base := path[0 : len(path)-len(ext)]
grammars[base] = append(grammars[base], ext)
}
return nil
})
for base, ext := range grammars {
pref := findPreferredExtension(ext)
files = append(files, base+pref)
}
return
}