From 5b52b8a60f4b6f02a2eb845fb6456703462f4f68 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 28 Dec 2019 21:57:03 -0500 Subject: [PATCH] Support includes --- internal/buffer/buffer.go | 25 ++++++++++++++++++++++--- pkg/highlight/parser.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/internal/buffer/buffer.go b/internal/buffer/buffer.go index d332aac6..a5eca5aa 100644 --- a/internal/buffer/buffer.go +++ b/internal/buffer/buffer.go @@ -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 { diff --git a/pkg/highlight/parser.go b/pkg/highlight/parser.go index 621d465f..9364218a 100644 --- a/pkg/highlight/parser.go +++ b/pkg/highlight/parser.go @@ -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) {