This repository was archived by the owner on Sep 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathlua.rs
More file actions
66 lines (59 loc) · 2.29 KB
/
lua.rs
File metadata and controls
66 lines (59 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// -*- coding: utf-8 -*-
// ------------------------------------------------------------------------------------------------
// Copyright © 2023, stack-graphs authors.
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
// ------------------------------------------------------------------------------------------------
use lua_helpers::new_lua;
use stack_graphs::graph::StackGraph;
use tree_sitter_stack_graphs::lua::StackGraphLanguageLua;
use tree_sitter_stack_graphs::NoCancellation;
trait CheckLua {
fn check(&self, graph: &mut StackGraph, chunk: &str) -> Result<(), mlua::Error>;
}
impl CheckLua for mlua::Lua {
fn check(&self, graph: &mut StackGraph, chunk: &str) -> Result<(), mlua::Error> {
self.scope(|scope| {
let graph = graph.lua_ref_mut(&scope)?;
self.load(chunk).set_name("test chunk").call(graph)
})
}
}
// This doesn't build a very _interesting_ stack graph, but it does test that the end-to-end
// spackle all works correctly.
#[test]
fn can_build_stack_graph_from_lua() -> Result<(), anyhow::Error> {
const LUA: &[u8] = br#"
function process(parsed, file)
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
"#;
let code = r#"
def double(x):
return x * 2
"#;
let mut graph = StackGraph::new();
let file = graph.get_or_create_file("test.py");
let language =
StackGraphLanguageLua::from_static_str(tree_sitter_python::language(), LUA, "test");
language.build_stack_graph_into(&mut graph, file, code, &NoCancellation)?;
let l = new_lua()?;
l.check(
&mut graph,
r#"
local graph = ...
local file = graph:file("test.py")
assert_deepeq("nodes", {
"[test.py(0) scope def 1:6-3:4]",
}, iter_tostring(file:nodes()))
assert_deepeq("edges", {
"[root] -0-> [test.py(0) scope]",
}, iter_tostring(values(file:edges())))
"#,
)?;
Ok(())
}