-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtree_cursor_test.go
69 lines (52 loc) · 1.19 KB
/
tree_cursor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package tree_sitter_test
import (
"fmt"
. "github.com/tree-sitter/go-tree-sitter"
tree_sitter_go "github.com/tree-sitter/tree-sitter-go/bindings/go"
)
func ExampleTreeCursor() {
parser := NewParser()
defer parser.Close()
language := NewLanguage(tree_sitter_go.Language())
parser.SetLanguage(language)
tree := parser.Parse(
[]byte(`
package main
func main() {
return
}
`),
nil,
)
defer tree.Close()
cursor := tree.Walk()
defer cursor.Close()
fmt.Println(cursor.Node().Kind())
fmt.Println(cursor.GotoFirstChild())
fmt.Println(cursor.Node().Kind())
fmt.Println(cursor.GotoFirstChild())
fmt.Println(cursor.Node().Kind())
// Returns `false` because the `package` node has no children
fmt.Println(cursor.GotoFirstChild())
fmt.Println(cursor.GotoNextSibling())
fmt.Println(cursor.Node().Kind())
fmt.Println(cursor.GotoParent())
fmt.Println(cursor.Node().Kind())
fmt.Println(cursor.GotoNextSibling())
fmt.Println(cursor.GotoNextSibling())
fmt.Println(cursor.Node().Kind())
// Output:
// source_file
// true
// package_clause
// true
// package
// false
// true
// package_identifier
// true
// package_clause
// true
// true
// function_declaration
}