mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
refactor(sshd_config): Migrating cursor to index position
This commit is contained in:
parent
41472c1a58
commit
3cff613620
@ -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,
|
||||
) {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 = ""
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user