Skip to content

Add syntax type and definiens to visualization #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions stack-graphs/src/serde/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl StackGraph {
.as_ref()
.map(|st| graph.add_string(&st))
.into(),
definiens_span: source_info.definiens_span.clone(),
..Default::default()
};
}
Expand Down Expand Up @@ -330,8 +331,17 @@ impl Node {
)]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
pub struct SourceInfo {
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "span_is_empty")
)]
pub span: lsp_positions::Span,
pub syntax_type: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "span_is_empty")
)]
pub definiens_span: lsp_positions::Span,
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -473,6 +483,7 @@ impl crate::graph::StackGraph {
self.source_info(handle).map(|info| SourceInfo {
span: info.span.clone(),
syntax_type: info.syntax_type.into_option().map(|ty| self[ty].to_owned()),
definiens_span: info.definiens_span.clone(),
})
}

Expand Down Expand Up @@ -597,3 +608,8 @@ impl crate::graph::StackGraph {
})
}
}

#[cfg(feature = "serde")]
fn span_is_empty(span: &lsp_positions::Span) -> bool {
*span == lsp_positions::Span::default()
}
33 changes: 22 additions & 11 deletions stack-graphs/src/visualization/visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,15 @@ class StackGraph {
tooltip.add_row("exported?", node.is_exported ? "yes" : "no");
}
if (this.node_has_source_info(node)) {
tooltip.add_row("location", this.source_info_to_str(node.source_info));
if (!this.span_is_empty(node.source_info.span)) {
tooltip.add_row("location", this.location_to_str(node.source_info.span.start));
}
if (node.source_info.syntax_type) {
tooltip.add_row("syntax type", node.source_info.syntax_type);
}
if (!this.span_is_empty(node.source_info.definiens_span)) {
tooltip.add_row("definiens span", this.span_to_str(node.source_info.definiens_span));
}
}
if (node.paths.length > 0) {
tooltip.add_row("outgoing paths", `${node.paths.length}`);
Expand Down Expand Up @@ -964,20 +972,23 @@ class StackGraph {

node_has_source_info(node) {
return node.hasOwnProperty("source_info")
&& !this.source_info_is_empty(node.source_info);
}

source_info_to_str(source_info) {
const line = source_info.span.start.line;
const column = source_info.span.start.column.grapheme_offset;
return `line ${line + 1} column ${column + 1}`;
span_to_str(span) {
return `${this.location_to_str(span.start)}–${this.location_to_str(span.end)}`;
}

location_to_str(loc) {
return `${loc.line + 1}:${loc.column.grapheme_offset + 1}`;
}

source_info_is_empty(source_info) {
return source_info.span.start.line === 0
&& source_info.span.start.column.utf8_offset === 0
&& source_info.span.end.line === 0
&& source_info.span.end.column.utf8_offset === 0;
span_is_empty(span) {
return !span
|| ( span.start.line === 0
&& span.start.column.utf8_offset === 0
&& span.end.line === 0
&& span.end.column.utf8_offset === 0
);
}

// ------------------------------------------------------------------------------------------------
Expand Down
Loading