mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-19 07:25:27 +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(
|
func TestComplexExample(
|
||||||
t *testing.T,
|
t *testing.T,
|
||||||
) {
|
) {
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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 = ""
|
||||||
|
@ -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
|
||||||
|
@ -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,
|
||||||
|
@ -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,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user