-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathmain.go
88 lines (72 loc) · 1.73 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
sitter "github.com/smacker/go-tree-sitter"
"github.com/smacker/go-tree-sitter/javascript"
)
func main() {
input := []byte("function hello() { console.log('hello') }; function goodbye(){}")
parser := sitter.NewParser()
parser.SetLanguage(javascript.GetLanguage())
tree := parser.Parse(nil, input)
n := tree.RootNode()
fmt.Println("AST:", n)
fmt.Println("Root type:", n.Type())
fmt.Println("Root children:", n.ChildCount())
fmt.Println("\nFunctions in input:")
q, _ := sitter.NewQuery([]byte("(function_declaration) @func"), javascript.GetLanguage())
qc := sitter.NewQueryCursor()
qc.Exec(q, n)
var funcs []*sitter.Node
for {
m, ok := qc.NextMatch()
if !ok {
break
}
for _, c := range m.Captures {
funcs = append(funcs, c.Node)
fmt.Println("-", funcName(input, c.Node))
}
}
fmt.Println("\nEdit input")
input = []byte("function hello() { console.log('hello') }; function goodbye(){ console.log('goodbye') }")
// reuse tree
tree.Edit(sitter.EditInput{
StartIndex: 62,
OldEndIndex: 63,
NewEndIndex: 87,
StartPoint: sitter.Point{
Row: 0,
Column: 62,
},
OldEndPoint: sitter.Point{
Row: 0,
Column: 63,
},
NewEndPoint: sitter.Point{
Row: 0,
Column: 87,
},
})
for _, f := range funcs {
var textChange string
if f.HasChanges() {
textChange = "has change"
} else {
textChange = "no changes"
}
fmt.Println("-", funcName(input, f), ">", textChange)
}
newTree := parser.Parse(tree, input)
n = newTree.RootNode()
fmt.Println("\nNew AST:", n)
}
func funcName(content []byte, n *sitter.Node) string {
if n == nil {
return ""
}
if n.Type() != "function_declaration" {
return ""
}
return n.ChildByFieldName("name").Content(content)
}