From f4f68e62c29b08fb888e02151c54f5e6f38dd425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Oliveira?= Date: Tue, 10 Aug 2021 13:34:30 +0000 Subject: [PATCH] Use d3-graphviz for rendering crates graph on the extension side --- crates/rust-analyzer/src/handlers.rs | 13 ++---------- editors/code/package.json | 4 +++- editors/code/src/commands.ts | 31 +++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 460271a269f..fd0e51ca7c7 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -133,18 +133,9 @@ pub(crate) fn handle_view_crate_graph( let _p = profile::span("handle_view_crate_graph"); let dot = snap.analysis.view_crate_graph(params.full)??; - // 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())?; + eprintln!("{}", dot); - let mut svg = String::new(); - child.stdout.unwrap().read_to_string(&mut svg)?; - Ok(svg) + Ok(dot) } pub(crate) fn handle_expand_macro( diff --git a/editors/code/package.json b/editors/code/package.json index 48b2eddd101..453abb6b1cf 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -38,7 +38,9 @@ "dependencies": { "https-proxy-agent": "^5.0.0", "node-fetch": "^2.6.1", - "vscode-languageclient": "^7.1.0-next.5" + "vscode-languageclient": "^7.1.0-next.5", + "d3": "^7.0.0", + "d3-graphviz": "^4.0.0" }, "devDependencies": { "@types/glob": "^7.1.4", diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index 227b9032987..6b69e5229b4 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -472,12 +472,37 @@ export function viewItemTree(ctx: Ctx): Cmd { function crateGraph(ctx: Ctx, full: boolean): Cmd { return async () => { - const panel = vscode.window.createWebviewPanel("rust-analyzer.crate-graph", "rust-analyzer crate graph", vscode.ViewColumn.Two); + const panel = vscode.window.createWebviewPanel("rust-analyzer.crate-graph", "rust-analyzer crate graph", vscode.ViewColumn.Two, { + enableScripts: true, + retainContextWhenHidden: true + }); const params = { full: full, }; - const svg = await ctx.client.sendRequest(ra.viewCrateGraph, params); - panel.webview.html = svg; + const dot = await ctx.client.sendRequest(ra.viewCrateGraph, params); + + console.log(dot); + + const html = ` + + + + + + + +
+ + + `; + + console.log(html); + + panel.webview.html = html; }; }