diff options
Diffstat (limited to 'crates/ide/src/view_crate_graph.rs')
-rw-r--r-- | crates/ide/src/view_crate_graph.rs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/crates/ide/src/view_crate_graph.rs b/crates/ide/src/view_crate_graph.rs new file mode 100644 index 000000000..df6cc8aed --- /dev/null +++ b/crates/ide/src/view_crate_graph.rs | |||
@@ -0,0 +1,90 @@ | |||
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 rustc_hash::FxHashSet; | ||
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) -> Result<String, String> { | ||
23 | let crate_graph = db.crate_graph(); | ||
24 | let crates_to_render = crate_graph | ||
25 | .iter() | ||
26 | .filter(|krate| { | ||
27 | // Only render workspace crates | ||
28 | let root_id = db.file_source_root(crate_graph[*krate].root_file_id); | ||
29 | !db.source_root(root_id).is_library | ||
30 | }) | ||
31 | .collect(); | ||
32 | let graph = DotCrateGraph { graph: crate_graph, crates_to_render }; | ||
33 | |||
34 | let mut dot = Vec::new(); | ||
35 | dot::render(&graph, &mut dot).unwrap(); | ||
36 | Ok(String::from_utf8(dot).unwrap()) | ||
37 | } | ||
38 | |||
39 | struct DotCrateGraph { | ||
40 | graph: Arc<CrateGraph>, | ||
41 | crates_to_render: FxHashSet<CrateId>, | ||
42 | } | ||
43 | |||
44 | type Edge<'a> = (CrateId, &'a Dependency); | ||
45 | |||
46 | impl<'a> dot::GraphWalk<'a, CrateId, Edge<'a>> for DotCrateGraph { | ||
47 | fn nodes(&'a self) -> dot::Nodes<'a, CrateId> { | ||
48 | self.crates_to_render.iter().copied().collect() | ||
49 | } | ||
50 | |||
51 | fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { | ||
52 | self.crates_to_render | ||
53 | .iter() | ||
54 | .flat_map(|krate| { | ||
55 | self.graph[*krate] | ||
56 | .dependencies | ||
57 | .iter() | ||
58 | .filter(|dep| self.crates_to_render.contains(&dep.crate_id)) | ||
59 | .map(move |dep| (*krate, dep)) | ||
60 | }) | ||
61 | .collect() | ||
62 | } | ||
63 | |||
64 | fn source(&'a self, edge: &Edge<'a>) -> CrateId { | ||
65 | edge.0 | ||
66 | } | ||
67 | |||
68 | fn target(&'a self, edge: &Edge<'a>) -> CrateId { | ||
69 | edge.1.crate_id | ||
70 | } | ||
71 | } | ||
72 | |||
73 | impl<'a> dot::Labeller<'a, CrateId, Edge<'a>> for DotCrateGraph { | ||
74 | fn graph_id(&'a self) -> Id<'a> { | ||
75 | Id::new("rust_analyzer_crate_graph").unwrap() | ||
76 | } | ||
77 | |||
78 | fn node_id(&'a self, n: &CrateId) -> Id<'a> { | ||
79 | Id::new(format!("_{}", n.0)).unwrap() | ||
80 | } | ||
81 | |||
82 | fn node_shape(&'a self, _node: &CrateId) -> Option<LabelText<'a>> { | ||
83 | Some(LabelText::LabelStr("box".into())) | ||
84 | } | ||
85 | |||
86 | fn node_label(&'a self, n: &CrateId) -> LabelText<'a> { | ||
87 | let name = self.graph[*n].display_name.as_ref().map_or("(unnamed crate)", |name| &*name); | ||
88 | LabelText::LabelStr(name.into()) | ||
89 | } | ||
90 | } | ||