Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit df054b3

Browse files
author
Hendrik van Antwerpen
authored
Merge pull request #150 from github/add-empty-spans
Add empty_span attribute
2 parents 5013993 + f6ea9c8 commit df054b3

File tree

1 file changed

+25
-2
lines changed
  • tree-sitter-stack-graphs/src

1 file changed

+25
-2
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@
145145
//! (the entirety of the function definition) and for the _name_ of the definition (the content of
146146
//! the function's `name`).
147147
//!
148+
//! Adding the `empty_source_span` attribute will use an empty source span located at the start of the
149+
//! span of the `source_node`. This can be useful when a proper reference or definition is desired,
150+
//! and thus `source_node` is required, but the span of the available source node is too large. For
151+
//! example, a module definition which is located at the start of the program, but does span the
152+
//! whole program:
153+
//!
154+
//! ``` skip
155+
//! (program)@prog {
156+
//! ; ...
157+
//! node mod_def
158+
//! attr mod_def type = "pop_symbol", symbol = mod_name, is_definition, source_node = @prog, empty_source_span
159+
//! ; ...
160+
//! }
161+
//! ```
162+
//!
148163
//! ### Connecting stack graph nodes with edges
149164
//!
150165
//! To connect two stack graph nodes, use the `edge` statement to add an edge between them:
@@ -334,9 +349,10 @@ static SCOPE_TYPE: &'static str = "scope";
334349

335350
// Node attribute names
336351
static DEBUG_ATTR_PREFIX: &'static str = "debug_";
352+
static EMPTY_SOURCE_SPAN_ATTR: &'static str = "empty_source_span";
337353
static IS_DEFINITION_ATTR: &'static str = "is_definition";
338-
static IS_EXPORTED_ATTR: &'static str = "is_exported";
339354
static IS_ENDPOINT_ATTR: &'static str = "is_endpoint";
355+
static IS_EXPORTED_ATTR: &'static str = "is_exported";
340356
static IS_REFERENCE_ATTR: &'static str = "is_reference";
341357
static SCOPE_ATTR: &'static str = "scope";
342358
static SOURCE_NODE_ATTR: &'static str = "source_node";
@@ -870,7 +886,13 @@ impl<'a> Builder<'a> {
870886
Some(source_node) => &self.graph[source_node.as_syntax_node_ref()?],
871887
None => return Ok(()),
872888
};
873-
let span = self.span_calculator.for_node(source_node);
889+
let mut span = self.span_calculator.for_node(source_node);
890+
if match node.attributes.get(EMPTY_SOURCE_SPAN_ATTR) {
891+
Some(empty_source_span) => empty_source_span.as_boolean()?,
892+
None => false,
893+
} {
894+
span.end = span.start.clone();
895+
}
874896
let containing_line = &self.source[span.start.containing_line.clone()];
875897
let containing_line = self.stack_graph.add_string(containing_line);
876898
let source_info = self.stack_graph.source_info_mut(node_handle);
@@ -912,6 +934,7 @@ impl<'a> Builder<'a> {
912934
let id = id.as_str();
913935
if !allowed_attributes.contains(id)
914936
&& id != SOURCE_NODE_ATTR
937+
&& id != EMPTY_SOURCE_SPAN_ATTR
915938
&& !id.starts_with(DEBUG_ATTR_PREFIX)
916939
{
917940
eprintln!("Unexpected attribute {} on node of type {}", id, node_type);

0 commit comments

Comments
 (0)