Only include workspace crates

This commit is contained in:
Jonas Schievink 2021-05-11 19:17:43 +02:00
parent b8d40a02a9
commit 5b3af25121

View file

@ -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<String, String> {
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<String, Box<dyn Error>> {
.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<String, Box<dyn Error>> {
Ok(svg)
}
struct DotCrateGraph(Arc<CrateGraph>);
struct DotCrateGraph {
graph: Arc<CrateGraph>,
crates_to_render: FxHashSet<CrateId>,
}
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()
}
}