Skip to content

Commit 0d4f95a

Browse files
Only load definions for definitions
1 parent 1c39888 commit 0d4f95a

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

tree-sitter-stack-graphs/src/lib.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@ static TYPE_ATTR: &'static str = "type";
391391

392392
// Expected attributes per node type
393393
static POP_SCOPED_SYMBOL_ATTRS: Lazy<HashSet<&'static str>> =
394-
Lazy::new(|| HashSet::from([TYPE_ATTR, SYMBOL_ATTR, IS_DEFINITION_ATTR]));
394+
Lazy::new(|| HashSet::from([TYPE_ATTR, SYMBOL_ATTR, IS_DEFINITION_ATTR, DEFINIENS_NODE]));
395395
static POP_SYMBOL_ATTRS: Lazy<HashSet<&'static str>> =
396-
Lazy::new(|| HashSet::from([TYPE_ATTR, SYMBOL_ATTR, IS_DEFINITION_ATTR]));
396+
Lazy::new(|| HashSet::from([TYPE_ATTR, SYMBOL_ATTR, IS_DEFINITION_ATTR, DEFINIENS_NODE]));
397397
static PUSH_SCOPED_SYMBOL_ATTRS: Lazy<HashSet<&'static str>> =
398398
Lazy::new(|| HashSet::from([TYPE_ATTR, SYMBOL_ATTR, SCOPE_ATTR, IS_REFERENCE_ATTR]));
399399
static PUSH_SYMBOL_ATTRS: Lazy<HashSet<&'static str>> =
@@ -1019,10 +1019,14 @@ impl<'a> Builder<'a> {
10191019
let id = self.node_id_for_graph_node(node_ref);
10201020
let is_definition = self.load_flag(node, IS_DEFINITION_ATTR)?;
10211021
self.verify_attributes(node, POP_SCOPED_SYMBOL_TYPE, &POP_SCOPED_SYMBOL_ATTRS);
1022-
Ok(self
1022+
let node_handle = self
10231023
.stack_graph
10241024
.add_pop_scoped_symbol_node(id, symbol, is_definition)
1025-
.unwrap())
1025+
.unwrap();
1026+
if is_definition {
1027+
self.load_definiens_info(node_ref, node_handle)?;
1028+
}
1029+
Ok(node_handle)
10261030
}
10271031

10281032
fn load_pop_symbol(&mut self, node_ref: GraphNodeRef) -> Result<Handle<Node>, BuildError> {
@@ -1035,10 +1039,14 @@ impl<'a> Builder<'a> {
10351039
let id = self.node_id_for_graph_node(node_ref);
10361040
let is_definition = self.load_flag(node, IS_DEFINITION_ATTR)?;
10371041
self.verify_attributes(node, POP_SYMBOL_TYPE, &POP_SYMBOL_ATTRS);
1038-
Ok(self
1042+
let node_handle = self
10391043
.stack_graph
10401044
.add_pop_symbol_node(id, symbol, is_definition)
1041-
.unwrap())
1045+
.unwrap();
1046+
if is_definition {
1047+
self.load_definiens_info(node_ref, node_handle)?;
1048+
}
1049+
Ok(node_handle)
10421050
}
10431051

10441052
fn load_push_scoped_symbol(
@@ -1127,12 +1135,6 @@ impl<'a> Builder<'a> {
11271135
source_info.span = span;
11281136
source_info.containing_line = ControlledOption::some(containing_line);
11291137
};
1130-
if let Some(definiens_node) = node.attributes.get(DEFINIENS_NODE) {
1131-
let definiens_node = &self.graph[definiens_node.as_syntax_node_ref()?];
1132-
let span = self.span_calculator.for_node(definiens_node);
1133-
let source_info = self.stack_graph.source_info_mut(node_handle);
1134-
source_info.definiens_span = span;
1135-
}
11361138
if let Some(syntax_type) = node.attributes.get(SYNTAX_TYPE) {
11371139
let syntax_type = syntax_type.as_str()?;
11381140
let interned_string = self.stack_graph.add_string(syntax_type);
@@ -1142,6 +1144,22 @@ impl<'a> Builder<'a> {
11421144
Ok(())
11431145
}
11441146

1147+
fn load_definiens_info(
1148+
&mut self,
1149+
node_ref: GraphNodeRef,
1150+
node_handle: Handle<Node>,
1151+
) -> Result<(), BuildError> {
1152+
let node = &self.graph[node_ref];
1153+
let definiens_node = match node.attributes.get(DEFINIENS_NODE) {
1154+
Some(definiens_node) => &self.graph[definiens_node.as_syntax_node_ref()?],
1155+
None => return Ok(()),
1156+
};
1157+
let span = self.span_calculator.for_node(definiens_node);
1158+
let source_info = self.stack_graph.source_info_mut(node_handle);
1159+
source_info.definiens_span = span;
1160+
Ok(())
1161+
}
1162+
11451163
fn load_node_debug_info(
11461164
&mut self,
11471165
node_ref: GraphNodeRef,

tree-sitter-stack-graphs/tests/it/nodes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ fn can_calculate_spans() {
288288
#[test]
289289
fn can_set_definiens() {
290290
let tsg = r#"
291-
(function_definition body:(_)@body) {
291+
(function_definition name:(_)@name body:(_)@body) {
292292
node result
293+
attr (result) type = "pop_symbol", symbol = (source-text @name), source_node = @name, is_definition
293294
attr (result) definiens_node = @body
294295
}
295296
"#;

0 commit comments

Comments
 (0)