Skip to content

Commit

Permalink
buffer: Refactor UpdateRules() by creating further helper functions
Browse files Browse the repository at this point in the history
- `FindRealRuntimeFile()`
- `FindRuntimeFile()`

This will reduce the length of this function again and thus improves the
readability.
  • Loading branch information
JoeKar committed Apr 14, 2024
1 parent 06ade60 commit 27beb10
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
37 changes: 3 additions & 34 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,17 +846,7 @@ func (b *Buffer) UpdateRules() {

if syntaxFile != "" && !foundDef {
// we found a syntax file using a syntax header file
for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
if f.Name() == syntaxFile {
syndef := highlight.ParseDefFromFile(f, header)
if syndef == nil {
continue
}

b.SyntaxDef = syndef
break
}
}
b.SyntaxDef = highlight.FindRuntimeFile(syntaxFile, header)
}

if b.SyntaxDef != nil && highlight.HasIncludes(b.SyntaxDef) {
Expand Down Expand Up @@ -900,31 +890,10 @@ func (b *Buffer) UpdateRules() {
b.Settings["filetype"] = b.SyntaxDef.FileType
} else {
// search for the default file in the user's custom syntax files
for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) {
if f.Name() == "default" {
syndef := highlight.ParseDefFromFile(f, nil)
if syndef == nil {
continue
}

b.SyntaxDef = syndef
break
}
}

b.SyntaxDef = highlight.FindRealRuntimeFile("default", nil)
if b.SyntaxDef == nil {
// search for the default file in the runtime files
for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
if f.Name() == "default" {
syndef := highlight.ParseDefFromFile(f, nil)
if syndef == nil {
continue
}

b.SyntaxDef = syndef
break
}
}
b.SyntaxDef = highlight.FindRuntimeFile("default", nil)
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/highlight/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,36 @@ func ParseDefFromFile(f config.RuntimeFile, header *Header) *Def {
return syndef
}

// FindRealRuntimeFile can be used to find a specific definition in the user's
// custom syntax files
func FindRealRuntimeFile(name string, header *Header) *Def {
for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) {
if f.Name() == name {
syndef := ParseDefFromFile(f, header)
if syndef == nil {
continue
}
return syndef
}
}
return nil
}

// FindRealRuntimeFile can be used to find a specific definition in the runtime
// files
func FindRuntimeFile(name string, header *Header) *Def {
for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
if f.Name() == name {
syndef := ParseDefFromFile(f, header)
if syndef == nil {
continue
}
return syndef
}
}
return nil
}

// HasIncludes returns whether this syntax def has any include statements
func HasIncludes(d *Def) bool {
hasIncludes := len(d.rules.includes) > 0
Expand Down

0 comments on commit 27beb10

Please sign in to comment.