diff --git a/tree-sitter-stack-graphs/src/lib.rs b/tree-sitter-stack-graphs/src/lib.rs index bca687b70..e8bdd7492 100644 --- a/tree-sitter-stack-graphs/src/lib.rs +++ b/tree-sitter-stack-graphs/src/lib.rs @@ -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: @@ -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(()), }; diff --git a/tree-sitter-stack-graphs/tests/it/nodes.rs b/tree-sitter-stack-graphs/tests/it/nodes.rs index ccb3e220e..c36a08744 100644 --- a/tree-sitter-stack-graphs/tests/it/nodes.rs +++ b/tree-sitter-stack-graphs/tests/it/nodes.rs @@ -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#"