Skip to content

Commit 39ee686

Browse files
committed
Calculate spans in Lua stack graph scripts
The stack graph builder now imports the `lsp-position` module before handing control to your Lua script. That lets you create a span calculator, and use that to fill in spans and definiens for the stack graph nodes that you create.
1 parent 0c639ff commit 39ee686

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

tree-sitter-stack-graphs/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ lsp = [
5050
lua = [
5151
"dep:mlua",
5252
"dep:mlua-tree-sitter",
53+
"lsp-positions/lua",
54+
"lsp-positions/tree-sitter",
5355
"stack-graphs/lua",
5456
]
5557

tree-sitter-stack-graphs/src/lua.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
1010
use std::borrow::Cow;
1111

12+
use lsp_positions::lua::Module as _;
1213
use mlua::Lua;
13-
use mlua_tree_sitter::Module;
14+
use mlua_tree_sitter::Module as _;
1415
use mlua_tree_sitter::WithSource;
1516
use stack_graphs::arena::Handle;
1617
use stack_graphs::graph::File;
@@ -82,6 +83,7 @@ impl StackGraphLanguageLua {
8283
// Create a Lua environment and load the language's stack graph rules.
8384
// TODO: Sandbox the Lua environment
8485
let lua = Lua::new();
86+
lua.open_lsp_positions()?;
8587
lua.open_ltreesitter()?;
8688
lua.load(self.lua_source.as_ref())
8789
.set_name(&self.lua_source_name)

tree-sitter-stack-graphs/tests/it/lua.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ impl CheckLua for mlua::Lua {
2929
fn can_build_stack_graph_from_lua() -> Result<(), anyhow::Error> {
3030
const LUA: &[u8] = br#"
3131
function process(parsed, file)
32-
-- TODO: fill in the definiens span from the parse tree root
32+
local sc = lsp_positions.SpanCalculator.new_from_tree(parsed)
33+
local module_ast = parsed:root()
3334
local module = file:internal_scope_node()
35+
module:set_definiens_span(sc:for_node(module_ast))
3436
module:add_edge_from(file:root_node())
3537
end
3638
"#;
@@ -52,7 +54,7 @@ fn can_build_stack_graph_from_lua() -> Result<(), anyhow::Error> {
5254
local graph = ...
5355
local file = graph:file("test.py")
5456
assert_deepeq("nodes", {
55-
"[test.py(0) scope]",
57+
"[test.py(0) scope def 1:6-3:4]",
5658
}, iter_tostring(file:nodes()))
5759
assert_deepeq("edges", {
5860
"[root] -0-> [test.py(0) scope]",

0 commit comments

Comments
 (0)