ide/
view_crate_graph.rs

1use dot::{Id, LabelText};
2use ide_db::base_db::salsa::plumbing::AsId;
3use ide_db::{
4    FxHashMap, RootDatabase,
5    base_db::{
6        BuiltCrateData, BuiltDependency, Crate, ExtraCrateData, RootQueryDb, SourceDatabase,
7    },
8};
9
10// Feature: View Crate Graph
11//
12// Renders the currently loaded crate graph as an SVG graphic. Requires the `dot` tool, which
13// is part of graphviz, to be installed.
14//
15// Only workspace crates are included, no crates.io dependencies or sysroot crates.
16//
17// | Editor  | Action Name |
18// |---------|-------------|
19// | VS Code | **rust-analyzer: View Crate Graph** |
20pub(crate) fn view_crate_graph(db: &RootDatabase, full: bool) -> Result<String, String> {
21    let all_crates = db.all_crates();
22    let crates_to_render = all_crates
23        .iter()
24        .copied()
25        .map(|krate| (krate, (krate.data(db), krate.extra_data(db))))
26        .filter(|(_, (crate_data, _))| {
27            if full {
28                true
29            } else {
30                // Only render workspace crates
31                let root_id = db.file_source_root(crate_data.root_file_id).source_root_id(db);
32                !db.source_root(root_id).source_root(db).is_library
33            }
34        })
35        .collect();
36    let graph = DotCrateGraph { crates_to_render };
37
38    let mut dot = Vec::new();
39    dot::render(&graph, &mut dot).unwrap();
40    Ok(String::from_utf8(dot).unwrap())
41}
42
43struct DotCrateGraph<'db> {
44    crates_to_render: FxHashMap<Crate, (&'db BuiltCrateData, &'db ExtraCrateData)>,
45}
46
47type Edge<'a> = (Crate, &'a BuiltDependency);
48
49impl<'a> dot::GraphWalk<'a, Crate, Edge<'a>> for DotCrateGraph<'_> {
50    fn nodes(&'a self) -> dot::Nodes<'a, Crate> {
51        self.crates_to_render.keys().copied().collect()
52    }
53
54    fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> {
55        self.crates_to_render
56            .iter()
57            .flat_map(|(krate, (crate_data, _))| {
58                crate_data
59                    .dependencies
60                    .iter()
61                    .filter(|dep| self.crates_to_render.contains_key(&dep.crate_id))
62                    .map(move |dep| (*krate, dep))
63            })
64            .collect()
65    }
66
67    fn source(&'a self, edge: &Edge<'a>) -> Crate {
68        edge.0
69    }
70
71    fn target(&'a self, edge: &Edge<'a>) -> Crate {
72        edge.1.crate_id
73    }
74}
75
76impl<'a> dot::Labeller<'a, Crate, Edge<'a>> for DotCrateGraph<'_> {
77    fn graph_id(&'a self) -> Id<'a> {
78        Id::new("rust_analyzer_crate_graph").unwrap()
79    }
80
81    fn node_id(&'a self, n: &Crate) -> Id<'a> {
82        let id = n.as_id().index();
83        Id::new(format!("_{id:?}")).unwrap()
84    }
85
86    fn node_shape(&'a self, _node: &Crate) -> Option<LabelText<'a>> {
87        Some(LabelText::LabelStr("box".into()))
88    }
89
90    fn node_label(&'a self, n: &Crate) -> LabelText<'a> {
91        let name = self.crates_to_render[n]
92            .1
93            .display_name
94            .as_ref()
95            .map_or("(unnamed crate)", |name| name.as_str());
96        LabelText::LabelStr(name.into())
97    }
98}