Skip to content

Commit f4f68e6

Browse files
committed
Use d3-graphviz for rendering crates graph on the extension side
1 parent e1dcec0 commit f4f68e6

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

crates/rust-analyzer/src/handlers.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,9 @@ pub(crate) fn handle_view_crate_graph(
133133
let _p = profile::span("handle_view_crate_graph");
134134
let dot = snap.analysis.view_crate_graph(params.full)??;
135135

136-
// We shell out to `dot` to render to SVG, as there does not seem to be a pure-Rust renderer.
137-
let child = Command::new("dot")
138-
.arg("-Tsvg")
139-
.stdin(Stdio::piped())
140-
.stdout(Stdio::piped())
141-
.spawn()
142-
.map_err(|err| format!("failed to spawn `dot`: {}", err))?;
143-
child.stdin.unwrap().write_all(dot.as_bytes())?;
144-
145-
let mut svg = String::new();
146-
child.stdout.unwrap().read_to_string(&mut svg)?;
147-
Ok(svg)
136+
eprintln!("{}", dot);
137+
138+
Ok(dot)
148139
}
149140

150141
pub(crate) fn handle_expand_macro(

editors/code/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
"dependencies": {
3939
"https-proxy-agent": "^5.0.0",
4040
"node-fetch": "^2.6.1",
41-
"vscode-languageclient": "^7.1.0-next.5"
41+
"vscode-languageclient": "^7.1.0-next.5",
42+
"d3": "^7.0.0",
43+
"d3-graphviz": "^4.0.0"
4244
},
4345
"devDependencies": {
4446
"@types/glob": "^7.1.4",

editors/code/src/commands.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,37 @@ export function viewItemTree(ctx: Ctx): Cmd {
472472

473473
function crateGraph(ctx: Ctx, full: boolean): Cmd {
474474
return async () => {
475-
const panel = vscode.window.createWebviewPanel("rust-analyzer.crate-graph", "rust-analyzer crate graph", vscode.ViewColumn.Two);
475+
const panel = vscode.window.createWebviewPanel("rust-analyzer.crate-graph", "rust-analyzer crate graph", vscode.ViewColumn.Two, {
476+
enableScripts: true,
477+
retainContextWhenHidden: true
478+
});
476479
const params = {
477480
full: full,
478481
};
479-
const svg = await ctx.client.sendRequest(ra.viewCrateGraph, params);
480-
panel.webview.html = svg;
482+
const dot = await ctx.client.sendRequest(ra.viewCrateGraph, params);
483+
484+
console.log(dot);
485+
486+
const html = `
487+
<!DOCTYPE html>
488+
<meta charset="utf-8">
489+
490+
<body>
491+
<script src="https://d3js.org/d3.v5.min.js"></script>
492+
<script src="https://unpkg.com/@hpcc-js/[email protected]/dist/index.min.js"></script>
493+
<script src="https://unpkg.com/[email protected]/build/d3-graphviz.js"></script>
494+
<div id="graph" style="text-align: center;"></div>
495+
<script>
496+
let margin = 0;
497+
d3.select("#graph").graphviz().fit(true).width(window.innerWidth - margin).height(window.innerHeight - margin)
498+
.renderDot(\`${dot}\`);
499+
</script>
500+
</body>
501+
`;
502+
503+
console.log(html);
504+
505+
panel.webview.html = html;
481506
};
482507
}
483508

0 commit comments

Comments
 (0)