From 06182ddda76863616f7f4e0ea4a29ccb3562fb8b Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 18 Sep 2024 00:13:43 +0200 Subject: [PATCH] fix(sshd_config): Bugfixes --- common/lsp.go | 2 +- .../sshd_config/ast/sshd_config_fields.go | 24 +++++++++++++++++ handlers/sshd_config/handlers/completions.go | 27 ++++++++++++++----- .../lsp/text-document-completion.go | 3 +-- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/common/lsp.go b/common/lsp.go index eede1be..29c0279 100644 --- a/common/lsp.go +++ b/common/lsp.go @@ -1,5 +1,5 @@ package common func CursorToCharacterIndex(cursor uint32) uint32 { - return max(0, cursor-1) + return max(1, cursor) - 1 } diff --git a/handlers/sshd_config/ast/sshd_config_fields.go b/handlers/sshd_config/ast/sshd_config_fields.go index ce760bd..97cb4b7 100644 --- a/handlers/sshd_config/ast/sshd_config_fields.go +++ b/handlers/sshd_config/ast/sshd_config_fields.go @@ -64,3 +64,27 @@ func (c SSHDConfig) FindOption(line uint32) (*SSHDOption, *SSHDMatchBlock) { return nil, nil } + +func (c SSHDConfig) GetAllOptions() []*SSHDOption { + options := make( + []*SSHDOption, + 0, + // Approximation, this does not need to be exact + c.Options.Size()+10, + ) + + for _, rawEntry := range c.Options.Values() { + switch entry := rawEntry.(type) { + case *SSHDOption: + options = append(options, entry) + case *SSHDMatchBlock: + options = append(options, entry.MatchEntry) + + for _, rawOption := range entry.Options.Values() { + options = append(options, rawOption.(*SSHDOption)) + } + } + } + + return options +} diff --git a/handlers/sshd_config/handlers/completions.go b/handlers/sshd_config/handlers/completions.go index e076030..aafbb99 100644 --- a/handlers/sshd_config/handlers/completions.go +++ b/handlers/sshd_config/handlers/completions.go @@ -18,16 +18,31 @@ func GetRootCompletions( ) ([]protocol.CompletionItem, error) { kind := protocol.CompletionItemKindField - availableOptions := make(map[string]docvalues.DocumentationValue) + availableOptions := make(map[string]docvalues.DocumentationValue, 0) if parentMatchBlock == nil { - availableOptions = fields.Options - } else { - for option := range fields.MatchAllowedOptions { - if opt, found := fields.Options[option]; found { - availableOptions[option] = opt + for key, option := range fields.Options { + if _, found := d.Indexes.AllOptionsPerName[key]; !found { + availableOptions[key] = option } } + } else { + for key := range fields.MatchAllowedOptions { + if option, found := fields.Options[key]; found { + if _, found := d.Indexes.AllOptionsPerName[key]; !found { + availableOptions[key] = option + } + } + } + } + + // Remove all fields that are already present and are not allowed to be duplicated + for _, option := range d.Config.GetAllOptions() { + if _, found := fields.AllowedDuplicateOptions[option.Key.Key]; found { + continue + } + + delete(availableOptions, option.Key.Key) } return utils.MapMapToSlice( diff --git a/handlers/sshd_config/lsp/text-document-completion.go b/handlers/sshd_config/lsp/text-document-completion.go index e91deaf..fa13229 100644 --- a/handlers/sshd_config/lsp/text-document-completion.go +++ b/handlers/sshd_config/lsp/text-document-completion.go @@ -1,7 +1,6 @@ package lsp import ( - "config-lsp/common" sshdconfig "config-lsp/handlers/sshd_config" "config-lsp/handlers/sshd_config/handlers" "regexp" @@ -14,7 +13,7 @@ var isEmptyPattern = regexp.MustCompile(`^\s*$`) func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionParams) (any, error) { line := params.Position.Line - cursor := common.CursorToCharacterIndex(params.Position.Character) + cursor := params.Position.Character d := sshdconfig.DocumentParserMap[params.TextDocument.URI]