]> git.lizzy.rs Git - rust.git/commitdiff
Move `dot` invocation to rust-analyzer crate
authorJonas Schievink <jonasschievink@gmail.com>
Tue, 11 May 2021 22:14:59 +0000 (00:14 +0200)
committerJonas Schievink <jonasschievink@gmail.com>
Tue, 11 May 2021 22:14:59 +0000 (00:14 +0200)
crates/ide/src/lib.rs
crates/ide/src/view_crate_graph.rs
crates/rust-analyzer/src/handlers.rs

index 34360501af8eea46de28fc148bd1d94117be26d9..db08547d1dce0fc171f49ab1fac735455e27b61e 100644 (file)
@@ -288,6 +288,7 @@ pub fn view_hir(&self, position: FilePosition) -> Cancelable<String> {
         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<Result<String, String>> {
         self.with_db(|db| view_crate_graph::view_crate_graph(&db))
     }
index 5e4ba881ef1b85664e5ef543ad3a0ccae91c9d18..df6cc8aedde78b1d85d29bea21aef55e9effb46d 100644 (file)
@@ -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<String, String> {
 
     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<String, Box<dyn Error>> {
-    // 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 {
index dafbab6d094f10a6d83c48864c297e7ec9679454..551013aa9c056fdb60384967352a818b8817a473 100644 (file)
@@ -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<String> {
     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(