-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
295 additions
and
91 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,87 @@ | ||
package lsp | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
sitter "github.com/smacker/go-tree-sitter" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetVariableDefinition(t *testing.T) { | ||
testCases := []struct { | ||
template string | ||
variableName string | ||
accessRange sitter.Range // the range must currently only contain the bytes | ||
expectedValue string | ||
expectedError error | ||
}{ | ||
{ | ||
template: `{{ $x := "hello" }} {{ $x }}`, | ||
variableName: "$x", | ||
accessRange: sitter.Range{ | ||
StartByte: 20, | ||
EndByte: 21, | ||
}, | ||
expectedValue: `"hello"`, | ||
expectedError: nil, | ||
}, | ||
{ | ||
template: ` | ||
{{ if true }} | ||
{{ $x := "hello" }} {{ $x }} | ||
{{ end }} | ||
{{ if false }} | ||
{{ $x := "goodby" }} {{ $x }} | ||
{{ end }} | ||
`, | ||
variableName: "$x", | ||
accessRange: sitter.Range{ | ||
StartByte: 46, | ||
EndByte: 47, | ||
}, | ||
expectedValue: `"hello"`, | ||
expectedError: nil, | ||
}, | ||
{ | ||
template: ` | ||
{{ if true }} | ||
{{ $x := "hello" }} {{ $x }} | ||
{{ end }} | ||
{{ if false }} | ||
{{ $x := "goodby" }} {{ $x }} | ||
{{ end }} | ||
`, | ||
variableName: "$x", | ||
accessRange: sitter.Range{ | ||
StartByte: 110, | ||
EndByte: 111, | ||
}, | ||
expectedValue: `"goodby"`, | ||
expectedError: nil, | ||
}, | ||
{ | ||
template: ` | ||
{{ if true }} | ||
{{ $x := "hello" }} {{ $x }} | ||
{{ end }} | ||
`, | ||
variableName: "$x", | ||
accessRange: sitter.Range{ | ||
StartByte: 67, | ||
EndByte: 68, | ||
}, | ||
expectedValue: ``, | ||
expectedError: fmt.Errorf("variable $x not found"), | ||
}, | ||
} | ||
for _, tC := range testCases { | ||
t.Run(tC.template, func(t *testing.T) { | ||
ast := ParseAst(nil, tC.template) | ||
symbolTable := NewSymbolTable(ast, []byte(tC.template)) | ||
result, err := symbolTable.getVariableDefinition(tC.variableName, tC.accessRange) | ||
assert.Equal(t, tC.expectedError, err) | ||
assert.Equal(t, tC.expectedValue, result.Value) | ||
}) | ||
} | ||
} |
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,47 @@ | ||
package lsp | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mrjosh/helm-ls/internal/util" | ||
sitter "github.com/smacker/go-tree-sitter" | ||
) | ||
|
||
type VariableType int64 | ||
|
||
const ( | ||
VariableTypeAssigment VariableType = iota | ||
VariableTypeRangeKeyOrIndex | ||
VariableTypeRangeValue | ||
) | ||
|
||
type VariableDefinition struct { | ||
Value string | ||
VariableType VariableType | ||
Scope sitter.Range | ||
Range sitter.Range | ||
} | ||
|
||
func (s *SymbolTable) AddVariableDefinition(symbol string, variableDefinition VariableDefinition) { | ||
s.variableDefinitions[symbol] = append(s.variableDefinitions[symbol], variableDefinition) | ||
} | ||
|
||
func (s *SymbolTable) getVariableDefinition(name string, accessRange sitter.Range) (VariableDefinition, error) { | ||
definitions, ok := s.variableDefinitions[name] | ||
if !ok || len(definitions) == 0 { | ||
return VariableDefinition{}, fmt.Errorf("variable %s not found", name) | ||
} | ||
for _, definition := range definitions { | ||
if util.RangeContainsRange(definition.Scope, accessRange) { | ||
return definition, nil | ||
} | ||
} | ||
return VariableDefinition{}, fmt.Errorf("variable %s not found", name) | ||
} | ||
|
||
func (s *SymbolTable) GetVariableDefinitionForNode(node *sitter.Node) (VariableDefinition, error) { | ||
if node == nil { | ||
return VariableDefinition{}, fmt.Errorf("Cannot get variable definition for node") | ||
} | ||
return VariableDefinition{}, fmt.Errorf("Cannot get variable definition for node") | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.