Skip to content

Commit 80fc00c

Browse files
committed
Access root and jump-to nodes from file's in Lua
1 parent 5a22bf7 commit 80fc00c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

stack-graphs/src/lua.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@
139139
//!
140140
//! Adds a new internal scope node to this file.
141141
//!
142+
//! #### `jump_to_node`
143+
//!
144+
//! ``` lua
145+
//! local node = file:jump_to_node()
146+
//! ```
147+
//!
148+
//! Returns the root node of the graph containing this file.
149+
//!
142150
//! #### `pop_scoped_symbol_node`
143151
//!
144152
//! ``` lua
@@ -185,6 +193,14 @@
185193
//! Adds a new definition node to this file. `symbol` must be a string, or an instance that can be
186194
//! converted to a string via its `tostring` method.
187195
//!
196+
//! #### `root_node`
197+
//!
198+
//! ``` lua
199+
//! local node = file:root_node()
200+
//! ```
201+
//!
202+
//! Returns the root node of the graph containing this file.
203+
//!
188204
//! #### `scoped_definition_node`
189205
//!
190206
//! ``` lua
@@ -494,6 +510,14 @@ impl UserData for Handle<File> {
494510
Ok(node_ud)
495511
});
496512

513+
methods.add_function("jump_to_node", |l, file_ud: AnyUserData| {
514+
let graph_ud = file_ud.user_value::<AnyUserData>()?;
515+
let node = StackGraph::jump_to_node();
516+
let node_ud = l.create_userdata(node)?;
517+
node_ud.set_user_value(graph_ud)?;
518+
Ok(node_ud)
519+
});
520+
497521
methods.add_function(
498522
"pop_scoped_symbol_node",
499523
|l, (file_ud, symbol): (AnyUserData, String)| {
@@ -599,6 +623,14 @@ impl UserData for Handle<File> {
599623
},
600624
);
601625

626+
methods.add_function("root_node", |l, file_ud: AnyUserData| {
627+
let graph_ud = file_ud.user_value::<AnyUserData>()?;
628+
let node = StackGraph::root_node();
629+
let node_ud = l.create_userdata(node)?;
630+
node_ud.set_user_value(graph_ud)?;
631+
Ok(node_ud)
632+
});
633+
602634
methods.add_function(
603635
"scoped_definition_node",
604636
|l, (file_ud, symbol): (AnyUserData, String)| {

stack-graphs/tests/it/lua.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ fn can_create_all_node_types_from_lua() -> Result<(), anyhow::Error> {
258258
local root = graph:root_node()
259259
local jump_to = graph:jump_to_node()
260260
local file = graph:file("test.py")
261+
local file_root = file:root_node()
262+
local file_jump_to = file:jump_to_node()
261263
local drop_scopes = file:drop_scopes_node()
262264
local exported = file:exported_scope_node()
263265
local internal = file:internal_scope_node()

0 commit comments

Comments
 (0)