Skip to content

Commit acde43f

Browse files
Merge #8807
8807: internal: Move `dot` invocation to rust-analyzer crate r=jonas-schievink a=jonas-schievink Addresses #8801 (comment) bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents a328a6b + 23cd6d0 commit acde43f

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

crates/ide/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ impl Analysis {
288288
self.with_db(|db| view_hir::view_hir(&db, position))
289289
}
290290

291+
/// Renders the crate graph to GraphViz "dot" syntax.
291292
pub fn view_crate_graph(&self) -> Cancelable<Result<String, String>> {
292293
self.with_db(|db| view_crate_graph::view_crate_graph(&db))
293294
}

crates/ide/src/view_crate_graph.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
use std::{
2-
error::Error,
3-
io::{Read, Write},
4-
process::{Command, Stdio},
5-
sync::Arc,
6-
};
1+
use std::sync::Arc;
72

83
use dot::{Id, LabelText};
94
use ide_db::{
@@ -38,23 +33,7 @@ pub(crate) fn view_crate_graph(db: &RootDatabase) -> Result<String, String> {
3833

3934
let mut dot = Vec::new();
4035
dot::render(&graph, &mut dot).unwrap();
41-
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)
36+
Ok(String::from_utf8(dot).unwrap())
5837
}
5938

6039
struct DotCrateGraph {

crates/rust-analyzer/src/handlers.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//! `ide` crate.
44
55
use std::{
6-
io::Write as _,
7-
process::{self, Stdio},
6+
io::{Read, Write as _},
7+
process::{self, Command, Stdio},
88
};
99

1010
use ide::{
@@ -119,8 +119,20 @@ pub(crate) fn handle_view_hir(
119119

120120
pub(crate) fn handle_view_crate_graph(snap: GlobalStateSnapshot, (): ()) -> Result<String> {
121121
let _p = profile::span("handle_view_crate_graph");
122-
let res = snap.analysis.view_crate_graph()??;
123-
Ok(res)
122+
let dot = snap.analysis.view_crate_graph()??;
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)
124136
}
125137

126138
pub(crate) fn handle_expand_macro(

0 commit comments

Comments
 (0)