diff --git a/internal/action/infocomplete.go b/internal/action/infocomplete.go index 11e60918..c373e923 100644 --- a/internal/action/infocomplete.go +++ b/internal/action/infocomplete.go @@ -8,6 +8,7 @@ import ( "github.com/zyedidia/micro/v2/internal/buffer" "github.com/zyedidia/micro/v2/internal/config" "github.com/zyedidia/micro/v2/internal/util" + "github.com/zyedidia/micro/v2/pkg/highlight" ) // This file is meant (for now) for autocompletion in command mode, not @@ -81,9 +82,44 @@ func colorschemeComplete(input string) (string, []string) { func filetypeComplete(input string) (string, []string) { var suggestions []string - for _, f := range config.ListRuntimeFiles(config.RTSyntax) { - if strings.HasPrefix(f.Name(), input) { - suggestions = append(suggestions, f.Name()) + // We cannot match filetypes just by names of syntax files, + // since those names may be different from the actual filetype values + // specified inside syntax files (e.g. "c++" filetype in cpp.yaml). + // So we need to parse filetype values out of those files. + for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) { + data, err := f.Data() + if err != nil { + continue + } + header, err := highlight.MakeHeaderYaml(data) + if err != nil { + continue + } + // Prevent duplicated defaults + if header.FileType == "off" || header.FileType == "unknown" { + continue + } + if strings.HasPrefix(header.FileType, input) { + suggestions = append(suggestions, header.FileType) + } + } +headerLoop: + for _, f := range config.ListRuntimeFiles(config.RTSyntaxHeader) { + data, err := f.Data() + if err != nil { + continue + } + header, err := highlight.MakeHeader(data) + if err != nil { + continue + } + for _, v := range suggestions { + if v == header.FileType { + continue headerLoop + } + } + if strings.HasPrefix(header.FileType, input) { + suggestions = append(suggestions, header.FileType) } }