aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-05-11 23:14:59 +0100
committerJonas Schievink <[email protected]>2021-05-11 23:14:59 +0100
commit23cd6d0d562552886c4b170327b22fb226fb9cb1 (patch)
tree852aceb23512b159e24316f58ef32c2f09ed8eb7 /crates
parenta328a6bc75e3832930bd22fb644a994dcd3d93ff (diff)
Move `dot` invocation to rust-analyzer crate
Diffstat (limited to 'crates')
-rw-r--r--crates/ide/src/lib.rs1
-rw-r--r--crates/ide/src/view_crate_graph.rs25
-rw-r--r--crates/rust-analyzer/src/handlers.rs20
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 @@
1use std::{ 1use std::sync::Arc;
2 error::Error,
3 io::{Read, Write},
4 process::{Command, Stdio},
5 sync::Arc,
6};
7 2
8use dot::{Id, LabelText}; 3use dot::{Id, LabelText};
9use ide_db::{ 4use 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
45fn 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
60struct DotCrateGraph { 39struct 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
5use std::{ 5use std::{
6 io::Write as _, 6 io::{Read, Write as _},
7 process::{self, Stdio}, 7 process::{self, Command, Stdio},
8}; 8};
9 9
10use ide::{ 10use ide::{
@@ -119,8 +119,20 @@ pub(crate) fn handle_view_hir(
119 119
120pub(crate) fn handle_view_crate_graph(snap: GlobalStateSnapshot, (): ()) -> Result<String> { 120pub(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
126pub(crate) fn handle_expand_macro( 138pub(crate) fn handle_expand_macro(