diff --git a/internal/lsp/ast_diagnostics.go b/internal/lsp/ast_diagnostics.go index 66b8ef79..de9b96b1 100644 --- a/internal/lsp/ast_diagnostics.go +++ b/internal/lsp/ast_diagnostics.go @@ -3,7 +3,6 @@ package lsp import sitter "github.com/smacker/go-tree-sitter" func IsInElseBranch(node *sitter.Node) bool { - parent := node.Parent() if parent == nil { @@ -11,27 +10,22 @@ func IsInElseBranch(node *sitter.Node) bool { } if parent.Type() == "if_action" { - - childIndex, err := getIndexOfChild(parent, node) - if err != nil { - return IsInElseBranch(parent) - } - - switch parent.FieldNameForChild(childIndex) { - case "option", "alertnative": - return true - default: - return false - + curser := sitter.NewTreeCursor(parent) + curser.GoToFirstChild() + for curser.GoToNextSibling() { + fieldName := curser.CurrentFieldName() + if fieldName == "alternative" || fieldName == "option" { + if curser.CurrentNode().Equal(node) { + return true + } + } } } - return IsInElseBranch(parent) } func getIndexOfChild(parent *sitter.Node, child *sitter.Node) (int, error) { - count := parent.ChildCount() for i := 0; i < int(count); i++ { if parent.Child(i) == child { diff --git a/internal/lsp/ast_diagnostics_test.go b/internal/lsp/ast_diagnostics_test.go index 9f16e9d7..3b9e81be 100644 --- a/internal/lsp/ast_diagnostics_test.go +++ b/internal/lsp/ast_diagnostics_test.go @@ -7,24 +7,36 @@ import ( ) func TestIsInElseBranch(t *testing.T) { - template := `{{if pipeline}} t1 {{ else if pipeline }} t2 {{ else }} t3 {{ end }}` + template := `{{if pipeline}} t1 {{ else if pipeline }} t2 {{ else if pipeline2 }} t3 {{ else }} t4 {{ end }}` var ast = ParseAst(template) - // (template - // (if_action - // (function_call (identifier)) - // (text) - // (function_call (identifier)) - // (text) - // (text))) + // (template [0, 0] - [1, 0] + // (if_action [0, 0] - [0, 95] + // condition: (function_call [0, 5] - [0, 13] + // function: (identifier [0, 5] - [0, 13])) + // consequence: (text [0, 15] - [0, 18]) + // condition: (function_call [0, 30] - [0, 38] + // function: (identifier [0, 30] - [0, 38])) + // option: (text [0, 41] - [0, 44]) + // condition: (function_call [0, 56] - [0, 65] + // function: (identifier [0, 56] - [0, 65])) + // option: (text [0, 68] - [0, 71]) + // alternative: (text [0, 82] - [0, 85]))) + + logger.Println("RootNode:", ast.RootNode().String()) t1_start := sitter.Point{Row: 0, Column: 16} t1 := ast.RootNode().NamedDescendantForPointRange(t1_start, t1_start) t2_start := sitter.Point{Row: 0, Column: 42} t2 := ast.RootNode().NamedDescendantForPointRange(t2_start, t2_start) - t3_start := sitter.Point{Row: 0, Column: 56} + t3_start := sitter.Point{Row: 0, Column: 69} t3 := ast.RootNode().NamedDescendantForPointRange(t3_start, t3_start) - - if (t1.Content([]byte(template))) != " t1 " || (t2.Content([]byte(template))) != " t2 " || (t3.Content([]byte(template))) != " t3 " { + t4_start := sitter.Point{Row: 0, Column: 83} + t4 := ast.RootNode().NamedDescendantForPointRange(t4_start, t4_start) + t1Content := t1.Content([]byte(template)) + t2Content := t2.Content([]byte(template)) + t3Content := t3.Content([]byte(template)) + t4Content := t4.Content([]byte(template)) + if (t1Content != " t1") || t2Content != " t2" || t3Content != " t3" || t4Content != " t4" { t.Errorf("Nodes were not correctly selected") } @@ -37,5 +49,8 @@ func TestIsInElseBranch(t *testing.T) { if !IsInElseBranch(t3) { t.Errorf("t3 was incorrectly identified as not in else branch") } + if !IsInElseBranch(t4) { + t.Errorf("t4 was incorrectly identified as not in else branch") + } }