Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: Move dot invocation to rust-analyzer crate #8807

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<String, String>> {
self.with_db(|db| view_crate_graph::view_crate_graph(&db))
}
Expand Down
25 changes: 2 additions & 23 deletions crates/ide/src/view_crate_graph.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 16 additions & 4 deletions crates/rust-analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//! `ide` crate.

use std::{
io::Write as _,
process::{self, Stdio},
io::{Read, Write as _},
process::{self, Command, Stdio},
};

use ide::{
Expand Down Expand Up @@ -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(
Expand Down