Skip to content

Commit

Permalink
Allow null values for definiens_node
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikvanantwerpen committed Oct 9, 2023
1 parent 31fbee7 commit edc7110
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tree-sitter-stack-graphs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@
//! }
//! ```
//!
//! Definiens are optional and setting them to `#null` explicitly is allowed.
//!
//! ### Connecting stack graph nodes with edges
//!
//! To connect two stack graph nodes, use the `edge` statement to add an edge between them:
Expand Down Expand Up @@ -1165,6 +1167,7 @@ impl<'a> Builder<'a> {
) -> Result<(), BuildError> {
let node = &self.graph[node_ref];
let definiens_node = match node.attributes.get(DEFINIENS_NODE) {
Some(Value::Null) => return Ok(()),
Some(definiens_node) => &self.graph[definiens_node.as_syntax_node_ref()?],
None => return Ok(()),
};
Expand Down
20 changes: 20 additions & 0 deletions tree-sitter-stack-graphs/tests/it/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,26 @@ fn can_set_definiens() {
assert_eq!("2:8-2:12", actual_span)
}

#[test]
fn can_set_null_definiens() {
let tsg = r#"
(function_definition name:(_)@name) {
node result
attr (result) type = "pop_symbol", symbol = (source-text @name), source_node = @name, is_definition
attr (result) definiens_node = #null
}
"#;
let python = r#"
def foo():
pass
"#;

let (graph, file) = build_stack_graph(python, tsg).unwrap();
let node_handle = graph.nodes_for_file(file).next().unwrap();
let source_info = graph.source_info(node_handle).unwrap();
assert_eq!(lsp_positions::Span::default(), source_info.definiens_span)
}

#[test]
fn can_set_syntax_type() {
let tsg = r#"
Expand Down

0 comments on commit edc7110

Please sign in to comment.