Skip to content

Commit

Permalink
feat: completion for unfinished_selector_expression
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed May 4, 2024
1 parent 848a395 commit 8a71fae
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 44 deletions.
4 changes: 4 additions & 0 deletions internal/handler/completion_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ func TestCompletionMain(t *testing.T) {
assert.Equal(t, tt.expectedError, err)
assert.NotNil(t, result)

if result == nil {
return
}

insertTexts := []string{}
for _, item := range result.Items {
insertTexts = append(insertTexts, item.InsertText)
Expand Down
42 changes: 42 additions & 0 deletions internal/lsp/symbol_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func TestSymbolTableForValues(t *testing.T) {
{{ .Test }}
{{ . }}
{{ if (and .Values. ) }}
{{ end }}
`

ast := ParseAst(nil, content)
Expand Down Expand Up @@ -210,3 +214,41 @@ func TestSymbolTableForValuesTestFile(t *testing.T) {
assert.Contains(t, points, v.startPoint)
}
}

func TestSymbolTableForValuesSingleTests(t *testing.T) {
type testCase struct {
template string
path []string
startPoint sitter.Point
}

testCases := []testCase{
{
template: `{{ if (and .Values. ) }} {{ end }} `,
path: []string{"Values"},
startPoint: sitter.Point{
Row: 0,
Column: 12,
},
},
{
template: `{{ if (and .Values. ) }} {{ end }} `,
path: []string{"Values", ""},
startPoint: sitter.Point{
Row: 0,
Column: 18,
},
},
}

for _, v := range testCases {
ast := ParseAst(nil, v.template)
symbolTable := NewSymbolTable(ast, []byte(v.template))
values := symbolTable.GetTemplateContextRanges(v.path)
points := []sitter.Point{}
for _, v := range values {
points = append(points, v.StartPoint)
}
assert.Contains(t, points, v.startPoint)
}
}
15 changes: 11 additions & 4 deletions internal/lsp/symbol_table_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (v *TemplateContextVisitor) RestoreStashedContext() {
}

func (v *TemplateContextVisitor) Enter(node *sitter.Node) {
switch node.Type() {
nodeType := node.Type()
switch nodeType {
case gotemplate.NodeTypeDot:
v.symbolTable.AddTemplateContext(v.currentContext, GetRangeForNode(node))
case gotemplate.NodeTypeFieldIdentifier:
Expand All @@ -57,6 +58,12 @@ func (v *TemplateContextVisitor) Enter(node *sitter.Node) {
case gotemplate.NodeTypeField:
content := node.ChildByFieldName("name").Content(v.content)
v.symbolTable.AddTemplateContext(append(v.currentContext, content), GetRangeForNode(node.ChildByFieldName("name")))
case gotemplate.NodeTypeUnfinishedSelectorExpression:
operandNode := node.ChildByFieldName("operand")
if operandNode.Type() == gotemplate.NodeTypeVariable && operandNode.Content(v.content) == "$" {
v.StashContext()
}
v.symbolTable.AddTemplateContext(append(getContextForSelectorExpression(operandNode, v.content), ""), GetRangeForNode(node.Child(int(node.ChildCount())-1)))
case gotemplate.NodeTypeSelectorExpression:
operandNode := node.ChildByFieldName("operand")
if operandNode.Type() == gotemplate.NodeTypeVariable && operandNode.Content(v.content) == "$" {
Expand All @@ -67,7 +74,7 @@ func (v *TemplateContextVisitor) Enter(node *sitter.Node) {

func (v *TemplateContextVisitor) Exit(node *sitter.Node) {
switch node.Type() {
case gotemplate.NodeTypeSelectorExpression:
case gotemplate.NodeTypeSelectorExpression, gotemplate.NodeTypeUnfinishedSelectorExpression:
operandNode := node.ChildByFieldName("operand")
if operandNode.Type() == gotemplate.NodeTypeVariable && operandNode.Content(v.content) == "$" {
v.RestoreStashedContext()
Expand All @@ -83,7 +90,7 @@ func (v *TemplateContextVisitor) EnterContextShift(node *sitter.Node, suffix str
case gotemplate.NodeTypeField:
content := node.ChildByFieldName("name").Content(v.content) + suffix
v.PushContext(content)
case gotemplate.NodeTypeSelectorExpression:
case gotemplate.NodeTypeSelectorExpression, gotemplate.NodeTypeUnfinishedSelectorExpression:
s := getContextForSelectorExpression(node, v.content)
if len(s) > 0 {
s[len(s)-1] = s[len(s)-1] + suffix
Expand All @@ -100,7 +107,7 @@ func (v *TemplateContextVisitor) ExitContextShift(node *sitter.Node) {
switch node.Type() {
case gotemplate.NodeTypeField, gotemplate.NodeTypeFieldIdentifier:
v.PopContext()
case gotemplate.NodeTypeSelectorExpression:
case gotemplate.NodeTypeSelectorExpression, gotemplate.NodeTypeUnfinishedSelectorExpression:
s := getContextForSelectorExpression(node, v.content)
if len(s) > 0 && s[0] == "$" {
v.RestoreStashedContext()
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (v *Visitors) visitNodesRecursiveWithScopeShift(node *sitter.Node) {
for _, visitor := range v.visitors {
visitor.ExitContextShift(rangeNode)
}
case gotemplate.NodeTypeSelectorExpression:
case gotemplate.NodeTypeSelectorExpression, gotemplate.NodeTypeUnfinishedSelectorExpression:
operand := node.ChildByFieldName("operand")
v.visitNodesRecursiveWithScopeShift(operand)
for _, visitor := range v.visitors {
Expand Down
73 changes: 37 additions & 36 deletions internal/tree-sitter/gotemplate/node-types.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
package gotemplate

const (
NodeTypeAssignment = "assignment"
NodeTypeArgumentList = "argument_list"
NodeTypeBlock = "block"
NodeTypeBlockAction = "block_action"
NodeTypeChainedPipeline = "chained_pipeline"
NodeTypeCloseBraces = "}}"
NodeTypeCloseBracesDash = "-}}"
NodeTypeComment = "comment"
NodeTypeDefine = "define"
NodeTypeDefineAction = "define_action"
NodeTypeDollar = "$"
NodeTypeDot = "dot"
NodeTypeDotSymbol = "."
NodeTypeElse = "else"
NodeTypeElseIf = "else if"
NodeTypeEnd = "end"
NodeTypeError = "ERROR"
NodeTypeField = "field"
NodeTypeFieldIdentifier = "field_identifier"
NodeTypeFunctionCall = "function_call"
NodeTypeIdentifier = "identifier"
NodeTypeIf = "if"
NodeTypeIfAction = "if_action"
NodeTypeInterpretedStringLiteral = "interpreted_string_literal"
NodeTypeOpenBraces = "{{"
NodeTypeOpenBracesDash = "{{-"
NodeTypeRange = "range"
NodeTypeRangeAction = "range_action"
NodeTypeRangeVariableDefinition = "range_variable_definition"
NodeTypeSelectorExpression = "selector_expression"
NodeTypeTemplate = "template"
NodeTypeText = "text"
NodeTypeVariable = "variable"
NodeTypeVariableDefinition = "variable_definition"
NodeTypeWith = "with"
NodeTypeWithAction = "with_action"
NodeTypeAssignment = "assignment"
NodeTypeArgumentList = "argument_list"
NodeTypeBlock = "block"
NodeTypeBlockAction = "block_action"
NodeTypeChainedPipeline = "chained_pipeline"
NodeTypeCloseBraces = "}}"
NodeTypeCloseBracesDash = "-}}"
NodeTypeComment = "comment"
NodeTypeDefine = "define"
NodeTypeDefineAction = "define_action"
NodeTypeDollar = "$"
NodeTypeDot = "dot"
NodeTypeDotSymbol = "."
NodeTypeElse = "else"
NodeTypeElseIf = "else if"
NodeTypeEnd = "end"
NodeTypeError = "ERROR"
NodeTypeField = "field"
NodeTypeFieldIdentifier = "field_identifier"
NodeTypeFunctionCall = "function_call"
NodeTypeIdentifier = "identifier"
NodeTypeIf = "if"
NodeTypeIfAction = "if_action"
NodeTypeInterpretedStringLiteral = "interpreted_string_literal"
NodeTypeOpenBraces = "{{"
NodeTypeOpenBracesDash = "{{-"
NodeTypeRange = "range"
NodeTypeRangeAction = "range_action"
NodeTypeRangeVariableDefinition = "range_variable_definition"
NodeTypeSelectorExpression = "selector_expression"
NodeTypeUnfinishedSelectorExpression = "unfished_selector_expression" // TODO: fix this typo in the grammar
NodeTypeTemplate = "template"
NodeTypeText = "text"
NodeTypeVariable = "variable"
NodeTypeVariableDefinition = "variable_definition"
NodeTypeWith = "with"
NodeTypeWithAction = "with_action"

FieldNameAlternative = "alternative"
FieldNameCondition = "condition"
Expand Down
5 changes: 2 additions & 3 deletions testdata/example/templates/completion-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
{{ .Chart.N }}
{{ . }}

{{ toYaml .Release. }}
{{ toYaml (.Release. ) }}
{{ .Release. }}
{{ if (and .Values. ) }}

{{ end }}

0 comments on commit 8a71fae

Please sign in to comment.