]> git.lizzy.rs Git - micro.git/commitdiff
Support includes
authorZachary Yedidia <zyedidia@gmail.com>
Sun, 29 Dec 2019 02:57:03 +0000 (21:57 -0500)
committerZachary Yedidia <zyedidia@gmail.com>
Sun, 29 Dec 2019 02:57:03 +0000 (21:57 -0500)
internal/buffer/buffer.go
pkg/highlight/parser.go

index d332aac6dc122bf058f7062a1022ce2b4b2247b0..a5eca5aa319f9c48209d126748e39944548ff6fd 100644 (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 {
index 621d465f8953551b7c5b1fd53f5bd87d2026a751..9364218ae5a73d7c78ef17856635f9162bd2c86b 100644 (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) {