Skip to content

Commit

Permalink
Calculate spans in Lua stack graph scripts
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dcreager committed Nov 21, 2023
1 parent 0c639ff commit 39ee686
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions tree-sitter-stack-graphs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ lsp = [
lua = [
"dep:mlua",
"dep:mlua-tree-sitter",
"lsp-positions/lua",
"lsp-positions/tree-sitter",
"stack-graphs/lua",
]

Expand Down
4 changes: 3 additions & 1 deletion tree-sitter-stack-graphs/src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

use std::borrow::Cow;

use lsp_positions::lua::Module as _;
use mlua::Lua;
use mlua_tree_sitter::Module;
use mlua_tree_sitter::Module as _;
use mlua_tree_sitter::WithSource;
use stack_graphs::arena::Handle;
use stack_graphs::graph::File;
Expand Down Expand Up @@ -82,6 +83,7 @@ impl StackGraphLanguageLua {
// Create a Lua environment and load the language's stack graph rules.
// TODO: Sandbox the Lua environment
let lua = Lua::new();
lua.open_lsp_positions()?;
lua.open_ltreesitter()?;
lua.load(self.lua_source.as_ref())
.set_name(&self.lua_source_name)
Expand Down
6 changes: 4 additions & 2 deletions tree-sitter-stack-graphs/tests/it/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ impl CheckLua for mlua::Lua {
fn can_build_stack_graph_from_lua() -> Result<(), anyhow::Error> {
const LUA: &[u8] = br#"
function process(parsed, file)
-- TODO: fill in the definiens span from the parse tree root
local sc = lsp_positions.SpanCalculator.new_from_tree(parsed)
local module_ast = parsed:root()
local module = file:internal_scope_node()
module:set_definiens_span(sc:for_node(module_ast))
module:add_edge_from(file:root_node())
end
"#;
Expand All @@ -52,7 +54,7 @@ fn can_build_stack_graph_from_lua() -> Result<(), anyhow::Error> {
local graph = ...
local file = graph:file("test.py")
assert_deepeq("nodes", {
"[test.py(0) scope]",
"[test.py(0) scope def 1:6-3:4]",
}, iter_tostring(file:nodes()))
assert_deepeq("edges", {
"[root] -0-> [test.py(0) scope]",
Expand Down

0 comments on commit 39ee686

Please sign in to comment.