fix(ssh_config): Fix all argument completions

This commit is contained in:
Myzel394 2024-09-29 20:46:52 +02:00
parent 14e0be08a6
commit 9183a3f004
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
4 changed files with 31 additions and 3 deletions

View File

@ -81,7 +81,7 @@ func checkMatch(
allEntry := allEntries[0]
previousEntry := m.GetPreviousEntry(allEntry)
if previousEntry != nil && !utils.KeyExists(fields.MatchAllOptionAllowedPreviousOptions, previousEntry.Criteria.Type) {
if previousEntry != nil && !utils.KeyExists(fields.MatchAllArgumentAllowedPreviousOptions, previousEntry.Criteria.Type) {
errs = append(errs, common.LSPError{
Range: allEntry.LocationRange,
Err: errors.New("'all' should either be the first entry or immediately follow 'final' or 'canonical'"),

View File

@ -27,7 +27,7 @@ var MatchValueFieldMap = map[matchparser.MatchCriteriaType]docvalues.DeprecatedV
matchparser.MatchCriteriaTypeLocalUser: MatchTypeLocalUserField,
}
var MatchAllOptionAllowedPreviousOptions = map[matchparser.MatchCriteriaType]struct{}{
var MatchAllArgumentAllowedPreviousOptions = map[matchparser.MatchCriteriaType]struct{}{
matchparser.MatchCriteriaTypeCanonical: {},
matchparser.MatchCriteriaTypeFinal: {},
}

View File

@ -5,6 +5,7 @@ import (
sshconfig "config-lsp/handlers/ssh_config"
"config-lsp/handlers/ssh_config/fields"
matchparser "config-lsp/handlers/ssh_config/match-parser"
"config-lsp/utils"
protocol "github.com/tliron/glsp/protocol_3_16"
)
@ -24,7 +25,21 @@ func getMatchCompletions(
entry := match.GetEntryAtPosition(cursor)
if entry == nil || entry.Criteria.ContainsPosition(cursor) {
return getMatchCriteriaCompletions(), nil
completions := getMatchCriteriaCompletions()
var showAllArgument = true
previousEntry := match.GetPreviousEntryFromCursor(cursor)
if previousEntry != nil && !utils.KeyExists(fields.MatchAllArgumentAllowedPreviousOptions, previousEntry.Criteria.Type) {
showAllArgument = false
}
if showAllArgument {
completions = append(completions, getMatchAllKeywordCompletion())
}
return completions, nil
}
return getMatchValueCompletions(entry, cursor), nil

View File

@ -53,6 +53,19 @@ func (m Match) GetPreviousEntry(e *MatchEntry) *MatchEntry {
return m.Entries[index-1]
}
func (m Match) GetPreviousEntryFromCursor(cursor common.CursorPosition) *MatchEntry {
entries := slices.Clone(m.Entries)
slices.Reverse(entries)
for _, entry := range entries {
if entry.IsPositionAfterStart(cursor) {
return entry
}
}
return nil
}
func (e MatchEntry) GetValueAtPosition(position common.Position) *MatchValue {
if e.Values == nil {
return nil