Skip to content

Commit

Permalink
fix: don't modify slices
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed May 22, 2024
1 parent d6e8731 commit 096c7b3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
7 changes: 6 additions & 1 deletion internal/lsp/symbol_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func (t TemplateContext) Format() string {
return strings.Join(t, ".")
}

func (t TemplateContext) Copy() TemplateContext {
return append(TemplateContext{}, t...)
}

func (t TemplateContext) Tail() TemplateContext {
return t[1:]
}
Expand Down Expand Up @@ -65,7 +69,8 @@ func (s *SymbolTable) GetTemplateContext(pointRange sitter.Range) (TemplateConte
if !ok {
return result, fmt.Errorf("no template context found")
}
return result, nil
// return a copy to never modify the original
return result.Copy(), nil
}

func (s *SymbolTable) AddIncludeDefinition(symbol string, pointRange sitter.Range) {
Expand Down
12 changes: 5 additions & 7 deletions internal/util/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func GetPositionOfNode(node *yamlv3.Node, query []string) (lsp.Position, error)
isRange := false

if strings.HasSuffix(query[0], "[]") {
queryCopy := append([]string{}, query...)
copy(queryCopy, query)
query = queryCopy
query[0] = strings.TrimSuffix(query[0], "[]")
isRange = true
}
Expand All @@ -48,7 +51,7 @@ func GetPositionOfNode(node *yamlv3.Node, query []string) (lsp.Position, error)
return GetPositionOfNode(nestedNode, query[1:])
}
if len(node.Content) < index+1 {
return lsp.Position{}, fmt.Errorf("could not find Position of %s in values.", query)
return lsp.Position{}, fmt.Errorf("could not find Position of %s in values", query)
}
if isRange {
return getPositionOfNodeAfterMapping(node.Content[index+1], query[1:])
Expand All @@ -60,12 +63,7 @@ func GetPositionOfNode(node *yamlv3.Node, query []string) (lsp.Position, error)
}

func getPositionOfNodeAfterMapping(node *yamlv3.Node, query []string) (lsp.Position, error) {
kind := node.Kind

println(fmt.Sprintf("Kind is %d, len %d", kind, len(node.Content)))
println(fmt.Sprintf("ChildKind is %d, len %d", node.Content[0].Kind, len(node.Content[0].Content)))

switch kind {
switch node.Kind {
case yamlv3.SequenceNode:
if len(node.Content) > 0 {
return GetPositionOfNode(node.Content[0], query)
Expand Down
7 changes: 5 additions & 2 deletions internal/util/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestGetPositionOfNodeInEmptyDocument(t *testing.T) {
func TestGetPositionOfNodeTable(t *testing.T) {
tests := []struct {
name string
keys []string
query []string
expected lsp.Position
expectErr bool
}{
Expand All @@ -68,14 +68,17 @@ func TestGetPositionOfNodeTable(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := GetPositionOfNode(&node, tt.keys)
queryCopy := append([]string{}, tt.query...)

result, err := GetPositionOfNode(&node, tt.query)

if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
assert.Equal(t, queryCopy, tt.query)
})
}
}

0 comments on commit 096c7b3

Please sign in to comment.