Skip to content

Commit

Permalink
[FIX] IsInElseBranch is working now
Browse files Browse the repository at this point in the history
as long as the grammar is correct
  • Loading branch information
qvalentin committed Nov 1, 2023
1 parent d06dabd commit 4932bd3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
24 changes: 9 additions & 15 deletions internal/lsp/ast_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,29 @@ package lsp
import sitter "github.com/smacker/go-tree-sitter"

func IsInElseBranch(node *sitter.Node) bool {

parent := node.Parent()

if parent == nil {
return false
}

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 {
Expand Down
37 changes: 26 additions & 11 deletions internal/lsp/ast_diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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")
}

}

0 comments on commit 4932bd3

Please sign in to comment.