]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide/src/view_crate_graph.rs
Auto merge of #99603 - fasterthanlime:ra-subtree-2, 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     FxHashSet, RootDatabase,
7 };
8
9 // Feature: View Crate Graph
10 //
11 // Renders the currently loaded crate graph as an SVG graphic. Requires the `dot` tool, which
12 // is part of graphviz, to be installed.
13 //
14 // Only workspace crates are included, no crates.io dependencies or sysroot crates.
15 //
16 // |===
17 // | Editor  | Action Name
18 //
19 // | VS Code | **Rust Analyzer: View Crate Graph**
20 // |===
21 pub(crate) fn view_crate_graph(db: &RootDatabase, full: bool) -> Result<String, String> {
22     let crate_graph = db.crate_graph();
23     let crates_to_render = crate_graph
24         .iter()
25         .filter(|krate| {
26             if full {
27                 true
28             } else {
29                 // Only render workspace crates
30                 let root_id = db.file_source_root(crate_graph[*krate].root_file_id);
31                 !db.source_root(root_id).is_library
32             }
33         })
34         .collect();
35     let graph = DotCrateGraph { graph: crate_graph, crates_to_render };
36
37     let mut dot = Vec::new();
38     dot::render(&graph, &mut dot).unwrap();
39     Ok(String::from_utf8(dot).unwrap())
40 }
41
42 struct DotCrateGraph {
43     graph: Arc<CrateGraph>,
44     crates_to_render: FxHashSet<CrateId>,
45 }
46
47 type Edge<'a> = (CrateId, &'a Dependency);
48
49 impl<'a> dot::GraphWalk<'a, CrateId, Edge<'a>> for DotCrateGraph {
50     fn nodes(&'a self) -> dot::Nodes<'a, CrateId> {
51         self.crates_to_render.iter().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| {
58                 self.graph[*krate]
59                     .dependencies
60                     .iter()
61                     .filter(|dep| self.crates_to_render.contains(&dep.crate_id))
62                     .map(move |dep| (*krate, dep))
63             })
64             .collect()
65     }
66
67     fn source(&'a self, edge: &Edge<'a>) -> CrateId {
68         edge.0
69     }
70
71     fn target(&'a self, edge: &Edge<'a>) -> CrateId {
72         edge.1.crate_id
73     }
74 }
75
76 impl<'a> dot::Labeller<'a, CrateId, 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: &CrateId) -> Id<'a> {
82         Id::new(format!("_{}", n.0)).unwrap()
83     }
84
85     fn node_shape(&'a self, _node: &CrateId) -> Option<LabelText<'a>> {
86         Some(LabelText::LabelStr("box".into()))
87     }
88
89     fn node_label(&'a self, n: &CrateId) -> LabelText<'a> {
90         let name = self.graph[*n].display_name.as_ref().map_or("(unnamed crate)", |name| &*name);
91         LabelText::LabelStr(name.into())
92     }
93 }