diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/ide/src/view_crate_graph.rs | 25 | ||||
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 20 |
3 files changed, 19 insertions, 27 deletions
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 34360501a..db08547d1 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs | |||
@@ -288,6 +288,7 @@ impl Analysis { | |||
288 | self.with_db(|db| view_hir::view_hir(&db, position)) | 288 | self.with_db(|db| view_hir::view_hir(&db, position)) |
289 | } | 289 | } |
290 | 290 | ||
291 | /// Renders the crate graph to GraphViz "dot" syntax. | ||
291 | pub fn view_crate_graph(&self) -> Cancelable<Result<String, String>> { | 292 | pub fn view_crate_graph(&self) -> Cancelable<Result<String, String>> { |
292 | self.with_db(|db| view_crate_graph::view_crate_graph(&db)) | 293 | self.with_db(|db| view_crate_graph::view_crate_graph(&db)) |
293 | } | 294 | } |
diff --git a/crates/ide/src/view_crate_graph.rs b/crates/ide/src/view_crate_graph.rs index 5e4ba881e..df6cc8aed 100644 --- a/crates/ide/src/view_crate_graph.rs +++ b/crates/ide/src/view_crate_graph.rs | |||
@@ -1,9 +1,4 @@ | |||
1 | use std::{ | 1 | use std::sync::Arc; |
2 | error::Error, | ||
3 | io::{Read, Write}, | ||
4 | process::{Command, Stdio}, | ||
5 | sync::Arc, | ||
6 | }; | ||
7 | 2 | ||
8 | use dot::{Id, LabelText}; | 3 | use dot::{Id, LabelText}; |
9 | use ide_db::{ | 4 | use ide_db::{ |
@@ -38,23 +33,7 @@ pub(crate) fn view_crate_graph(db: &RootDatabase) -> Result<String, String> { | |||
38 | 33 | ||
39 | let mut dot = Vec::new(); | 34 | let mut dot = Vec::new(); |
40 | dot::render(&graph, &mut dot).unwrap(); | 35 | dot::render(&graph, &mut dot).unwrap(); |
41 | 36 | Ok(String::from_utf8(dot).unwrap()) | |
42 | render_svg(&dot).map_err(|e| e.to_string()) | ||
43 | } | ||
44 | |||
45 | fn render_svg(dot: &[u8]) -> Result<String, Box<dyn Error>> { | ||
46 | // We shell out to `dot` to render to SVG, as there does not seem to be a pure-Rust renderer. | ||
47 | let child = Command::new("dot") | ||
48 | .arg("-Tsvg") | ||
49 | .stdin(Stdio::piped()) | ||
50 | .stdout(Stdio::piped()) | ||
51 | .spawn() | ||
52 | .map_err(|err| format!("failed to spawn `dot`: {}", err))?; | ||
53 | child.stdin.unwrap().write_all(&dot)?; | ||
54 | |||
55 | let mut svg = String::new(); | ||
56 | child.stdout.unwrap().read_to_string(&mut svg)?; | ||
57 | Ok(svg) | ||
58 | } | 37 | } |
59 | 38 | ||
60 | struct DotCrateGraph { | 39 | struct DotCrateGraph { |
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index dafbab6d0..551013aa9 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -3,8 +3,8 @@ | |||
3 | //! `ide` crate. | 3 | //! `ide` crate. |
4 | 4 | ||
5 | use std::{ | 5 | use std::{ |
6 | io::Write as _, | 6 | io::{Read, Write as _}, |
7 | process::{self, Stdio}, | 7 | process::{self, Command, Stdio}, |
8 | }; | 8 | }; |
9 | 9 | ||
10 | use ide::{ | 10 | use ide::{ |
@@ -119,8 +119,20 @@ pub(crate) fn handle_view_hir( | |||
119 | 119 | ||
120 | pub(crate) fn handle_view_crate_graph(snap: GlobalStateSnapshot, (): ()) -> Result<String> { | 120 | pub(crate) fn handle_view_crate_graph(snap: GlobalStateSnapshot, (): ()) -> Result<String> { |
121 | let _p = profile::span("handle_view_crate_graph"); | 121 | let _p = profile::span("handle_view_crate_graph"); |
122 | let res = snap.analysis.view_crate_graph()??; | 122 | let dot = snap.analysis.view_crate_graph()??; |
123 | Ok(res) | 123 | |
124 | // We shell out to `dot` to render to SVG, as there does not seem to be a pure-Rust renderer. | ||
125 | let child = Command::new("dot") | ||
126 | .arg("-Tsvg") | ||
127 | .stdin(Stdio::piped()) | ||
128 | .stdout(Stdio::piped()) | ||
129 | .spawn() | ||
130 | .map_err(|err| format!("failed to spawn `dot`: {}", err))?; | ||
131 | child.stdin.unwrap().write_all(dot.as_bytes())?; | ||
132 | |||
133 | let mut svg = String::new(); | ||
134 | child.stdout.unwrap().read_to_string(&mut svg)?; | ||
135 | Ok(svg) | ||
124 | } | 136 | } |
125 | 137 | ||
126 | pub(crate) fn handle_expand_macro( | 138 | pub(crate) fn handle_expand_macro( |