From 3cff613620556d8527e0f62d918501eebaaedf3f Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 21 Sep 2024 13:30:45 +0200 Subject: [PATCH] refactor(sshd_config): Migrating cursor to index position --- handlers/sshd_config/ast/parser_test.go | 16 +++++++++ .../fields/match-parser/full_test.go | 19 +++++----- .../fields/match-parser/match_fields.go | 36 ++++++++----------- handlers/sshd_config/handlers/completions.go | 4 ++- .../sshd_config/handlers/completions_match.go | 9 ++--- handlers/sshd_config/handlers/definition.go | 8 ++--- handlers/sshd_config/handlers/hover.go | 4 +-- .../lsp/text-document-completion.go | 4 +-- 8 files changed, 56 insertions(+), 44 deletions(-) diff --git a/handlers/sshd_config/ast/parser_test.go b/handlers/sshd_config/ast/parser_test.go index 2a024d5..b309380 100644 --- a/handlers/sshd_config/ast/parser_test.go +++ b/handlers/sshd_config/ast/parser_test.go @@ -303,6 +303,22 @@ Sample } +func TestIncompleteExample( + t *testing.T, +) { + input := utils.Dedent(` +MACs +`) + p := NewSSHConfig() + errors := p.Parse(input) + + if len(errors) != 0 { + t.Fatalf("Expected no errors, got %v", errors) + } + + println(p) +} + func TestComplexExample( t *testing.T, ) { diff --git a/handlers/sshd_config/fields/match-parser/full_test.go b/handlers/sshd_config/fields/match-parser/full_test.go index 33121ad..4f37c87 100644 --- a/handlers/sshd_config/fields/match-parser/full_test.go +++ b/handlers/sshd_config/fields/match-parser/full_test.go @@ -1,6 +1,7 @@ package matchparser import ( + "config-lsp/common" "testing" ) @@ -15,32 +16,32 @@ func TestFullExample( t.Fatalf("Failed to parse match: %v", errs) } - entry := match.GetEntryByCursor(0) + entry := match.GetEntryAtPosition(common.LSPCharacterAsCursorPosition(0)) if !(entry == match.Entries[0]) { t.Errorf("Expected entry at 0 to be %v, but got %v", match.Entries[0], entry) } - entry = match.GetEntryByCursor(5) + entry = match.GetEntryAtPosition(common.LSPCharacterAsCursorPosition(5)) if !(entry == match.Entries[0]) { t.Errorf("Expected entry at 5 to be %v, but got %v", match.Entries[0], entry) } - entry = match.GetEntryByCursor(13) + entry = match.GetEntryAtPosition(common.LSPCharacterAsCursorPosition(13)) if !(entry == match.Entries[0]) { t.Errorf("Expected entry at 13 to be %v, but got %v", match.Entries[1], entry) } - entry = match.GetEntryByCursor(16) + entry = match.GetEntryAtPosition(common.LSPCharacterAsCursorPosition(16)) if !(entry == match.Entries[1]) { t.Errorf("Expected entry at 16 to be %v, but got %v", match.Entries[1], entry) } - entry = match.GetEntryByCursor(24) + entry = match.GetEntryAtPosition(common.LSPCharacterAsCursorPosition(24)) if !(entry == match.Entries[1]) { t.Errorf("Expected entry at 24 to be %v, but got %v", match.Entries[2], entry) } - entry = match.GetEntryByCursor(36) + entry = match.GetEntryAtPosition(common.LSPCharacterAsCursorPosition(36)) if !(entry == match.Entries[2]) { t.Errorf("Expected entry at 36 to be %v, but got %v", match.Entries[2], entry) } @@ -57,17 +58,17 @@ func TestGetEntryForIncompleteExample( t.Fatalf("Failed to parse match: %v", errs) } - entry := match.GetEntryByCursor(0) + entry := match.GetEntryAtPosition(common.LSPCharacterAsCursorPosition(0)) if !(entry == match.Entries[0]) { t.Errorf("Expected entry at 0 to be %v, but got %v", match.Entries[0], entry) } - entry = match.GetEntryByCursor(4) + entry = match.GetEntryAtPosition(common.LSPCharacterAsCursorPosition(4)) if !(entry == match.Entries[0]) { t.Errorf("Expected entry at 4 to be %v, but got %v", match.Entries[0], entry) } - entry = match.GetEntryByCursor(5) + entry = match.GetEntryAtPosition(common.LSPCharacterAsCursorPosition(5)) if !(entry == match.Entries[0]) { t.Errorf("Expected entry at 5 to be %v, but got %v", match.Entries[0], entry) } diff --git a/handlers/sshd_config/fields/match-parser/match_fields.go b/handlers/sshd_config/fields/match-parser/match_fields.go index 547986d..ab30390 100644 --- a/handlers/sshd_config/fields/match-parser/match_fields.go +++ b/handlers/sshd_config/fields/match-parser/match_fields.go @@ -5,17 +5,17 @@ import ( "slices" ) -func (m Match) GetEntryByCursor(cursor common.CursorPosition) *MatchEntry { +func (m Match) GetEntryAtPosition(position common.Position) *MatchEntry { index, found := slices.BinarySearchFunc( m.Entries, - cursor, - func(current *MatchEntry, target common.CursorPosition) int { - if current.Start.IsAfterCursorPosition(target) { - return 1 + position, + func(current *MatchEntry, target common.Position) int { + if current.IsPositionAfterEnd(target) { + return -1 } - if current.End.IsBeforeCursorPosition(target) { - return -1 + if current.IsPositionBeforeStart(target) { + return 1 } return 0 @@ -31,25 +31,21 @@ func (m Match) GetEntryByCursor(cursor common.CursorPosition) *MatchEntry { return entry } -func (c MatchCriteria) IsCursorBetween(cursor uint32) bool { - return cursor >= c.Start.Character && cursor <= c.End.Character -} - -func (e MatchEntry) GetValueByCursor(cursor uint32) *MatchValue { +func (e MatchEntry) GetValueAtPosition(position common.Position) *MatchValue { if e.Values == nil { return nil } index, found := slices.BinarySearchFunc( e.Values.Values, - cursor, - func(current *MatchValue, target uint32) int { - if target < current.Start.Character { - return 1 + position, + func(current *MatchValue, target common.Position) int { + if current.IsPositionAfterEnd(target) { + return -1 } - if target > current.End.Character { - return -1 + if current.IsPositionBeforeStart(target) { + return 1 } return 0 @@ -64,7 +60,3 @@ func (e MatchEntry) GetValueByCursor(cursor uint32) *MatchValue { return value } - -func (v MatchValues) IsCursorBetween(cursor uint32) bool { - return cursor >= v.Start.Character && cursor <= v.End.Character -} diff --git a/handlers/sshd_config/handlers/completions.go b/handlers/sshd_config/handlers/completions.go index bd1c7e6..8314f3c 100644 --- a/handlers/sshd_config/handlers/completions.go +++ b/handlers/sshd_config/handlers/completions.go @@ -91,13 +91,15 @@ func GetOptionCompletions( return option.FetchCompletions("", 0), nil } + // Hello wo|rld line := entry.OptionValue.Value.Raw // NEW: docvalues index return option.FetchCompletions( line, common.DeprecatedImprovedCursorToIndex( - entry.OptionValue.Start.GetRelativeCursorPosition(cursor), + cursor, line, + entry.OptionValue.Start.Character, ), ), nil } diff --git a/handlers/sshd_config/handlers/completions_match.go b/handlers/sshd_config/handlers/completions_match.go index 17a8900..4c4707b 100644 --- a/handlers/sshd_config/handlers/completions_match.go +++ b/handlers/sshd_config/handlers/completions_match.go @@ -21,9 +21,9 @@ func getMatchCompletions( return completions, nil } - entry := match.GetEntryByCursor(cursor) + entry := match.GetEntryAtPosition(cursor) - if entry == nil || entry.Criteria.ContainsCursorPosition(cursor) { + if entry == nil || entry.Criteria.ContainsPosition(cursor) { return getMatchCriteriaCompletions(), nil } @@ -78,7 +78,7 @@ func getMatchValueCompletions( entry *matchparser.MatchEntry, cursor common.CursorPosition, ) []protocol.CompletionItem { - value := entry.GetValueByCursor(entry.End.Character) + value := entry.GetValueAtPosition(cursor) var line string var relativeCursor uint32 @@ -86,8 +86,9 @@ func getMatchValueCompletions( if value != nil { line = value.Value.Raw relativeCursor = common.DeprecatedImprovedCursorToIndex( - value.Start.GetRelativeCursorPosition(cursor), + cursor, line, + value.Start.Character, ) } else { line = "" diff --git a/handlers/sshd_config/handlers/definition.go b/handlers/sshd_config/handlers/definition.go index 02cf5de..ad064ea 100644 --- a/handlers/sshd_config/handlers/definition.go +++ b/handlers/sshd_config/handlers/definition.go @@ -17,12 +17,12 @@ func GetIncludeOptionLocation( include.Values, index, func(current *indexes.SSHDIndexIncludeValue, target common.IndexPosition) int { - if current.Start.IsAfterIndexPosition(target) { - return 1 + if current.IsPositionAfterEnd(target) { + return -1 } - if current.End.IsBeforeIndexPosition(target) { - return -1 + if current.IsPositionBeforeStart(target) { + return 1 } return 0 diff --git a/handlers/sshd_config/handlers/hover.go b/handlers/sshd_config/handlers/hover.go index 8e41121..0a56632 100644 --- a/handlers/sshd_config/handlers/hover.go +++ b/handlers/sshd_config/handlers/hover.go @@ -29,7 +29,7 @@ func GetHoverInfoForOption( } } - if option.Key.ContainsIndexPosition(index) { + if option.Key.ContainsPosition(index) { if docValue != nil { contents := []string{ "## " + option.Key.Key, @@ -53,7 +53,7 @@ func GetHoverInfoForOption( } } - if option.OptionValue != nil && option.OptionValue.ContainsIndexPosition(index) { + if option.OptionValue != nil && option.OptionValue.ContainsPosition(index) { line := option.OptionValue.Value.Raw contents := docValue.FetchHoverInfo( line, diff --git a/handlers/sshd_config/lsp/text-document-completion.go b/handlers/sshd_config/lsp/text-document-completion.go index 24e592a..d779ba2 100644 --- a/handlers/sshd_config/lsp/text-document-completion.go +++ b/handlers/sshd_config/lsp/text-document-completion.go @@ -27,7 +27,7 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa if entry == nil || entry.Separator == nil || entry.Key == nil || - entry.Key.Start.IsAfterCursorPosition(cursor) { + entry.Key.IsPositionBeforeEnd(cursor) { return handlers.GetRootCompletions( d, @@ -37,7 +37,7 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa ) } - if entry.Separator != nil && entry.Separator.End.IsBeforeCursorPosition(cursor) { + if entry.Separator != nil && entry.OptionValue.IsPositionAfterStart(cursor) { return handlers.GetOptionCompletions( d, entry,