From a92fe606839dce495a7be3cc0d03bc07c9c4cc24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Mon, 25 Mar 2024 20:41:32 +0100 Subject: [PATCH 1/2] infocomplete: Complete filetypes (follow-up) The first shot of the feature unfortunately completed the *.yaml file names instead of the included filetypes. This will be corrected with this follow up. --- internal/action/infocomplete.go | 42 ++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/internal/action/infocomplete.go b/internal/action/infocomplete.go index 11e60918e..94e624cfe 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()) + // Since we need to complete the file type instead of the file name + // we need to parse the RTSyntax* files and search for a matching FileType. + // The following logic can be simplified to the previos state again, + // in the moment the file names will represent the file types directly. + 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) } } From 9eb93fc78b2136a06a44db76af38472a739dec46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Tue, 26 Mar 2024 18:55:40 +0100 Subject: [PATCH 2/2] infocomplete: Correct comment of filetypeComplete according to review hint Co-authored-by: Dmytro Maluka --- internal/action/infocomplete.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/action/infocomplete.go b/internal/action/infocomplete.go index 94e624cfe..c373e923e 100644 --- a/internal/action/infocomplete.go +++ b/internal/action/infocomplete.go @@ -82,10 +82,10 @@ func colorschemeComplete(input string) (string, []string) { func filetypeComplete(input string) (string, []string) { var suggestions []string - // Since we need to complete the file type instead of the file name - // we need to parse the RTSyntax* files and search for a matching FileType. - // The following logic can be simplified to the previos state again, - // in the moment the file names will represent the file types directly. + // 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 {