]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide/src/view_crate_graph.rs
Auto merge of #98483 - dvtkrlbs:bootstrap-dist, r=jyn514
[rust.git] / src / tools / rust-analyzer / crates / ide / src / view_crate_graph.rs
1 use std::sync::Arc;
2
3 use dot::{Id, LabelText};
4 use ide_db::{
5     base_db::{CrateGraph, CrateId, Dependency, SourceDatabase, SourceDatabaseExt},
6     RootDatabase,
7 };
8 use stdx::hash::NoHashHashSet;
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 // |===
18 // | Editor  | Action Name
19 //
20 // | VS Code | **rust-analyzer: View Crate Graph**
21 // |===
22 pub(crate) fn view_crate_graph(db: &RootDatabase, full: bool) -> Result<String, String> {
23     let crate_graph = db.crate_graph();
24     let crates_to_render = crate_graph
25         .iter()
26         .filter(|krate| {
27             if full {
28                 true
29             } else {
30                 // Only render workspace crates
31                 let root_id = db.file_source_root(crate_graph[*krate].root_file_id);
32                 !db.source_root(root_id).is_library
33             }
34         })
35         .collect();
36     let graph = DotCrateGraph { graph: crate_graph, 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
43 struct DotCrateGraph {
44     graph: Arc<CrateGraph>,
45     crates_to_render: NoHashHashSet<CrateId>,
46 }
47
48 type Edge<'a> = (CrateId, &'a Dependency);
49
50 impl<'a> dot::GraphWalk<'a, CrateId, Edge<'a>> for DotCrateGraph {
51     fn nodes(&'a self) -> dot::Nodes<'a, CrateId> {
52         self.crates_to_render.iter().copied().collect()
53     }
54
55     fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> {
56         self.crates_to_render
57             .iter()
58             .flat_map(|krate| {
59                 self.graph[*krate]
60                     .dependencies
61                     .iter()
62                     .filter(|dep| self.crates_to_render.contains(&dep.crate_id))
63                     .map(move |dep| (*krate, dep))
64             })
65             .collect()
66     }
67
68     fn source(&'a self, edge: &Edge<'a>) -> CrateId {
69         edge.0
70     }
71
72     fn target(&'a self, edge: &Edge<'a>) -> CrateId {
73         edge.1.crate_id
74     }
75 }
76
77 impl<'a> dot::Labeller<'a, CrateId, Edge<'a>> for DotCrateGraph {
78     fn graph_id(&'a self) -> Id<'a> {
79         Id::new("rust_analyzer_crate_graph").unwrap()
80     }
81
82     fn node_id(&'a self, n: &CrateId) -> Id<'a> {
83         Id::new(format!("_{}", n.0)).unwrap()
84     }
85
86     fn node_shape(&'a self, _node: &CrateId) -> Option<LabelText<'a>> {
87         Some(LabelText::LabelStr("box".into()))
88     }
89
90     fn node_label(&'a self, n: &CrateId) -> LabelText<'a> {
91         let name = self.graph[*n].display_name.as_ref().map_or("(unnamed crate)", |name| &*name);
92         LabelText::LabelStr(name.into())
93     }
94 }