2021-05-11 17:14:59 -05:00
|
|
|
use std::sync::Arc;
|
2021-05-11 09:15:31 -05:00
|
|
|
|
2021-05-11 12:50:01 -05:00
|
|
|
use dot::{Id, LabelText};
|
2021-05-11 09:15:31 -05:00
|
|
|
use ide_db::{
|
2021-05-11 12:17:43 -05:00
|
|
|
base_db::{CrateGraph, CrateId, Dependency, SourceDatabase, SourceDatabaseExt},
|
2022-04-25 11:51:59 -05:00
|
|
|
FxHashSet, RootDatabase,
|
2021-05-11 09:15:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Feature: View Crate Graph
|
|
|
|
//
|
2021-05-11 09:42:27 -05:00
|
|
|
// Renders the currently loaded crate graph as an SVG graphic. Requires the `dot` tool, which
|
|
|
|
// is part of graphviz, to be installed.
|
2021-05-11 09:15:31 -05:00
|
|
|
//
|
2021-05-11 12:17:43 -05:00
|
|
|
// Only workspace crates are included, no crates.io dependencies or sysroot crates.
|
|
|
|
//
|
2021-05-11 09:15:31 -05:00
|
|
|
// |===
|
|
|
|
// | Editor | Action Name
|
|
|
|
//
|
|
|
|
// | VS Code | **Rust Analyzer: View Crate Graph**
|
|
|
|
// |===
|
2021-07-01 17:08:05 -05:00
|
|
|
pub(crate) fn view_crate_graph(db: &RootDatabase, full: bool) -> Result<String, String> {
|
2021-05-11 12:17:43 -05:00
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
let crates_to_render = crate_graph
|
|
|
|
.iter()
|
|
|
|
.filter(|krate| {
|
2021-07-01 17:08:05 -05:00
|
|
|
if full {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
// Only render workspace crates
|
|
|
|
let root_id = db.file_source_root(crate_graph[*krate].root_file_id);
|
|
|
|
!db.source_root(root_id).is_library
|
|
|
|
}
|
2021-05-11 12:17:43 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let graph = DotCrateGraph { graph: crate_graph, crates_to_render };
|
|
|
|
|
2021-05-11 09:15:31 -05:00
|
|
|
let mut dot = Vec::new();
|
|
|
|
dot::render(&graph, &mut dot).unwrap();
|
2021-05-11 17:14:59 -05:00
|
|
|
Ok(String::from_utf8(dot).unwrap())
|
2021-05-11 09:15:31 -05:00
|
|
|
}
|
|
|
|
|
2021-05-11 12:17:43 -05:00
|
|
|
struct DotCrateGraph {
|
|
|
|
graph: Arc<CrateGraph>,
|
|
|
|
crates_to_render: FxHashSet<CrateId>,
|
|
|
|
}
|
2021-05-11 09:15:31 -05:00
|
|
|
|
|
|
|
type Edge<'a> = (CrateId, &'a Dependency);
|
|
|
|
|
|
|
|
impl<'a> dot::GraphWalk<'a, CrateId, Edge<'a>> for DotCrateGraph {
|
|
|
|
fn nodes(&'a self) -> dot::Nodes<'a, CrateId> {
|
2021-05-11 12:17:43 -05:00
|
|
|
self.crates_to_render.iter().copied().collect()
|
2021-05-11 09:15:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> {
|
2021-05-11 12:17:43 -05:00
|
|
|
self.crates_to_render
|
2021-05-11 09:15:31 -05:00
|
|
|
.iter()
|
2021-05-11 12:17:43 -05:00
|
|
|
.flat_map(|krate| {
|
|
|
|
self.graph[*krate]
|
|
|
|
.dependencies
|
|
|
|
.iter()
|
|
|
|
.filter(|dep| self.crates_to_render.contains(&dep.crate_id))
|
|
|
|
.map(move |dep| (*krate, dep))
|
|
|
|
})
|
2021-05-11 09:15:31 -05:00
|
|
|
.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> {
|
2021-05-11 12:50:01 -05:00
|
|
|
Id::new(format!("_{}", n.0)).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn node_shape(&'a self, _node: &CrateId) -> Option<LabelText<'a>> {
|
|
|
|
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())
|
2021-05-11 09:15:31 -05:00
|
|
|
}
|
|
|
|
}
|