diff --git a/gazelle/MODULE.bazel b/gazelle/MODULE.bazel index 6bbc74bc61..b155870f47 100644 --- a/gazelle/MODULE.bazel +++ b/gazelle/MODULE.bazel @@ -17,14 +17,25 @@ local_path_override( go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps") go_deps.from_file(go_mod = "//:go.mod") +go_deps.module_override( + patches = ["//patches:go-tree-sitter-cc_library.patch"], + path = "github.com/tree-sitter/go-tree-sitter", + patch_strip = 1, +) +go_deps.module_override( + path = "github.com/tree-sitter/tree-sitter-python", + patches = ["//patches:tree-sitter-python-cc_library.patch"], + patch_strip = 1, +) use_repo( go_deps, "com_github_bazelbuild_buildtools", "com_github_bmatcuk_doublestar_v4", - "com_github_dougthor42_go_tree_sitter", "com_github_emirpasic_gods", "com_github_ghodss_yaml", "com_github_stretchr_testify", + "com_github_tree_sitter_go_tree_sitter", + "com_github_tree_sitter_tree_sitter_python", "in_gopkg_yaml_v2", "org_golang_x_sync", ) diff --git a/gazelle/deps.bzl b/gazelle/deps.bzl index 7253ef8194..25f8067253 100644 --- a/gazelle/deps.bzl +++ b/gazelle/deps.bzl @@ -169,6 +169,13 @@ def go_deps(): sum = "h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=", version = "v0.5.9", ) + go_repository( + name = "com_github_mattn_go_pointer", + importpath = "github.com/mattn/go-pointer", + sum = "h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=", + version = "v0.0.1", + ) + go_repository( name = "com_github_pmezard_go_difflib", importpath = "github.com/pmezard/go-difflib", @@ -182,12 +189,7 @@ def go_deps(): sum = "h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=", version = "v0.0.0-20190812154241-14fe0d1b01d4", ) - go_repository( - name = "com_github_dougthor42_go_tree_sitter", - importpath = "github.com/dougthor42/go-tree-sitter", - sum = "h1:b9s96BulIARx0konX36sJ5oZhWvAvjQBBntxp1eUukQ=", - version = "v0.0.0-20241210060307-2737e1d0de6b", - ) + go_repository( name = "com_github_stretchr_objx", importpath = "github.com/stretchr/objx", @@ -197,8 +199,94 @@ def go_deps(): go_repository( name = "com_github_stretchr_testify", importpath = "github.com/stretchr/testify", - sum = "h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=", - version = "v1.9.0", + sum = "h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=", + version = "v1.10.0", + ) + go_repository( + name = "com_github_tree_sitter_go_tree_sitter", + importpath = "github.com/tree-sitter/go-tree-sitter", + sum = "h1:sx6kcg8raRFCvc9BnXglke6axya12krCJF5xJ2sftRU=", + version = "v0.25.0", + patches = [ + "//patches:go-tree-sitter-cc_library.patch", # keep + ], + patch_strip = 1, # keep + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_c", + importpath = "github.com/tree-sitter/tree-sitter-c", + sum = "h1:nBPH3FV07DzAD7p0GfNvXM+Y7pNIoPenQWBpvM++t4c=", + version = "v0.23.4", + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_cpp", + importpath = "github.com/tree-sitter/tree-sitter-cpp", + sum = "h1:LaWZsiqQKvR65yHgKmnaqA+uz6tlDJTJFCyFIeZU/8w=", + version = "v0.23.4", + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_embedded_template", + importpath = "github.com/tree-sitter/tree-sitter-embedded-template", + sum = "h1:nFkkH6Sbe56EXLmZBqHHcamTpmz3TId97I16EnGy4rg=", + version = "v0.23.2", + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_go", + importpath = "github.com/tree-sitter/tree-sitter-go", + sum = "h1:yt5KMGnTHS+86pJmLIAZMWxukr8W7Ae1STPvQUuNROA=", + version = "v0.23.4", + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_html", + importpath = "github.com/tree-sitter/tree-sitter-html", + sum = "h1:1UYDV+Yd05GGRhVnTcbP58GkKLSHHZwVaN+lBZV11Lc=", + version = "v0.23.2", + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_java", + importpath = "github.com/tree-sitter/tree-sitter-java", + sum = "h1:J9YeMGMwXYlKSP3K4Us8CitC6hjtMjqpeOf2GGo6tig=", + version = "v0.23.5", + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_javascript", + importpath = "github.com/tree-sitter/tree-sitter-javascript", + sum = "h1:1fWupaRC0ArlHJ/QJzsfQ3Ibyopw7ZfQK4xXc40Zveo=", + version = "v0.23.1", + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_json", + importpath = "github.com/tree-sitter/tree-sitter-json", + sum = "h1:tV5rMkihgtiOe14a9LHfDY5kzTl5GNUYe6carZBn0fQ=", + version = "v0.24.8", + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_php", + importpath = "github.com/tree-sitter/tree-sitter-php", + sum = "h1:iHewsLNDmznh8kgGyfWfujsZxIz1YGbSd2ZTEM0ZiP8=", + version = "v0.23.11", + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_python", + importpath = "github.com/tree-sitter/tree-sitter-python", + sum = "h1:qHnWFR5WhtMQpxBZRwiaU5Hk/29vGju6CVtmvu5Haas=", + version = "v0.23.6", + patches = [ + "//patches:tree-sitter-python-cc_library.patch", # keep + ], + patch_strip = 1, # keep + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_ruby", + importpath = "github.com/tree-sitter/tree-sitter-ruby", + sum = "h1:T/NKHUA+iVbHM440hFx+lzVOzS4dV6z8Qw8ai+72bYo=", + version = "v0.23.1", + ) + go_repository( + name = "com_github_tree_sitter_tree_sitter_rust", + importpath = "github.com/tree-sitter/tree-sitter-rust", + sum = "h1:6AtoooCW5GqNrRpfnvl0iUhxTAZEovEmLKDbyHlfw90=", + version = "v0.23.2", ) go_repository( diff --git a/gazelle/go.mod b/gazelle/go.mod index 91d27fdd5a..fcc7cb47d1 100644 --- a/gazelle/go.mod +++ b/gazelle/go.mod @@ -7,10 +7,11 @@ require ( github.com/bazelbuild/buildtools v0.0.0-20231103205921-433ea8554e82 github.com/bazelbuild/rules_go v0.41.0 github.com/bmatcuk/doublestar/v4 v4.7.1 - github.com/dougthor42/go-tree-sitter v0.0.0-20241210060307-2737e1d0de6b github.com/emirpasic/gods v1.18.1 github.com/ghodss/yaml v1.0.0 - github.com/stretchr/testify v1.9.0 + github.com/stretchr/testify v1.10.0 + github.com/tree-sitter/go-tree-sitter v0.25.0 + github.com/tree-sitter/tree-sitter-python v0.23.6 golang.org/x/sync v0.2.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -18,6 +19,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/go-cmp v0.5.9 // indirect + github.com/mattn/go-pointer v0.0.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/mod v0.10.0 // indirect golang.org/x/sys v0.8.0 // indirect diff --git a/gazelle/go.sum b/gazelle/go.sum index 5acd4a6db5..cf5d0108d5 100644 --- a/gazelle/go.sum +++ b/gazelle/go.sum @@ -6,8 +6,6 @@ github.com/bazelbuild/buildtools v0.0.0-20231103205921-433ea8554e82 h1:HTepWP/jh github.com/bazelbuild/buildtools v0.0.0-20231103205921-433ea8554e82/go.mod h1:689QdV3hBP7Vo9dJMmzhoYIyo/9iMhEmHkJcnaPRCbo= github.com/bazelbuild/rules_go v0.41.0 h1:JzlRxsFNhlX+g4drDRPhIaU5H5LnI978wdMJ0vK4I+k= github.com/bazelbuild/rules_go v0.41.0/go.mod h1:TMHmtfpvyfsxaqfL9WnahCsXMWDMICTw7XeK9yVb+YU= -github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwNy7PA4I= -github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/bmatcuk/doublestar/v4 v4.7.1 h1:fdDeAqgT47acgwd9bd9HxJRDmc9UAmPpc+2m0CXv75Q= github.com/bmatcuk/doublestar/v4 v4.7.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -17,8 +15,6 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dougthor42/go-tree-sitter v0.0.0-20241210060307-2737e1d0de6b h1:b9s96BulIARx0konX36sJ5oZhWvAvjQBBntxp1eUukQ= -github.com/dougthor42/go-tree-sitter v0.0.0-20241210060307-2737e1d0de6b/go.mod h1:87UkDyPt18bTH/FvinLc/kj587VNYOdRKZT1la4T8Hg= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -44,11 +40,39 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0= +github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tree-sitter/go-tree-sitter v0.25.0 h1:sx6kcg8raRFCvc9BnXglke6axya12krCJF5xJ2sftRU= +github.com/tree-sitter/go-tree-sitter v0.25.0/go.mod h1:r77ig7BikoZhHrrsjAnv8RqGti5rtSyvDHPzgTPsUuU= +github.com/tree-sitter/tree-sitter-c v0.23.4 h1:nBPH3FV07DzAD7p0GfNvXM+Y7pNIoPenQWBpvM++t4c= +github.com/tree-sitter/tree-sitter-c v0.23.4/go.mod h1:MkI5dOiIpeN94LNjeCp8ljXN/953JCwAby4bClMr6bw= +github.com/tree-sitter/tree-sitter-cpp v0.23.4 h1:LaWZsiqQKvR65yHgKmnaqA+uz6tlDJTJFCyFIeZU/8w= +github.com/tree-sitter/tree-sitter-cpp v0.23.4/go.mod h1:doqNW64BriC7WBCQ1klf0KmJpdEvfxyXtoEybnBo6v8= +github.com/tree-sitter/tree-sitter-embedded-template v0.23.2 h1:nFkkH6Sbe56EXLmZBqHHcamTpmz3TId97I16EnGy4rg= +github.com/tree-sitter/tree-sitter-embedded-template v0.23.2/go.mod h1:HNPOhN0qF3hWluYLdxWs5WbzP/iE4aaRVPMsdxuzIaQ= +github.com/tree-sitter/tree-sitter-go v0.23.4 h1:yt5KMGnTHS+86pJmLIAZMWxukr8W7Ae1STPvQUuNROA= +github.com/tree-sitter/tree-sitter-go v0.23.4/go.mod h1:Jrx8QqYN0v7npv1fJRH1AznddllYiCMUChtVjxPK040= +github.com/tree-sitter/tree-sitter-html v0.23.2 h1:1UYDV+Yd05GGRhVnTcbP58GkKLSHHZwVaN+lBZV11Lc= +github.com/tree-sitter/tree-sitter-html v0.23.2/go.mod h1:gpUv/dG3Xl/eebqgeYeFMt+JLOY9cgFinb/Nw08a9og= +github.com/tree-sitter/tree-sitter-java v0.23.5 h1:J9YeMGMwXYlKSP3K4Us8CitC6hjtMjqpeOf2GGo6tig= +github.com/tree-sitter/tree-sitter-java v0.23.5/go.mod h1:NRKlI8+EznxA7t1Yt3xtraPk1Wzqh3GAIC46wxvc320= +github.com/tree-sitter/tree-sitter-javascript v0.23.1 h1:1fWupaRC0ArlHJ/QJzsfQ3Ibyopw7ZfQK4xXc40Zveo= +github.com/tree-sitter/tree-sitter-javascript v0.23.1/go.mod h1:lmGD1EJdCA+v0S1u2fFgepMg/opzSg/4pgFym2FPGAs= +github.com/tree-sitter/tree-sitter-json v0.24.8 h1:tV5rMkihgtiOe14a9LHfDY5kzTl5GNUYe6carZBn0fQ= +github.com/tree-sitter/tree-sitter-json v0.24.8/go.mod h1:F351KK0KGvCaYbZ5zxwx/gWWvZhIDl0eMtn+1r+gQbo= +github.com/tree-sitter/tree-sitter-php v0.23.11 h1:iHewsLNDmznh8kgGyfWfujsZxIz1YGbSd2ZTEM0ZiP8= +github.com/tree-sitter/tree-sitter-php v0.23.11/go.mod h1:T/kbfi+UcCywQfUNAJnGTN/fMSUjnwPXA8k4yoIks74= +github.com/tree-sitter/tree-sitter-python v0.23.6 h1:qHnWFR5WhtMQpxBZRwiaU5Hk/29vGju6CVtmvu5Haas= +github.com/tree-sitter/tree-sitter-python v0.23.6/go.mod h1:cpdthSy/Yoa28aJFBscFHlGiU+cnSiSh1kuDVtI8YeM= +github.com/tree-sitter/tree-sitter-ruby v0.23.1 h1:T/NKHUA+iVbHM440hFx+lzVOzS4dV6z8Qw8ai+72bYo= +github.com/tree-sitter/tree-sitter-ruby v0.23.1/go.mod h1:kUS4kCCQloFcdX6sdpr8p6r2rogbM6ZjTox5ZOQy8cA= +github.com/tree-sitter/tree-sitter-rust v0.23.2 h1:6AtoooCW5GqNrRpfnvl0iUhxTAZEovEmLKDbyHlfw90= +github.com/tree-sitter/tree-sitter-rust v0.23.2/go.mod h1:hfeGWic9BAfgTrc7Xf6FaOAguCFJRo3RBbs7QJ6D7MI= go.starlark.net v0.0.0-20210223155950-e043a3d3c984/go.mod h1:t3mmBBPzAVvK0L0n1drDmrQsJ8FoIx4INCqVMTr/Zo0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= diff --git a/gazelle/patches/BUILD.bazel b/gazelle/patches/BUILD.bazel new file mode 100644 index 0000000000..d518110449 --- /dev/null +++ b/gazelle/patches/BUILD.bazel @@ -0,0 +1 @@ +exports_files(glob(["*.patch"])) diff --git a/gazelle/patches/go-tree-sitter-cc_library.patch b/gazelle/patches/go-tree-sitter-cc_library.patch new file mode 100644 index 0000000000..040b767957 --- /dev/null +++ b/gazelle/patches/go-tree-sitter-cc_library.patch @@ -0,0 +1,25 @@ +diff --git a/BUILD.bazel b/BUILD.bazel +index 37704d1..b6d17a8 100755 +--- a/BUILD.bazel ++++ b/BUILD.bazel +@@ -1,5 +1,12 @@ + load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + ++cc_library( ++ name = "clib", ++ hdrs = glob(["src/**/*.h", "src/**/*.c", "include/**/*.h"]), ++ includes = ["include", "src"], ++ visibility = ["//:__subpackages__"], ++) ++ + go_library( + name = "go-tree-sitter", + srcs = [ +@@ -22,6 +30,7 @@ go_library( + "tree_sitter.go", + ], + cgo = True, ++ cdeps = ["//:clib"], + copts = ["-Iinclude -Isrc -std=c11 -D_POSIX_C_SOURCE=200112L -D_DEFAULT_SOURCE"], + importpath = "github.com/tree-sitter/go-tree-sitter", + visibility = ["//visibility:public"], diff --git a/gazelle/patches/tree-sitter-python-cc_library.patch b/gazelle/patches/tree-sitter-python-cc_library.patch new file mode 100644 index 0000000000..2bd4a23f55 --- /dev/null +++ b/gazelle/patches/tree-sitter-python-cc_library.patch @@ -0,0 +1,32 @@ +diff --git a/BUILD.bazel b/BUILD.bazel +index e69de29..1f5d973 100644 +--- a/BUILD.bazel ++++ b/BUILD.bazel +@@ -0,0 +1,11 @@ ++filegroup( ++ name = "csrcs", ++ srcs = glob(["src/**/*.h"]), ++ visibility = ["//visibility:public"], ++) ++ ++cc_library( ++ name = "clib", ++ hdrs = ["src/parser.c", "src/scanner.c"] + glob(["src/tree_sitter/*.h"]), ++ visibility = ["//bindings/go:__subpackages__"], ++) +diff --git a/bindings/go/BUILD.bazel b/bindings/go/BUILD.bazel +index f3e5232..cec7083 100644 +--- a/bindings/go/BUILD.bazel ++++ b/bindings/go/BUILD.bazel +@@ -2,9 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + + go_library( + name = "go", +- srcs = ["binding.go"], ++ srcs = ["binding.go", "//:csrcs"], + cgo = True, + copts = ["-std=c11 -fPIC"], ++ cdeps = ["//:clib"], + importpath = "github.com/tree-sitter/tree-sitter-python/bindings/go", + visibility = ["//visibility:public"], + ) diff --git a/gazelle/python/BUILD.bazel b/gazelle/python/BUILD.bazel index eb2d72e5eb..e5255c4184 100644 --- a/gazelle/python/BUILD.bazel +++ b/gazelle/python/BUILD.bazel @@ -29,7 +29,6 @@ go_library( importpath = "github.com/bazel-contrib/rules_python/gazelle/python", visibility = ["//visibility:public"], deps = [ - "//manifest", "//pythonconfig", "@bazel_gazelle//config:go_default_library", "@bazel_gazelle//label:go_default_library", @@ -37,13 +36,13 @@ go_library( "@bazel_gazelle//repo:go_default_library", "@bazel_gazelle//resolve:go_default_library", "@bazel_gazelle//rule:go_default_library", - "@com_github_bazelbuild_buildtools//build:go_default_library", + "@com_github_bazelbuild_buildtools//build", "@com_github_bmatcuk_doublestar_v4//:doublestar", - "@com_github_dougthor42_go_tree_sitter//:go-tree-sitter", - "@com_github_dougthor42_go_tree_sitter//python", "@com_github_emirpasic_gods//lists/singlylinkedlist", "@com_github_emirpasic_gods//sets/treeset", "@com_github_emirpasic_gods//utils", + "@com_github_tree_sitter_go_tree_sitter//:go-tree-sitter", + "@com_github_tree_sitter_tree_sitter_python//bindings/go", "@org_golang_x_sync//errgroup", ], ) diff --git a/gazelle/python/file_parser.go b/gazelle/python/file_parser.go index c147984fc3..efd3ffded5 100644 --- a/gazelle/python/file_parser.go +++ b/gazelle/python/file_parser.go @@ -22,8 +22,8 @@ import ( "path/filepath" "strings" - sitter "github.com/dougthor42/go-tree-sitter" - "github.com/dougthor42/go-tree-sitter/python" + sitter "github.com/tree-sitter/go-tree-sitter" + python "github.com/tree-sitter/tree-sitter-python/bindings/go" ) const ( @@ -61,12 +61,9 @@ func NewFileParser() *FileParser { // It prints a warning if parsing fails. func ParseCode(code []byte, path string) (*sitter.Node, error) { parser := sitter.NewParser() - parser.SetLanguage(python.GetLanguage()) + parser.SetLanguage(sitter.NewLanguage(python.Language())) - tree, err := parser.ParseCtx(context.Background(), nil, code) - if err != nil { - return nil, err - } + tree := parser.Parse(code, nil) root := tree.RootNode() if !root.HasError() { @@ -82,16 +79,16 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) { return root, nil } - for i := 0; i < int(root.ChildCount()); i++ { + for i := uint(0); i < root.ChildCount(); i++ { child := root.Child(i) if child.IsError() { // Example logs: // gazelle: Parse error at {Row:1 Column:0}: // def search_one_more_level[T](): - log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code)) + log.Printf("Parse error at %+v:\n%+v", child.StartPosition(), child.Utf8Text(code)) // Log the internal tree-sitter representation of what was parsed. Eg: // gazelle: The above was parsed as: (ERROR (identifier) (call function: (list (identifier)) arguments: (argument_list))) - log.Printf("The above was parsed as: %v", child.String()) + log.Printf("The above was parsed as: %v", child.Kind()) } } @@ -101,25 +98,25 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) { // parseMain returns true if the python file has an `if __name__ == "__main__":` block, // which is a common idiom for python scripts/binaries. func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool { - for i := 0; i < int(node.ChildCount()); i++ { + for i := uint(0); i < node.ChildCount(); i++ { if err := ctx.Err(); err != nil { return false } child := node.Child(i) - if child.Type() == sitterNodeTypeIfStatement && - child.Child(1).Type() == sitterNodeTypeComparisonOperator && child.Child(1).Child(1).Type() == "==" { + if child.Kind() == sitterNodeTypeIfStatement && + child.Child(1).Kind() == sitterNodeTypeComparisonOperator && child.Child(1).Child(1).Kind() == "==" { statement := child.Child(1) a, b := statement.Child(0), statement.Child(2) // convert "'__main__' == __name__" to "__name__ == '__main__'" - if b.Type() == sitterNodeTypeIdentifier { + if b.Kind() == sitterNodeTypeIdentifier { a, b = b, a } - if a.Type() == sitterNodeTypeIdentifier && a.Content(p.code) == "__name__" && - // at github.com/dougthor42/go-tree-sitter@latest (after v0.0.0-20240422154435-0628b34cbf9c we used) + if a.Kind() == sitterNodeTypeIdentifier && a.Utf8Text(p.code) == "__name__" && + // at github.com/smacker/go-tree-sitter@latest (after v0.0.0-20240422154435-0628b34cbf9c we used) // "__main__" is the second child of b. But now, it isn't. // we cannot use the latest go-tree-sitter because of the top level reference in scanner.c. - // https://github.com/dougthor42/go-tree-sitter/blob/04d6b33fe138a98075210f5b770482ded024dc0f/python/scanner.c#L1 - b.Type() == sitterNodeTypeString && string(p.code[b.StartByte()+1:b.EndByte()-1]) == "__main__" { + // https://github.com/smacker/go-tree-sitter/blob/04d6b33fe138a98075210f5b770482ded024dc0f/python/scanner.c#L1 + b.Kind() == sitterNodeTypeString && string(p.code[b.StartByte()+1:b.EndByte()-1]) == "__main__" { return true } } @@ -130,18 +127,18 @@ func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool { // parseImportStatement parses a node for an import statement, returning a `module` and a boolean // representing if the parse was OK or not. func parseImportStatement(node *sitter.Node, code []byte) (module, bool) { - switch node.Type() { + switch node.Kind() { case sitterNodeTypeDottedName: return module{ - Name: node.Content(code), - LineNumber: node.StartPoint().Row + 1, + Name: node.Utf8Text(code), + LineNumber: node.StartPosition().Row + 1, }, true case sitterNodeTypeAliasedImport: return parseImportStatement(node.Child(0), code) case sitterNodeTypeWildcardImport: return module{ Name: "*", - LineNumber: node.StartPoint().Row + 1, + LineNumber: node.StartPosition().Row + 1, }, true } return module{}, false @@ -151,8 +148,8 @@ func parseImportStatement(node *sitter.Node, code []byte) (module, bool) { // an import statement. It updates FileParser.output.Modules with the `module` that the // import represents. func (p *FileParser) parseImportStatements(node *sitter.Node) bool { - if node.Type() == sitterNodeTypeImportStatement { - for j := 1; j < int(node.ChildCount()); j++ { + if node.Kind() == sitterNodeTypeImportStatement { + for j := uint(1); j < node.ChildCount(); j++ { m, ok := parseImportStatement(node.Child(j), p.code) if !ok { continue @@ -163,12 +160,12 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool { } p.output.Modules = append(p.output.Modules, m) } - } else if node.Type() == sitterNodeTypeImportFromStatement { - from := node.Child(1).Content(p.code) + } else if node.Kind() == sitterNodeTypeImportFromStatement { + from := node.Child(1).Utf8Text(p.code) if strings.HasPrefix(from, ".") { return true } - for j := 3; j < int(node.ChildCount()); j++ { + for j := uint(3); j < node.ChildCount(); j++ { m, ok := parseImportStatement(node.Child(j), p.code) if !ok { continue @@ -187,8 +184,8 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool { // parseComments parses a node for comments, returning true if the node is a comment. // It updates FileParser.output.Comments with the parsed comment. func (p *FileParser) parseComments(node *sitter.Node) bool { - if node.Type() == sitterNodeTypeComment { - p.output.Comments = append(p.output.Comments, comment(node.Content(p.code))) + if node.Kind() == sitterNodeTypeComment { + p.output.Comments = append(p.output.Comments, comment(node.Utf8Text(p.code))) return true } return false @@ -204,7 +201,7 @@ func (p *FileParser) parse(ctx context.Context, node *sitter.Node) { if node == nil { return } - for i := 0; i < int(node.ChildCount()); i++ { + for i := uint(0); i < node.ChildCount(); i++ { if err := ctx.Err(); err != nil { return } diff --git a/gazelle/python/parser.go b/gazelle/python/parser.go index 1b2a90dddf..6b1d99c9b2 100644 --- a/gazelle/python/parser.go +++ b/gazelle/python/parser.go @@ -152,7 +152,7 @@ type module struct { // statements. Name string `json:"name"` // The line number where the import happened. - LineNumber uint32 `json:"lineno"` + LineNumber uint `json:"lineno"` // The path to the module file relative to the Bazel workspace root. Filepath string `json:"filepath"` // If this was a from import, e.g. from foo import bar, From indicates the module