Skip to content

Commit b7e3913

Browse files
committed
Revert "official tree-sitter"
This reverts commit 406e3d5.
1 parent 406e3d5 commit b7e3913

File tree

6 files changed

+42
-67
lines changed

6 files changed

+42
-67
lines changed

gazelle/MODULE.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ 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",
2627
"com_github_stretchr_testify",
27-
"com_github_tree_sitter_go_tree_sitter",
28-
"com_github_tree_sitter_tree_sitter_python",
2928
"in_gopkg_yaml_v2",
3029
"org_golang_x_sync",
3130
)

gazelle/go.mod

Lines changed: 4 additions & 6 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.23
3+
go 1.22.9
44

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

77
require (
88
github.com/bazelbuild/bazel-gazelle v0.36.0
@@ -11,17 +11,15 @@ 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/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
14+
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82
15+
github.com/stretchr/testify v1.9.0
1716
golang.org/x/sync v0.11.0
1817
gopkg.in/yaml.v2 v2.4.0
1918
)
2019

2120
require (
2221
github.com/davecgh/go-spew v1.1.1 // indirect
2322
github.com/google/go-cmp v0.6.0 // indirect
24-
github.com/mattn/go-pointer v0.0.1 // indirect
2523
github.com/pmezard/go-difflib v1.0.0 // indirect
2624
golang.org/x/mod v0.23.0 // indirect
2725
golang.org/x/sys v0.30.0 // indirect

gazelle/go.sum

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,12 @@ 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=
1917
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2018
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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=
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=
4923
golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM=
5024
golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
5125
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=

gazelle/python/BUILD.bazel

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@ go_library(
2929
importpath = "github.com/bazel-contrib/rules_python/gazelle/python",
3030
visibility = ["//visibility:public"],
3131
deps = [
32+
"//manifest",
3233
"//pythonconfig",
3334
"@bazel_gazelle//config:go_default_library",
3435
"@bazel_gazelle//label:go_default_library",
3536
"@bazel_gazelle//language:go_default_library",
3637
"@bazel_gazelle//repo:go_default_library",
3738
"@bazel_gazelle//resolve:go_default_library",
3839
"@bazel_gazelle//rule:go_default_library",
39-
"@com_github_bazelbuild_buildtools//build",
40+
"@com_github_bazelbuild_buildtools//build:go_default_library",
4041
"@com_github_bmatcuk_doublestar_v4//:doublestar",
4142
"@com_github_emirpasic_gods//lists/singlylinkedlist",
4243
"@com_github_emirpasic_gods//sets/treeset",
4344
"@com_github_emirpasic_gods//utils",
44-
"@com_github_tree_sitter_go_tree_sitter//:go-tree-sitter",
45-
"@com_github_tree_sitter_tree_sitter_python//bindings/go",
45+
"@com_github_smacker_go_tree_sitter//:go-tree-sitter",
46+
"@com_github_smacker_go_tree_sitter//python",
4647
"@org_golang_x_sync//errgroup",
4748
],
4849
)

gazelle/python/file_parser.go

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

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

2929
const (
@@ -61,9 +61,12 @@ 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(sitter.NewLanguage(python.Language()))
64+
parser.SetLanguage(python.GetLanguage())
6565

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

6871
root := tree.RootNode()
6972
if !root.HasError() {
@@ -79,16 +82,16 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
7982
return root, nil
8083
}
8184

82-
for i := uint(0); i < root.ChildCount(); i++ {
85+
for i := 0; i < int(root.ChildCount()); i++ {
8386
child := root.Child(i)
8487
if child.IsError() {
8588
// Example logs:
8689
// gazelle: Parse error at {Row:1 Column:0}:
8790
// def search_one_more_level[T]():
88-
log.Printf("Parse error at %+v:\n%+v", child.StartPosition(), child.Utf8Text(code))
91+
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code))
8992
// Log the internal tree-sitter representation of what was parsed. Eg:
9093
// gazelle: The above was parsed as: (ERROR (identifier) (call function: (list (identifier)) arguments: (argument_list)))
91-
log.Printf("The above was parsed as: %v", child.Kind())
94+
log.Printf("The above was parsed as: %v", child.String())
9295
}
9396
}
9497

@@ -98,21 +101,21 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
98101
// parseMain returns true if the python file has an `if __name__ == "__main__":` block,
99102
// which is a common idiom for python scripts/binaries.
100103
func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
101-
for i := uint(0); i < node.ChildCount(); i++ {
104+
for i := 0; i < int(node.ChildCount()); i++ {
102105
if err := ctx.Err(); err != nil {
103106
return false
104107
}
105108
child := node.Child(i)
106-
if child.Kind() == sitterNodeTypeIfStatement &&
107-
child.Child(1).Kind() == sitterNodeTypeComparisonOperator && child.Child(1).Child(1).Kind() == "==" {
109+
if child.Type() == sitterNodeTypeIfStatement &&
110+
child.Child(1).Type() == sitterNodeTypeComparisonOperator && child.Child(1).Child(1).Type() == "==" {
108111
statement := child.Child(1)
109112
a, b := statement.Child(0), statement.Child(2)
110113
// convert "'__main__' == __name__" to "__name__ == '__main__'"
111-
if b.Kind() == sitterNodeTypeIdentifier {
114+
if b.Type() == sitterNodeTypeIdentifier {
112115
a, b = b, a
113116
}
114-
if a.Kind() == sitterNodeTypeIdentifier && a.Utf8Text(p.code) == "__name__" &&
115-
b.Kind() == sitterNodeTypeString && string(p.code[b.StartByte()+1:b.EndByte()-1]) == "__main__" {
117+
if a.Type() == sitterNodeTypeIdentifier && a.Content(p.code) == "__name__" &&
118+
b.Type() == sitterNodeTypeString && string(p.code[b.StartByte()+1:b.EndByte()-1]) == "__main__" {
116119
return true
117120
}
118121
}
@@ -123,18 +126,18 @@ func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
123126
// parseImportStatement parses a node for an import statement, returning a `module` and a boolean
124127
// representing if the parse was OK or not.
125128
func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
126-
switch node.Kind() {
129+
switch node.Type() {
127130
case sitterNodeTypeDottedName:
128131
return module{
129-
Name: node.Utf8Text(code),
130-
LineNumber: node.StartPosition().Row + 1,
132+
Name: node.Content(code),
133+
LineNumber: node.StartPoint().Row + 1,
131134
}, true
132135
case sitterNodeTypeAliasedImport:
133136
return parseImportStatement(node.Child(0), code)
134137
case sitterNodeTypeWildcardImport:
135138
return module{
136139
Name: "*",
137-
LineNumber: node.StartPosition().Row + 1,
140+
LineNumber: node.StartPoint().Row + 1,
138141
}, true
139142
}
140143
return module{}, false
@@ -144,8 +147,8 @@ func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
144147
// an import statement. It updates FileParser.output.Modules with the `module` that the
145148
// import represents.
146149
func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
147-
if node.Kind() == sitterNodeTypeImportStatement {
148-
for j := uint(1); j < node.ChildCount(); j++ {
150+
if node.Type() == sitterNodeTypeImportStatement {
151+
for j := 1; j < int(node.ChildCount()); j++ {
149152
m, ok := parseImportStatement(node.Child(j), p.code)
150153
if !ok {
151154
continue
@@ -156,12 +159,12 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
156159
}
157160
p.output.Modules = append(p.output.Modules, m)
158161
}
159-
} else if node.Kind() == sitterNodeTypeImportFromStatement {
160-
from := node.Child(1).Utf8Text(p.code)
162+
} else if node.Type() == sitterNodeTypeImportFromStatement {
163+
from := node.Child(1).Content(p.code)
161164
if strings.HasPrefix(from, ".") {
162165
return true
163166
}
164-
for j := uint(3); j < node.ChildCount(); j++ {
167+
for j := 3; j < int(node.ChildCount()); j++ {
165168
m, ok := parseImportStatement(node.Child(j), p.code)
166169
if !ok {
167170
continue
@@ -180,8 +183,8 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
180183
// parseComments parses a node for comments, returning true if the node is a comment.
181184
// It updates FileParser.output.Comments with the parsed comment.
182185
func (p *FileParser) parseComments(node *sitter.Node) bool {
183-
if node.Kind() == sitterNodeTypeComment {
184-
p.output.Comments = append(p.output.Comments, comment(node.Utf8Text(p.code)))
186+
if node.Type() == sitterNodeTypeComment {
187+
p.output.Comments = append(p.output.Comments, comment(node.Content(p.code)))
185188
return true
186189
}
187190
return false
@@ -197,7 +200,7 @@ func (p *FileParser) parse(ctx context.Context, node *sitter.Node) {
197200
if node == nil {
198201
return
199202
}
200-
for i := uint(0); i < node.ChildCount(); i++ {
203+
for i := 0; i < int(node.ChildCount()); i++ {
201204
if err := ctx.Err(); err != nil {
202205
return
203206
}

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 uint `json:"lineno"`
155+
LineNumber uint32 `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)