Skip to content

Commit 6cd39ef

Browse files
committed
buffer: Refactor UpdateRules() by creating further helper functions
- `findRealRuntimeSyntaxDef()` - `findRuntimeSyntaxDef()` This will reduce the length of this function again and thus improves the readability.
1 parent 089160a commit 6cd39ef

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

internal/buffer/buffer.go

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,34 @@ func parseDefFromFile(f config.RuntimeFile, header *highlight.Header) *highlight
712712
return syndef
713713
}
714714

715+
// findRealRuntimeSyntaxDef finds a specific syntax definition
716+
// in the user's custom syntax files
717+
func findRealRuntimeSyntaxDef(name string, header *highlight.Header) *highlight.Def {
718+
for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) {
719+
if f.Name() == name {
720+
syndef := parseDefFromFile(f, header)
721+
if syndef != nil {
722+
return syndef
723+
}
724+
}
725+
}
726+
return nil
727+
}
728+
729+
// findRuntimeSyntaxDef finds a specific syntax definition
730+
// in the runtime files
731+
func findRuntimeSyntaxDef(name string, header *highlight.Header) *highlight.Def {
732+
for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
733+
if f.Name() == name {
734+
syndef := parseDefFromFile(f, header)
735+
if syndef != nil {
736+
return syndef
737+
}
738+
}
739+
}
740+
return nil
741+
}
742+
715743
// UpdateRules updates the syntax rules and filetype for this buffer
716744
// This is called when the colorscheme changes
717745
func (b *Buffer) UpdateRules() {
@@ -878,17 +906,7 @@ func (b *Buffer) UpdateRules() {
878906

879907
if syntaxFile != "" && !foundDef {
880908
// we found a syntax file using a syntax header file
881-
for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
882-
if f.Name() == syntaxFile {
883-
syndef := parseDefFromFile(f, header)
884-
if syndef == nil {
885-
continue
886-
}
887-
888-
b.SyntaxDef = syndef
889-
break
890-
}
891-
}
909+
b.SyntaxDef = findRuntimeSyntaxDef(syntaxFile, header)
892910
}
893911

894912
if b.SyntaxDef != nil && highlight.HasIncludes(b.SyntaxDef) {
@@ -932,31 +950,10 @@ func (b *Buffer) UpdateRules() {
932950
b.Settings["filetype"] = b.SyntaxDef.FileType
933951
} else {
934952
// search for the default file in the user's custom syntax files
935-
for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) {
936-
if f.Name() == "default" {
937-
syndef := parseDefFromFile(f, nil)
938-
if syndef == nil {
939-
continue
940-
}
941-
942-
b.SyntaxDef = syndef
943-
break
944-
}
945-
}
946-
953+
b.SyntaxDef = findRealRuntimeSyntaxDef("default", nil)
947954
if b.SyntaxDef == nil {
948955
// search for the default file in the runtime files
949-
for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
950-
if f.Name() == "default" {
951-
syndef := parseDefFromFile(f, nil)
952-
if syndef == nil {
953-
continue
954-
}
955-
956-
b.SyntaxDef = syndef
957-
break
958-
}
959-
}
956+
b.SyntaxDef = findRuntimeSyntaxDef("default", nil)
960957
}
961958
}
962959
}

0 commit comments

Comments
 (0)