Skip to content

Commit 06e8340

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 05c071b commit 06e8340

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
@@ -31,8 +31,10 @@ impl CheckLua for mlua::Lua {
3131
fn can_build_stack_graph_from_lua() -> Result<(), anyhow::Error> {
3232
const LUA: &[u8] = br#"
3333
function process(parsed, file)
34-
-- TODO: fill in the definiens span from the parse tree root
34+
local sc = lsp_positions.SpanCalculator.new_from_tree(parsed)
35+
local module_ast = parsed:root()
3536
local module = file:internal_scope_node()
37+
module:set_definiens_span(sc:for_node(module_ast))
3638
module:add_edge_from(file:root_node())
3739
end
3840
"#;
@@ -54,7 +56,7 @@ fn can_build_stack_graph_from_lua() -> Result<(), anyhow::Error> {
5456
local graph = ...
5557
local file = graph:file("test.py")
5658
assert_deepeq("nodes", {
57-
"[test.py(0) scope]",
59+
"[test.py(0) scope def 1:6-3:4]",
5860
}, iter_tostring(file:nodes()))
5961
"#,
6062
)?;

0 commit comments

Comments
 (0)