mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-12-08 20:38:47 +00:00
compiler: Prefer specific grammar formats (#4012)
* compiler: Simplify the Dockerfile * compiler: Prefer grammar extensions based on type
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user