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/ide/src/view_crate_graph.rs') 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