Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ visibility = ["//visibility:public"],
urls = ["https://github.com/tree-sitter/tree-sitter-typescript/archive/v0.23.2.tar.gz"],
)

http_archive(
name = "tree-sitter-python",
build_file_content = """
filegroup(
name = "srcs",
srcs = glob(["src/**/*.c", "src/**/*.h"]),
visibility = ["//visibility:public"],
)
""",
sha256 = "bc7c1c922b14cb0edd75c0400a63ff260e09bd02684351ed5d38586b5771cd69",
urls = ["https://github.com/tree-sitter/tree-sitter-python/releases/download/v0.23.6/tree-sitter-python.tar.xz"],
)

http_archive(
name = "tree-sitter-ruby",
build_file_content = """
Expand Down
14 changes: 14 additions & 0 deletions common/treesitter/grammars/python/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@rules_go//go:def.bzl", "go_library")

go_library(
name = "python",
srcs = [
"binding.go",
"@tree-sitter-python//:srcs", #keep
],
cgo = True,
copts = ["-Iexternal/*tree-sitter-python"], #keep
importpath = "github.com/aspect-build/aspect-gazelle/common/treesitter/grammars/python",
visibility = ["//visibility:public"],
deps = ["//common/treesitter"],
)
17 changes: 17 additions & 0 deletions common/treesitter/grammars/python/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package python

//#include "tree_sitter/parser.h"
//TSLanguage *tree_sitter_python();
import "C"
import (
"unsafe"

"github.com/aspect-build/aspect-gazelle/common/treesitter"
)

func NewLanguage() treesitter.Language {
return treesitter.NewLanguage(
treesitter.Python,
unsafe.Pointer(C.tree_sitter_python()),
)
}
4 changes: 4 additions & 0 deletions common/treesitter/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
Go = "go"
Rust = "rust"
Ruby = "ruby"
Python = "python"
)

type Language any
Expand Down Expand Up @@ -154,6 +155,9 @@ var extLanguages = map[string]LanguageGrammar{
"thor": Ruby,
"jbuilder": Ruby,
"rabl": Ruby,

"py": Python,
"pyi": Python,
}

// In theory, this is a mirror of
Expand Down
1 change: 1 addition & 0 deletions language/orion/queries/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ go_library(
"@aspect_gazelle//common/treesitter/grammars/java",
"@aspect_gazelle//common/treesitter/grammars/json",
"@aspect_gazelle//common/treesitter/grammars/kotlin",
"@aspect_gazelle//common/treesitter/grammars/python",
"@aspect_gazelle//common/treesitter/grammars/ruby",
"@aspect_gazelle//common/treesitter/grammars/rust",
"@aspect_gazelle//common/treesitter/grammars/starlark",
Expand Down
3 changes: 3 additions & 0 deletions language/orion/queries/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aspect-build/aspect-gazelle/common/treesitter/grammars/java"
"github.com/aspect-build/aspect-gazelle/common/treesitter/grammars/json"
"github.com/aspect-build/aspect-gazelle/common/treesitter/grammars/kotlin"
"github.com/aspect-build/aspect-gazelle/common/treesitter/grammars/python"
"github.com/aspect-build/aspect-gazelle/common/treesitter/grammars/ruby"
"github.com/aspect-build/aspect-gazelle/common/treesitter/grammars/rust"
"github.com/aspect-build/aspect-gazelle/common/treesitter/grammars/starlark"
Expand Down Expand Up @@ -72,6 +73,8 @@ func toTreeLanguage(fileName string, queries plugin.NamedQueries) treesitter.Lan
return json.NewLanguage()
case treesitter.Kotlin:
return kotlin.NewLanguage()
case treesitter.Python:
return python.NewLanguage()
case treesitter.Ruby:
return ruby.NewLanguage()
case treesitter.Rust:
Expand Down
1 change: 1 addition & 0 deletions language/orion/tests/starzelle/query-python/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:generation_mode update_only
24 changes: 24 additions & 0 deletions language/orion/tests/starzelle/query-python/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@deps-test//my:rules.bzl", "x_lib")

# gazelle:generation_mode update_only

x_lib(
name = "a_lib",
srcs = ["a.py"],
deps = [
":b_lib",
":s_lib",
],
)

x_lib(
name = "b_lib",
srcs = ["b.py"],
deps = ["//lib:l_lib"],
)

x_lib(
name = "s_lib",
srcs = ["sub/s.py"],
deps = ["//lib:l_lib"],
)
1 change: 1 addition & 0 deletions language/orion/tests/starzelle/query-python/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspace(name = "query-python")
2 changes: 2 additions & 0 deletions language/orion/tests/starzelle/query-python/a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import b
import sub.s
1 change: 1 addition & 0 deletions language/orion/tests/starzelle/query-python/b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import lib.l
46 changes: 46 additions & 0 deletions language/orion/tests/starzelle/query-python/imports.axl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
aspect.gazelle_rule_kind("x_lib", {
"From": "@deps-test//my:rules.bzl",
"MergeableAttrs": ["srcs"],
"ResolveAttrs": ["deps"],
})

def prepare(_):
return aspect.PrepareResult(
sources = [aspect.SourceExtensions(".py")],
queries = {
"imports": aspect.AstQuery(
grammar = "python",
filter = "**/*.py",
query = "(import_statement name: (dotted_name) @imp)",
),
},
)

def declare(ctx):
for file in ctx.sources:
rel_modname = file.path[:file.path.rindex(".")].replace("/", ".")
ctx.targets.add(
name = path.base(file.path[:file.path.rindex(".")]) + "_lib",
kind = "x_lib",
attrs = {
"srcs": [file.path],
"deps": [
aspect.Import(
id = i.captures["imp"],
provider = "x",
src = file.path,
)
for i in file.query_results["imports"]
],
},
symbols = [aspect.Symbol(
id = ".".join([ctx.rel, rel_modname]) if ctx.rel else rel_modname,
provider = "x",
)],
)

aspect.orion_extension(
id = "python-test",
prepare = prepare,
declare = declare,
)
Empty file.
6 changes: 6 additions & 0 deletions language/orion/tests/starzelle/query-python/lib/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@deps-test//my:rules.bzl", "x_lib")

x_lib(
name = "l_lib",
srcs = ["l.py"],
)
1 change: 1 addition & 0 deletions language/orion/tests/starzelle/query-python/lib/l.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(1);
3 changes: 3 additions & 0 deletions language/orion/tests/starzelle/query-python/sub/s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import lib.l

print(1)