-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(variables): add variables resolving to templatecontext
- Loading branch information
Showing
18 changed files
with
240 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package handler | ||
|
||
import ( | ||
"strings" | ||
|
||
"go.lsp.dev/protocol" | ||
) | ||
|
||
// Takes a string with a mark (^) in it and returns the position and the string without the mark | ||
func getPositionForMarkedTestLine(buf string) (protocol.Position, string) { | ||
col := strings.Index(buf, "^") | ||
buf = strings.Replace(buf, "^", "", 1) | ||
pos := protocol.Position{Line: 0, Character: uint32(col)} | ||
return pos, buf | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package lsp | ||
|
||
import ( | ||
"fmt" | ||
|
||
sitter "github.com/smacker/go-tree-sitter" | ||
) | ||
|
||
func (s *SymbolTable) ResolveVariablesInTemplateContext(templateContext TemplateContext, pointRange sitter.Range) (TemplateContext, error) { | ||
if !templateContext.IsVariable() { | ||
return templateContext, nil | ||
} | ||
|
||
variableName := templateContext[0] | ||
if variableName == "$" { | ||
return templateContext.Tail(), nil | ||
} | ||
|
||
variableDefinitions := s.variableDefinitions[variableName] | ||
|
||
if len(variableDefinitions) == 0 { | ||
return templateContext, fmt.Errorf("variable %s not found", variableName) | ||
} | ||
|
||
definition, err := findDefinitionForRange(variableDefinitions, pointRange) | ||
if err != nil { | ||
return templateContext, fmt.Errorf("variable %s not found %e", variableName, err) | ||
} | ||
|
||
prefix := getPrefixTemplateContextForVariable(definition) | ||
|
||
return s.ResolveVariablesInTemplateContext(append(prefix, templateContext.Tail()...), pointRange) | ||
} | ||
|
||
func getPrefixTemplateContextForVariable(definition VariableDefinition) TemplateContext { | ||
prefix := NewTemplateContext(definition.Value) | ||
if definition.VariableType == VariableTypeRangeValue && len(prefix) > 0 { | ||
prefix[len(prefix)-1] = prefix[len(prefix)-1] + "[]" | ||
} | ||
return prefix | ||
} |
42 changes: 42 additions & 0 deletions
42
internal/lsp/symbol_table_template_context_variables_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package lsp | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
sitter "github.com/smacker/go-tree-sitter" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestResolveVariablesInTemplateContext(t *testing.T) { | ||
tests := []struct { | ||
template string | ||
templateCtx TemplateContext | ||
expectedCtx TemplateContext | ||
expectedErr error | ||
}{ | ||
{"{{ $values := .Values }} {{ $values.te^st }}", TemplateContext{"$values", "test"}, TemplateContext{"Values", "test"}, nil}, | ||
{"{{- range $type, $config := .Values.deployments }} {{ $config.te^st }}", TemplateContext{"$config", "test"}, TemplateContext{"Values", "deployments[]", "test"}, nil}, | ||
{" {{ $values := .Values }} {{- range $type, $config := $values.deployments }} {{ $config.te^st }}", TemplateContext{"$config", "test"}, TemplateContext{"Values", "deployments[]", "test"}, nil}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.template, func(t *testing.T) { | ||
col := strings.Index(tt.template, "^") | ||
buf := strings.Replace(tt.template, "^", "", 1) | ||
ast := ParseAst(nil, tt.template) | ||
symbolTable := NewSymbolTable(ast, []byte(buf)) | ||
|
||
result, err := symbolTable.ResolveVariablesInTemplateContext( | ||
tt.templateCtx, sitter.Range{StartByte: uint32(col), EndByte: uint32(col + 1)}) | ||
|
||
if tt.expectedErr != nil { | ||
assert.Error(t, err) | ||
assert.Equal(t, tt.expectedErr, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expectedCtx, result) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.