Support includes

This commit is contained in:
Zachary Yedidia
2019-12-28 21:57:03 -05:00
parent a61616d79e
commit 5b52b8a60f
2 changed files with 56 additions and 3 deletions

View File

@@ -533,9 +533,28 @@ func (b *Buffer) UpdateRules() {
}
// TODO: includes
// if b.SyntaxDef != nil {
// highlight.ResolveIncludes(b.SyntaxDef, files)
// }
if b.SyntaxDef != nil && highlight.HasIncludes(b.SyntaxDef) {
includes := highlight.GetIncludes(b.SyntaxDef)
var files []*highlight.File
for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
data, _ := f.Data()
header, _ := highlight.MakeHeaderYaml(data)
for _, i := range includes {
if header.FileType == i {
file, _ := highlight.ParseFile(data)
files = append(files, file)
break
}
}
if len(files) >= len(includes) {
break
}
}
highlight.ResolveIncludes(b.SyntaxDef, files)
}
if b.Highlighter == nil || syntaxFile != "" {
if b.SyntaxDef != nil {

View File

@@ -220,6 +220,40 @@ func ParseDef(f *File, header *Header) (s *Def, err error) {
return s, err
}
// HasIncludes returns whether this syntax def has any include statements
func HasIncludes(d *Def) bool {
hasIncludes := len(d.rules.includes) > 0
for _, r := range d.rules.regions {
hasIncludes = hasIncludes || hasIncludesInRegion(r)
}
return hasIncludes
}
func hasIncludesInRegion(region *region) bool {
hasIncludes := len(region.rules.includes) > 0
for _, r := range region.rules.regions {
hasIncludes = hasIncludes || hasIncludesInRegion(r)
}
return hasIncludes
}
// GetIncludes returns a list of filetypes that are included by this syntax def
func GetIncludes(d *Def) []string {
includes := d.rules.includes
for _, r := range d.rules.regions {
includes = append(includes, getIncludesInRegion(r)...)
}
return includes
}
func getIncludesInRegion(region *region) []string {
includes := region.rules.includes
for _, r := range region.rules.regions {
includes = append(includes, getIncludesInRegion(r)...)
}
return includes
}
// ResolveIncludes will sort out the rules for including other filetypes
// You should call this after parsing all the Defs
func ResolveIncludes(def *Def, files []*File) {