Skip to content

Commit 406e3d5

Browse files
committed
official tree-sitter
1 parent 611faa5 commit 406e3d5

File tree

6 files changed

+67
-42
lines changed

6 files changed

+67
-42
lines changed

gazelle/MODULE.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ use_repo(
2323
"com_github_bmatcuk_doublestar_v4",
2424
"com_github_emirpasic_gods",
2525
"com_github_ghodss_yaml",
26-
"com_github_smacker_go_tree_sitter",
2726
"com_github_stretchr_testify",
27+
"com_github_tree_sitter_go_tree_sitter",
28+
"com_github_tree_sitter_tree_sitter_python",
2829
"in_gopkg_yaml_v2",
2930
"org_golang_x_sync",
3031
)

gazelle/go.mod

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module github.com/bazel-contrib/rules_python/gazelle
22

3-
go 1.22.9
3+
go 1.23
44

5-
toolchain go1.24.2
5+
toolchain go1.24.4
66

77
require (
88
github.com/bazelbuild/bazel-gazelle v0.36.0
@@ -11,15 +11,17 @@ require (
1111
github.com/bmatcuk/doublestar/v4 v4.7.1
1212
github.com/emirpasic/gods v1.18.1
1313
github.com/ghodss/yaml v1.0.0
14-
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82
15-
github.com/stretchr/testify v1.9.0
14+
github.com/stretchr/testify v1.10.0
15+
github.com/tree-sitter/go-tree-sitter v0.25.0
16+
github.com/tree-sitter/tree-sitter-python v0.23.6
1617
golang.org/x/sync v0.11.0
1718
gopkg.in/yaml.v2 v2.4.0
1819
)
1920

2021
require (
2122
github.com/davecgh/go-spew v1.1.1 // indirect
2223
github.com/google/go-cmp v0.6.0 // indirect
24+
github.com/mattn/go-pointer v0.0.1 // indirect
2325
github.com/pmezard/go-difflib v1.0.0 // indirect
2426
golang.org/x/mod v0.23.0 // indirect
2527
golang.org/x/sys v0.30.0 // indirect

gazelle/go.sum

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,38 @@ github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
1414
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
1515
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1616
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
17+
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
18+
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
1719
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1820
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
19-
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82 h1:6C8qej6f1bStuePVkLSFxoU22XBS165D3klxlzRg8F4=
20-
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82/go.mod h1:xe4pgH49k4SsmkQq5OT8abwhWmnzkhpgnXeekbx2efw=
21-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
22-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
21+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
22+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
23+
github.com/tree-sitter/go-tree-sitter v0.25.0 h1:sx6kcg8raRFCvc9BnXglke6axya12krCJF5xJ2sftRU=
24+
github.com/tree-sitter/go-tree-sitter v0.25.0/go.mod h1:r77ig7BikoZhHrrsjAnv8RqGti5rtSyvDHPzgTPsUuU=
25+
github.com/tree-sitter/tree-sitter-c v0.23.4 h1:nBPH3FV07DzAD7p0GfNvXM+Y7pNIoPenQWBpvM++t4c=
26+
github.com/tree-sitter/tree-sitter-c v0.23.4/go.mod h1:MkI5dOiIpeN94LNjeCp8ljXN/953JCwAby4bClMr6bw=
27+
github.com/tree-sitter/tree-sitter-cpp v0.23.4 h1:LaWZsiqQKvR65yHgKmnaqA+uz6tlDJTJFCyFIeZU/8w=
28+
github.com/tree-sitter/tree-sitter-cpp v0.23.4/go.mod h1:doqNW64BriC7WBCQ1klf0KmJpdEvfxyXtoEybnBo6v8=
29+
github.com/tree-sitter/tree-sitter-embedded-template v0.23.2 h1:nFkkH6Sbe56EXLmZBqHHcamTpmz3TId97I16EnGy4rg=
30+
github.com/tree-sitter/tree-sitter-embedded-template v0.23.2/go.mod h1:HNPOhN0qF3hWluYLdxWs5WbzP/iE4aaRVPMsdxuzIaQ=
31+
github.com/tree-sitter/tree-sitter-go v0.23.4 h1:yt5KMGnTHS+86pJmLIAZMWxukr8W7Ae1STPvQUuNROA=
32+
github.com/tree-sitter/tree-sitter-go v0.23.4/go.mod h1:Jrx8QqYN0v7npv1fJRH1AznddllYiCMUChtVjxPK040=
33+
github.com/tree-sitter/tree-sitter-html v0.23.2 h1:1UYDV+Yd05GGRhVnTcbP58GkKLSHHZwVaN+lBZV11Lc=
34+
github.com/tree-sitter/tree-sitter-html v0.23.2/go.mod h1:gpUv/dG3Xl/eebqgeYeFMt+JLOY9cgFinb/Nw08a9og=
35+
github.com/tree-sitter/tree-sitter-java v0.23.5 h1:J9YeMGMwXYlKSP3K4Us8CitC6hjtMjqpeOf2GGo6tig=
36+
github.com/tree-sitter/tree-sitter-java v0.23.5/go.mod h1:NRKlI8+EznxA7t1Yt3xtraPk1Wzqh3GAIC46wxvc320=
37+
github.com/tree-sitter/tree-sitter-javascript v0.23.1 h1:1fWupaRC0ArlHJ/QJzsfQ3Ibyopw7ZfQK4xXc40Zveo=
38+
github.com/tree-sitter/tree-sitter-javascript v0.23.1/go.mod h1:lmGD1EJdCA+v0S1u2fFgepMg/opzSg/4pgFym2FPGAs=
39+
github.com/tree-sitter/tree-sitter-json v0.24.8 h1:tV5rMkihgtiOe14a9LHfDY5kzTl5GNUYe6carZBn0fQ=
40+
github.com/tree-sitter/tree-sitter-json v0.24.8/go.mod h1:F351KK0KGvCaYbZ5zxwx/gWWvZhIDl0eMtn+1r+gQbo=
41+
github.com/tree-sitter/tree-sitter-php v0.23.11 h1:iHewsLNDmznh8kgGyfWfujsZxIz1YGbSd2ZTEM0ZiP8=
42+
github.com/tree-sitter/tree-sitter-php v0.23.11/go.mod h1:T/kbfi+UcCywQfUNAJnGTN/fMSUjnwPXA8k4yoIks74=
43+
github.com/tree-sitter/tree-sitter-python v0.23.6 h1:qHnWFR5WhtMQpxBZRwiaU5Hk/29vGju6CVtmvu5Haas=
44+
github.com/tree-sitter/tree-sitter-python v0.23.6/go.mod h1:cpdthSy/Yoa28aJFBscFHlGiU+cnSiSh1kuDVtI8YeM=
45+
github.com/tree-sitter/tree-sitter-ruby v0.23.1 h1:T/NKHUA+iVbHM440hFx+lzVOzS4dV6z8Qw8ai+72bYo=
46+
github.com/tree-sitter/tree-sitter-ruby v0.23.1/go.mod h1:kUS4kCCQloFcdX6sdpr8p6r2rogbM6ZjTox5ZOQy8cA=
47+
github.com/tree-sitter/tree-sitter-rust v0.23.2 h1:6AtoooCW5GqNrRpfnvl0iUhxTAZEovEmLKDbyHlfw90=
48+
github.com/tree-sitter/tree-sitter-rust v0.23.2/go.mod h1:hfeGWic9BAfgTrc7Xf6FaOAguCFJRo3RBbs7QJ6D7MI=
2349
golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM=
2450
golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
2551
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=

gazelle/python/BUILD.bazel

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,20 @@ go_library(
2929
importpath = "github.com/bazel-contrib/rules_python/gazelle/python",
3030
visibility = ["//visibility:public"],
3131
deps = [
32-
"//manifest",
3332
"//pythonconfig",
3433
"@bazel_gazelle//config:go_default_library",
3534
"@bazel_gazelle//label:go_default_library",
3635
"@bazel_gazelle//language:go_default_library",
3736
"@bazel_gazelle//repo:go_default_library",
3837
"@bazel_gazelle//resolve:go_default_library",
3938
"@bazel_gazelle//rule:go_default_library",
40-
"@com_github_bazelbuild_buildtools//build:go_default_library",
39+
"@com_github_bazelbuild_buildtools//build",
4140
"@com_github_bmatcuk_doublestar_v4//:doublestar",
4241
"@com_github_emirpasic_gods//lists/singlylinkedlist",
4342
"@com_github_emirpasic_gods//sets/treeset",
4443
"@com_github_emirpasic_gods//utils",
45-
"@com_github_smacker_go_tree_sitter//:go-tree-sitter",
46-
"@com_github_smacker_go_tree_sitter//python",
44+
"@com_github_tree_sitter_go_tree_sitter//:go-tree-sitter",
45+
"@com_github_tree_sitter_tree_sitter_python//bindings/go",
4746
"@org_golang_x_sync//errgroup",
4847
],
4948
)

gazelle/python/file_parser.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"path/filepath"
2323
"strings"
2424

25-
sitter "github.com/smacker/go-tree-sitter"
26-
"github.com/smacker/go-tree-sitter/python"
25+
sitter "github.com/tree-sitter/go-tree-sitter"
26+
python "github.com/tree-sitter/tree-sitter-python/bindings/go"
2727
)
2828

2929
const (
@@ -61,12 +61,9 @@ func NewFileParser() *FileParser {
6161
// It prints a warning if parsing fails.
6262
func ParseCode(code []byte, path string) (*sitter.Node, error) {
6363
parser := sitter.NewParser()
64-
parser.SetLanguage(python.GetLanguage())
64+
parser.SetLanguage(sitter.NewLanguage(python.Language()))
6565

66-
tree, err := parser.ParseCtx(context.Background(), nil, code)
67-
if err != nil {
68-
return nil, err
69-
}
66+
tree := parser.Parse(code, nil)
7067

7168
root := tree.RootNode()
7269
if !root.HasError() {
@@ -82,16 +79,16 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
8279
return root, nil
8380
}
8481

85-
for i := 0; i < int(root.ChildCount()); i++ {
82+
for i := uint(0); i < root.ChildCount(); i++ {
8683
child := root.Child(i)
8784
if child.IsError() {
8885
// Example logs:
8986
// gazelle: Parse error at {Row:1 Column:0}:
9087
// def search_one_more_level[T]():
91-
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code))
88+
log.Printf("Parse error at %+v:\n%+v", child.StartPosition(), child.Utf8Text(code))
9289
// Log the internal tree-sitter representation of what was parsed. Eg:
9390
// gazelle: The above was parsed as: (ERROR (identifier) (call function: (list (identifier)) arguments: (argument_list)))
94-
log.Printf("The above was parsed as: %v", child.String())
91+
log.Printf("The above was parsed as: %v", child.Kind())
9592
}
9693
}
9794

@@ -101,21 +98,21 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
10198
// parseMain returns true if the python file has an `if __name__ == "__main__":` block,
10299
// which is a common idiom for python scripts/binaries.
103100
func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
104-
for i := 0; i < int(node.ChildCount()); i++ {
101+
for i := uint(0); i < node.ChildCount(); i++ {
105102
if err := ctx.Err(); err != nil {
106103
return false
107104
}
108105
child := node.Child(i)
109-
if child.Type() == sitterNodeTypeIfStatement &&
110-
child.Child(1).Type() == sitterNodeTypeComparisonOperator && child.Child(1).Child(1).Type() == "==" {
106+
if child.Kind() == sitterNodeTypeIfStatement &&
107+
child.Child(1).Kind() == sitterNodeTypeComparisonOperator && child.Child(1).Child(1).Kind() == "==" {
111108
statement := child.Child(1)
112109
a, b := statement.Child(0), statement.Child(2)
113110
// convert "'__main__' == __name__" to "__name__ == '__main__'"
114-
if b.Type() == sitterNodeTypeIdentifier {
111+
if b.Kind() == sitterNodeTypeIdentifier {
115112
a, b = b, a
116113
}
117-
if a.Type() == sitterNodeTypeIdentifier && a.Content(p.code) == "__name__" &&
118-
b.Type() == sitterNodeTypeString && string(p.code[b.StartByte()+1:b.EndByte()-1]) == "__main__" {
114+
if a.Kind() == sitterNodeTypeIdentifier && a.Utf8Text(p.code) == "__name__" &&
115+
b.Kind() == sitterNodeTypeString && string(p.code[b.StartByte()+1:b.EndByte()-1]) == "__main__" {
119116
return true
120117
}
121118
}
@@ -126,18 +123,18 @@ func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
126123
// parseImportStatement parses a node for an import statement, returning a `module` and a boolean
127124
// representing if the parse was OK or not.
128125
func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
129-
switch node.Type() {
126+
switch node.Kind() {
130127
case sitterNodeTypeDottedName:
131128
return module{
132-
Name: node.Content(code),
133-
LineNumber: node.StartPoint().Row + 1,
129+
Name: node.Utf8Text(code),
130+
LineNumber: node.StartPosition().Row + 1,
134131
}, true
135132
case sitterNodeTypeAliasedImport:
136133
return parseImportStatement(node.Child(0), code)
137134
case sitterNodeTypeWildcardImport:
138135
return module{
139136
Name: "*",
140-
LineNumber: node.StartPoint().Row + 1,
137+
LineNumber: node.StartPosition().Row + 1,
141138
}, true
142139
}
143140
return module{}, false
@@ -147,8 +144,8 @@ func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
147144
// an import statement. It updates FileParser.output.Modules with the `module` that the
148145
// import represents.
149146
func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
150-
if node.Type() == sitterNodeTypeImportStatement {
151-
for j := 1; j < int(node.ChildCount()); j++ {
147+
if node.Kind() == sitterNodeTypeImportStatement {
148+
for j := uint(1); j < node.ChildCount(); j++ {
152149
m, ok := parseImportStatement(node.Child(j), p.code)
153150
if !ok {
154151
continue
@@ -159,12 +156,12 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
159156
}
160157
p.output.Modules = append(p.output.Modules, m)
161158
}
162-
} else if node.Type() == sitterNodeTypeImportFromStatement {
163-
from := node.Child(1).Content(p.code)
159+
} else if node.Kind() == sitterNodeTypeImportFromStatement {
160+
from := node.Child(1).Utf8Text(p.code)
164161
if strings.HasPrefix(from, ".") {
165162
return true
166163
}
167-
for j := 3; j < int(node.ChildCount()); j++ {
164+
for j := uint(3); j < node.ChildCount(); j++ {
168165
m, ok := parseImportStatement(node.Child(j), p.code)
169166
if !ok {
170167
continue
@@ -183,8 +180,8 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
183180
// parseComments parses a node for comments, returning true if the node is a comment.
184181
// It updates FileParser.output.Comments with the parsed comment.
185182
func (p *FileParser) parseComments(node *sitter.Node) bool {
186-
if node.Type() == sitterNodeTypeComment {
187-
p.output.Comments = append(p.output.Comments, comment(node.Content(p.code)))
183+
if node.Kind() == sitterNodeTypeComment {
184+
p.output.Comments = append(p.output.Comments, comment(node.Utf8Text(p.code)))
188185
return true
189186
}
190187
return false
@@ -200,7 +197,7 @@ func (p *FileParser) parse(ctx context.Context, node *sitter.Node) {
200197
if node == nil {
201198
return
202199
}
203-
for i := 0; i < int(node.ChildCount()); i++ {
200+
for i := uint(0); i < node.ChildCount(); i++ {
204201
if err := ctx.Err(); err != nil {
205202
return
206203
}

gazelle/python/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ type module struct {
152152
// statements.
153153
Name string `json:"name"`
154154
// The line number where the import happened.
155-
LineNumber uint32 `json:"lineno"`
155+
LineNumber uint `json:"lineno"`
156156
// The path to the module file relative to the Bazel workspace root.
157157
Filepath string `json:"filepath"`
158158
// If this was a from import, e.g. from foo import bar, From indicates the module

0 commit comments

Comments
 (0)