Skip to content

Commit

Permalink
fix(sshd_config): Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Sep 17, 2024
1 parent 942e247 commit 06182dd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion common/lsp.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package common

func CursorToCharacterIndex(cursor uint32) uint32 {
return max(0, cursor-1)
return max(1, cursor) - 1
}
24 changes: 24 additions & 0 deletions handlers/sshd_config/ast/sshd_config_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
25 changes: 20 additions & 5 deletions handlers/sshd_config/handlers/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,33 @@ 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
for key, option := range fields.Options {
if _, found := d.Indexes.AllOptionsPerName[key]; !found {
availableOptions[key] = option
}
}
} else {
for option := range fields.MatchAllowedOptions {
if opt, found := fields.Options[option]; found {
availableOptions[option] = opt
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(
availableOptions,
func(name string, doc docvalues.DocumentationValue) protocol.CompletionItem {
Expand Down
3 changes: 1 addition & 2 deletions handlers/sshd_config/lsp/text-document-completion.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lsp

import (
"config-lsp/common"
sshdconfig "config-lsp/handlers/sshd_config"
"config-lsp/handlers/sshd_config/handlers"
"regexp"
Expand All @@ -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]

Expand Down

0 comments on commit 06182dd

Please sign in to comment.