From 23cd6d0d562552886c4b170327b22fb226fb9cb1 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 12 May 2021 00:14:59 +0200 Subject: Move `dot` invocation to rust-analyzer crate --- crates/ide/src/lib.rs | 1 + crates/ide/src/view_crate_graph.rs | 25 ++----------------------- crates/rust-analyzer/src/handlers.rs | 20 ++++++++++++++++---- 3 files changed, 19 insertions(+), 27 deletions(-) (limited to 'crates') 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 { self.with_db(|db| view_hir::view_hir(&db, position)) } + /// Renders the crate graph to GraphViz "dot" syntax. pub fn view_crate_graph(&self) -> Cancelable> { self.with_db(|db| view_crate_graph::view_crate_graph(&db)) } 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 @@ -use std::{ - error::Error, - io::{Read, Write}, - process::{Command, Stdio}, - sync::Arc, -}; +use std::sync::Arc; use dot::{Id, LabelText}; use ide_db::{ @@ -38,23 +33,7 @@ pub(crate) fn view_crate_graph(db: &RootDatabase) -> Result { let mut dot = Vec::new(); dot::render(&graph, &mut dot).unwrap(); - - render_svg(&dot).map_err(|e| e.to_string()) -} - -fn render_svg(dot: &[u8]) -> Result> { - // We shell out to `dot` to render to SVG, as there does not seem to be a pure-Rust renderer. - let child = Command::new("dot") - .arg("-Tsvg") - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .map_err(|err| format!("failed to spawn `dot`: {}", err))?; - child.stdin.unwrap().write_all(&dot)?; - - let mut svg = String::new(); - child.stdout.unwrap().read_to_string(&mut svg)?; - Ok(svg) + Ok(String::from_utf8(dot).unwrap()) } 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 @@ //! `ide` crate. use std::{ - io::Write as _, - process::{self, Stdio}, + io::{Read, Write as _}, + process::{self, Command, Stdio}, }; use ide::{ @@ -119,8 +119,20 @@ pub(crate) fn handle_view_hir( pub(crate) fn handle_view_crate_graph(snap: GlobalStateSnapshot, (): ()) -> Result { let _p = profile::span("handle_view_crate_graph"); - let res = snap.analysis.view_crate_graph()??; - Ok(res) + let dot = snap.analysis.view_crate_graph()??; + + // We shell out to `dot` to render to SVG, as there does not seem to be a pure-Rust renderer. + let child = Command::new("dot") + .arg("-Tsvg") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .map_err(|err| format!("failed to spawn `dot`: {}", err))?; + child.stdin.unwrap().write_all(dot.as_bytes())?; + + let mut svg = String::new(); + child.stdout.unwrap().read_to_string(&mut svg)?; + Ok(svg) } pub(crate) fn handle_expand_macro( -- cgit v1.2.3