From a85a2c4d151d9d2e8fb016d76aad99a6ca88bc75 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 11 May 2021 16:15:31 +0200 Subject: Allow viewing the crate graph in a webview --- crates/ide/Cargo.toml | 1 + crates/ide/src/lib.rs | 5 +++ crates/ide/src/view_crate_graph.rs | 81 +++++++++++++++++++++++++++++++++++ crates/rust-analyzer/src/handlers.rs | 6 +++ crates/rust-analyzer/src/lsp_ext.rs | 8 ++++ crates/rust-analyzer/src/main_loop.rs | 1 + 6 files changed, 102 insertions(+) create mode 100644 crates/ide/src/view_crate_graph.rs (limited to 'crates') diff --git a/crates/ide/Cargo.toml b/crates/ide/Cargo.toml index f04bcf531..88f3d09d3 100644 --- a/crates/ide/Cargo.toml +++ b/crates/ide/Cargo.toml @@ -20,6 +20,7 @@ oorandom = "11.1.2" pulldown-cmark-to-cmark = "6.0.0" pulldown-cmark = { version = "0.8.0", default-features = false } url = "2.1.1" +dot = "0.1.4" stdx = { path = "../stdx", version = "0.0.0" } syntax = { path = "../syntax", version = "0.0.0" } diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 8e5b72044..34360501a 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -49,6 +49,7 @@ mod syntax_tree; mod typing; mod markdown_remove; mod doc_links; +mod view_crate_graph; use std::sync::Arc; @@ -287,6 +288,10 @@ impl Analysis { self.with_db(|db| view_hir::view_hir(&db, position)) } + pub fn view_crate_graph(&self) -> Cancelable> { + self.with_db(|db| view_crate_graph::view_crate_graph(&db)) + } + pub fn expand_macro(&self, position: FilePosition) -> Cancelable> { self.with_db(|db| expand_macro::expand_macro(db, position)) } diff --git a/crates/ide/src/view_crate_graph.rs b/crates/ide/src/view_crate_graph.rs new file mode 100644 index 000000000..4da4ce2b3 --- /dev/null +++ b/crates/ide/src/view_crate_graph.rs @@ -0,0 +1,81 @@ +use std::{ + error::Error, + io::{Read, Write}, + process::{Command, Stdio}, + sync::Arc, +}; + +use dot::Id; +use ide_db::{ + base_db::{CrateGraph, CrateId, Dependency, SourceDatabase}, + RootDatabase, +}; + +// Feature: View Crate Graph +// +// Renders the currently loaded crate graph as an SVG graphic. Requires the `dot` tool to be +// installed. +// +// |=== +// | Editor | Action Name +// +// | VS Code | **Rust Analyzer: View Crate Graph** +// |=== +pub(crate) fn view_crate_graph(db: &RootDatabase) -> Result { + let mut dot = Vec::new(); + let graph = DotCrateGraph(db.crate_graph()); + dot::render(&graph, &mut dot).unwrap(); + + render_svg(&dot).map_err(|e| e.to_string()) +} + +fn render_svg(dot: &[u8]) -> Result> { + // We shell out to `dot` to render to SVG, as there does not seem to be a pure-Rust renderer. + let child = Command::new("dot") + .arg("-Tsvg") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .map_err(|err| format!("failed to spawn `dot -Tsvg`: {}", err))?; + child.stdin.unwrap().write_all(&dot)?; + + let mut svg = String::new(); + child.stdout.unwrap().read_to_string(&mut svg)?; + Ok(svg) +} + +struct DotCrateGraph(Arc); + +type Edge<'a> = (CrateId, &'a Dependency); + +impl<'a> dot::GraphWalk<'a, CrateId, Edge<'a>> for DotCrateGraph { + fn nodes(&'a self) -> dot::Nodes<'a, CrateId> { + self.0.iter().collect() + } + + fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { + self.0 + .iter() + .flat_map(|krate| self.0[krate].dependencies.iter().map(move |dep| (krate, dep))) + .collect() + } + + fn source(&'a self, edge: &Edge<'a>) -> CrateId { + edge.0 + } + + fn target(&'a self, edge: &Edge<'a>) -> CrateId { + edge.1.crate_id + } +} + +impl<'a> dot::Labeller<'a, CrateId, Edge<'a>> for DotCrateGraph { + fn graph_id(&'a self) -> Id<'a> { + Id::new("rust_analyzer_crate_graph").unwrap() + } + + fn node_id(&'a self, n: &CrateId) -> Id<'a> { + let name = self.0[*n].display_name.as_ref().map_or("_missing_name_", |name| &*name); + Id::new(name).unwrap() + } +} diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index f6e40f872..dafbab6d0 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -117,6 +117,12 @@ pub(crate) fn handle_view_hir( Ok(res) } +pub(crate) fn handle_view_crate_graph(snap: GlobalStateSnapshot, (): ()) -> Result { + let _p = profile::span("handle_view_crate_graph"); + let res = snap.analysis.view_crate_graph()??; + Ok(res) +} + pub(crate) fn handle_expand_macro( snap: GlobalStateSnapshot, params: lsp_ext::ExpandMacroParams, diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index b8835a534..3bd098058 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -61,6 +61,14 @@ impl Request for ViewHir { const METHOD: &'static str = "rust-analyzer/viewHir"; } +pub enum ViewCrateGraph {} + +impl Request for ViewCrateGraph { + type Params = (); + type Result = String; + const METHOD: &'static str = "rust-analyzer/viewCrateGraph"; +} + pub enum ExpandMacro {} impl Request for ExpandMacro { diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index ce7ece559..c7bd7eee1 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -513,6 +513,7 @@ impl GlobalState { .on::(handlers::handle_analyzer_status) .on::(handlers::handle_syntax_tree) .on::(handlers::handle_view_hir) + .on::(handlers::handle_view_crate_graph) .on::(handlers::handle_expand_macro) .on::(handlers::handle_parent_module) .on::(handlers::handle_runnables) -- cgit v1.2.3 From 435c422963ef16a42070356e350c89670979e2d0 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 11 May 2021 16:36:00 +0200 Subject: Distinguish crates with identical name --- crates/ide/src/view_crate_graph.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ide/src/view_crate_graph.rs b/crates/ide/src/view_crate_graph.rs index 4da4ce2b3..8aa598170 100644 --- a/crates/ide/src/view_crate_graph.rs +++ b/crates/ide/src/view_crate_graph.rs @@ -76,6 +76,6 @@ impl<'a> dot::Labeller<'a, CrateId, Edge<'a>> for DotCrateGraph { fn node_id(&'a self, n: &CrateId) -> Id<'a> { let name = self.0[*n].display_name.as_ref().map_or("_missing_name_", |name| &*name); - Id::new(name).unwrap() + Id::new(format!("{}_{}", name, n.0)).unwrap() } } -- cgit v1.2.3 From 9e6d9baf2e301b9aebd6ae8802961bc53426200e Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 11 May 2021 16:42:27 +0200 Subject: Update crates/ide/src/view_crate_graph.rs Co-authored-by: bjorn3 --- crates/ide/src/view_crate_graph.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/view_crate_graph.rs b/crates/ide/src/view_crate_graph.rs index 8aa598170..45995bf58 100644 --- a/crates/ide/src/view_crate_graph.rs +++ b/crates/ide/src/view_crate_graph.rs @@ -13,8 +13,8 @@ use ide_db::{ // Feature: View Crate Graph // -// Renders the currently loaded crate graph as an SVG graphic. Requires the `dot` tool to be -// installed. +// Renders the currently loaded crate graph as an SVG graphic. Requires the `dot` tool, which +// is part of graphviz, to be installed. // // |=== // | Editor | Action Name -- cgit v1.2.3 From 5b3af25121b320a8eb66e2688add7883356e138f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 11 May 2021 19:17:43 +0200 Subject: Only include workspace crates --- crates/ide/src/view_crate_graph.rs | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/view_crate_graph.rs b/crates/ide/src/view_crate_graph.rs index 45995bf58..527a5ae0d 100644 --- a/crates/ide/src/view_crate_graph.rs +++ b/crates/ide/src/view_crate_graph.rs @@ -7,23 +7,36 @@ use std::{ use dot::Id; use ide_db::{ - base_db::{CrateGraph, CrateId, Dependency, SourceDatabase}, + base_db::{CrateGraph, CrateId, Dependency, SourceDatabase, SourceDatabaseExt}, RootDatabase, }; +use rustc_hash::FxHashSet; // Feature: View Crate Graph // // Renders the currently loaded crate graph as an SVG graphic. Requires the `dot` tool, which // is part of graphviz, to be installed. // +// Only workspace crates are included, no crates.io dependencies or sysroot crates. +// // |=== // | Editor | Action Name // // | VS Code | **Rust Analyzer: View Crate Graph** // |=== pub(crate) fn view_crate_graph(db: &RootDatabase) -> Result { + let crate_graph = db.crate_graph(); + let crates_to_render = crate_graph + .iter() + .filter(|krate| { + // Only render workspace crates + let root_id = db.file_source_root(crate_graph[*krate].root_file_id); + !db.source_root(root_id).is_library + }) + .collect(); + let graph = DotCrateGraph { graph: crate_graph, crates_to_render }; + let mut dot = Vec::new(); - let graph = DotCrateGraph(db.crate_graph()); dot::render(&graph, &mut dot).unwrap(); render_svg(&dot).map_err(|e| e.to_string()) @@ -36,7 +49,7 @@ fn render_svg(dot: &[u8]) -> Result> { .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() - .map_err(|err| format!("failed to spawn `dot -Tsvg`: {}", err))?; + .map_err(|err| format!("failed to spawn `dot`: {}", err))?; child.stdin.unwrap().write_all(&dot)?; let mut svg = String::new(); @@ -44,19 +57,28 @@ fn render_svg(dot: &[u8]) -> Result> { Ok(svg) } -struct DotCrateGraph(Arc); +struct DotCrateGraph { + graph: Arc, + crates_to_render: FxHashSet, +} type Edge<'a> = (CrateId, &'a Dependency); impl<'a> dot::GraphWalk<'a, CrateId, Edge<'a>> for DotCrateGraph { fn nodes(&'a self) -> dot::Nodes<'a, CrateId> { - self.0.iter().collect() + self.crates_to_render.iter().copied().collect() } fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { - self.0 + self.crates_to_render .iter() - .flat_map(|krate| self.0[krate].dependencies.iter().map(move |dep| (krate, dep))) + .flat_map(|krate| { + self.graph[*krate] + .dependencies + .iter() + .filter(|dep| self.crates_to_render.contains(&dep.crate_id)) + .map(move |dep| (*krate, dep)) + }) .collect() } @@ -75,7 +97,7 @@ impl<'a> dot::Labeller<'a, CrateId, Edge<'a>> for DotCrateGraph { } fn node_id(&'a self, n: &CrateId) -> Id<'a> { - let name = self.0[*n].display_name.as_ref().map_or("_missing_name_", |name| &*name); + let name = self.graph[*n].display_name.as_ref().map_or("_missing_name_", |name| &*name); Id::new(format!("{}_{}", name, n.0)).unwrap() } } -- cgit v1.2.3 From d1aa6bbe753e50517d568a4b7560f765fbc75bd2 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 11 May 2021 19:50:01 +0200 Subject: Better node label/shapes --- crates/ide/src/view_crate_graph.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/view_crate_graph.rs b/crates/ide/src/view_crate_graph.rs index 527a5ae0d..5e4ba881e 100644 --- a/crates/ide/src/view_crate_graph.rs +++ b/crates/ide/src/view_crate_graph.rs @@ -5,7 +5,7 @@ use std::{ sync::Arc, }; -use dot::Id; +use dot::{Id, LabelText}; use ide_db::{ base_db::{CrateGraph, CrateId, Dependency, SourceDatabase, SourceDatabaseExt}, RootDatabase, @@ -97,7 +97,15 @@ impl<'a> dot::Labeller<'a, CrateId, Edge<'a>> for DotCrateGraph { } fn node_id(&'a self, n: &CrateId) -> Id<'a> { - let name = self.graph[*n].display_name.as_ref().map_or("_missing_name_", |name| &*name); - Id::new(format!("{}_{}", name, n.0)).unwrap() + Id::new(format!("_{}", n.0)).unwrap() + } + + fn node_shape(&'a self, _node: &CrateId) -> Option> { + Some(LabelText::LabelStr("box".into())) + } + + fn node_label(&'a self, n: &CrateId) -> LabelText<'a> { + let name = self.graph[*n].display_name.as_ref().map_or("(unnamed crate)", |name| &*name); + LabelText::LabelStr(name.into()) } } -- cgit v1.2.3