refactor(sshd_config): Migrating cursor to index position

This commit is contained in:
Myzel394 2024-09-21 13:30:45 +02:00
parent 41472c1a58
commit 3cff613620
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
8 changed files with 56 additions and 44 deletions

View File

@ -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( func TestComplexExample(
t *testing.T, t *testing.T,
) { ) {

View File

@ -1,6 +1,7 @@
package matchparser package matchparser
import ( import (
"config-lsp/common"
"testing" "testing"
) )
@ -15,32 +16,32 @@ func TestFullExample(
t.Fatalf("Failed to parse match: %v", errs) t.Fatalf("Failed to parse match: %v", errs)
} }
entry := match.GetEntryByCursor(0) entry := match.GetEntryAtPosition(common.LSPCharacterAsCursorPosition(0))
if !(entry == match.Entries[0]) { if !(entry == match.Entries[0]) {
t.Errorf("Expected entry at 0 to be %v, but got %v", match.Entries[0], entry) 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]) { if !(entry == match.Entries[0]) {
t.Errorf("Expected entry at 5 to be %v, but got %v", match.Entries[0], entry) 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]) { if !(entry == match.Entries[0]) {
t.Errorf("Expected entry at 13 to be %v, but got %v", match.Entries[1], entry) 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]) { if !(entry == match.Entries[1]) {
t.Errorf("Expected entry at 16 to be %v, but got %v", match.Entries[1], entry) 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]) { if !(entry == match.Entries[1]) {
t.Errorf("Expected entry at 24 to be %v, but got %v", match.Entries[2], entry) 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]) { if !(entry == match.Entries[2]) {
t.Errorf("Expected entry at 36 to be %v, but got %v", match.Entries[2], entry) 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) t.Fatalf("Failed to parse match: %v", errs)
} }
entry := match.GetEntryByCursor(0) entry := match.GetEntryAtPosition(common.LSPCharacterAsCursorPosition(0))
if !(entry == match.Entries[0]) { if !(entry == match.Entries[0]) {
t.Errorf("Expected entry at 0 to be %v, but got %v", match.Entries[0], entry) 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]) { if !(entry == match.Entries[0]) {
t.Errorf("Expected entry at 4 to be %v, but got %v", match.Entries[0], entry) 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]) { if !(entry == match.Entries[0]) {
t.Errorf("Expected entry at 5 to be %v, but got %v", match.Entries[0], entry) t.Errorf("Expected entry at 5 to be %v, but got %v", match.Entries[0], entry)
} }

View File

@ -5,17 +5,17 @@ import (
"slices" "slices"
) )
func (m Match) GetEntryByCursor(cursor common.CursorPosition) *MatchEntry { func (m Match) GetEntryAtPosition(position common.Position) *MatchEntry {
index, found := slices.BinarySearchFunc( index, found := slices.BinarySearchFunc(
m.Entries, m.Entries,
cursor, position,
func(current *MatchEntry, target common.CursorPosition) int { func(current *MatchEntry, target common.Position) int {
if current.Start.IsAfterCursorPosition(target) { if current.IsPositionAfterEnd(target) {
return 1 return -1
} }
if current.End.IsBeforeCursorPosition(target) { if current.IsPositionBeforeStart(target) {
return -1 return 1
} }
return 0 return 0
@ -31,25 +31,21 @@ func (m Match) GetEntryByCursor(cursor common.CursorPosition) *MatchEntry {
return entry return entry
} }
func (c MatchCriteria) IsCursorBetween(cursor uint32) bool { func (e MatchEntry) GetValueAtPosition(position common.Position) *MatchValue {
return cursor >= c.Start.Character && cursor <= c.End.Character
}
func (e MatchEntry) GetValueByCursor(cursor uint32) *MatchValue {
if e.Values == nil { if e.Values == nil {
return nil return nil
} }
index, found := slices.BinarySearchFunc( index, found := slices.BinarySearchFunc(
e.Values.Values, e.Values.Values,
cursor, position,
func(current *MatchValue, target uint32) int { func(current *MatchValue, target common.Position) int {
if target < current.Start.Character { if current.IsPositionAfterEnd(target) {
return 1 return -1
} }
if target > current.End.Character { if current.IsPositionBeforeStart(target) {
return -1 return 1
} }
return 0 return 0
@ -64,7 +60,3 @@ func (e MatchEntry) GetValueByCursor(cursor uint32) *MatchValue {
return value return value
} }
func (v MatchValues) IsCursorBetween(cursor uint32) bool {
return cursor >= v.Start.Character && cursor <= v.End.Character
}

View File

@ -91,13 +91,15 @@ func GetOptionCompletions(
return option.FetchCompletions("", 0), nil return option.FetchCompletions("", 0), nil
} }
// Hello wo|rld
line := entry.OptionValue.Value.Raw line := entry.OptionValue.Value.Raw
// NEW: docvalues index // NEW: docvalues index
return option.FetchCompletions( return option.FetchCompletions(
line, line,
common.DeprecatedImprovedCursorToIndex( common.DeprecatedImprovedCursorToIndex(
entry.OptionValue.Start.GetRelativeCursorPosition(cursor), cursor,
line, line,
entry.OptionValue.Start.Character,
), ),
), nil ), nil
} }

View File

@ -21,9 +21,9 @@ func getMatchCompletions(
return completions, nil 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 return getMatchCriteriaCompletions(), nil
} }
@ -78,7 +78,7 @@ func getMatchValueCompletions(
entry *matchparser.MatchEntry, entry *matchparser.MatchEntry,
cursor common.CursorPosition, cursor common.CursorPosition,
) []protocol.CompletionItem { ) []protocol.CompletionItem {
value := entry.GetValueByCursor(entry.End.Character) value := entry.GetValueAtPosition(cursor)
var line string var line string
var relativeCursor uint32 var relativeCursor uint32
@ -86,8 +86,9 @@ func getMatchValueCompletions(
if value != nil { if value != nil {
line = value.Value.Raw line = value.Value.Raw
relativeCursor = common.DeprecatedImprovedCursorToIndex( relativeCursor = common.DeprecatedImprovedCursorToIndex(
value.Start.GetRelativeCursorPosition(cursor), cursor,
line, line,
value.Start.Character,
) )
} else { } else {
line = "" line = ""

View File

@ -17,12 +17,12 @@ func GetIncludeOptionLocation(
include.Values, include.Values,
index, index,
func(current *indexes.SSHDIndexIncludeValue, target common.IndexPosition) int { func(current *indexes.SSHDIndexIncludeValue, target common.IndexPosition) int {
if current.Start.IsAfterIndexPosition(target) { if current.IsPositionAfterEnd(target) {
return 1 return -1
} }
if current.End.IsBeforeIndexPosition(target) { if current.IsPositionBeforeStart(target) {
return -1 return 1
} }
return 0 return 0

View File

@ -29,7 +29,7 @@ func GetHoverInfoForOption(
} }
} }
if option.Key.ContainsIndexPosition(index) { if option.Key.ContainsPosition(index) {
if docValue != nil { if docValue != nil {
contents := []string{ contents := []string{
"## " + option.Key.Key, "## " + 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 line := option.OptionValue.Value.Raw
contents := docValue.FetchHoverInfo( contents := docValue.FetchHoverInfo(
line, line,

View File

@ -27,7 +27,7 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
if entry == nil || if entry == nil ||
entry.Separator == nil || entry.Separator == nil ||
entry.Key == nil || entry.Key == nil ||
entry.Key.Start.IsAfterCursorPosition(cursor) { entry.Key.IsPositionBeforeEnd(cursor) {
return handlers.GetRootCompletions( return handlers.GetRootCompletions(
d, 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( return handlers.GetOptionCompletions(
d, d,
entry, entry,