Skip to content

Commit

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

This will reduce the length of this function again and thus improves the
readability.
  • Loading branch information
JoeKar committed Apr 17, 2024
1 parent 5270008 commit 82c3d01
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,36 @@ func parseDefFromFile(f config.RuntimeFile, header *highlight.Header) *highlight
return syndef
}

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

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

// UpdateRules updates the syntax rules and filetype for this buffer
// This is called when the colorscheme changes
func (b *Buffer) UpdateRules() {
Expand Down Expand Up @@ -877,17 +907,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 := parseDefFromFile(f, header)
if syndef == nil {
continue
}

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

if b.SyntaxDef != nil && highlight.HasIncludes(b.SyntaxDef) {
Expand Down Expand Up @@ -931,31 +951,11 @@ 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 := parseDefFromFile(f, nil)
if syndef == nil {
continue
}

b.SyntaxDef = syndef
break
}
}

name := "default"
b.SyntaxDef = findRealRuntimeSyntaxDef(name, 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 := parseDefFromFile(f, nil)
if syndef == nil {
continue
}

b.SyntaxDef = syndef
break
}
}
b.SyntaxDef = findRuntimeSyntaxDef(name, nil)
}
}
}
Expand Down

0 comments on commit 82c3d01

Please sign in to comment.