Skip to content

Commit

Permalink
editor/code: Allow to Type Checking "show crate graph" webview code
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsuharuohzeki committed Jul 11, 2023
1 parent c32d72a commit 4ef41cb
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 36 deletions.
6 changes: 6 additions & 0 deletions editors/code/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ async function bundleSource(options) {

await Promise.all([
bundleSource(createBuildOption(["src/main.ts"])),
bundleSource(
createBuildOptionForWebView([
"src/webview/show_crate_graph.ts",
"src/webview/show_crate_graph.css",
])
),
bundleSource(
createBuildOptionForWebView([
"src/webview/view_memory_layout.ts",
Expand Down
51 changes: 15 additions & 36 deletions editors/code/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type { LanguageClient } from "vscode-languageclient/node";
import { LINKED_COMMANDS } from "./client";
import type { DependencyId } from "./dependencies_provider";
import { unwrapUndefinable } from "./undefinable";
import { getWebViewModulePath } from "./uri";
import { getNodeModulePath, getWebViewModulePath } from "./uri";

export * from "./ast_inspector";
export * from "./run";
Expand Down Expand Up @@ -737,7 +737,8 @@ export function viewItemTree(ctx: CtxInit): Cmd {

function crateGraph(ctx: CtxInit, full: boolean): Cmd {
return async () => {
const nodeModulesPath = vscode.Uri.file(path.join(ctx.extensionPath, "node_modules"));
const nodeModulesPath = getNodeModulePath(ctx);
const webviewModulePath = getWebViewModulePath(ctx);

const panel = vscode.window.createWebviewPanel(
"rust-analyzer.crate-graph",
Expand All @@ -746,55 +747,33 @@ function crateGraph(ctx: CtxInit, full: boolean): Cmd {
{
enableScripts: true,
retainContextWhenHidden: true,
localResourceRoots: [nodeModulesPath],
localResourceRoots: [nodeModulesPath, webviewModulePath],
}
);
const params = {
full: full,
};
const client = ctx.client;
const dot = await client.sendRequest(ra.viewCrateGraph, params);
const uri = panel.webview.asWebviewUri(nodeModulesPath);

const nodeModuleUri = panel.webview.asWebviewUri(nodeModulesPath);
const webviewModuleUri = panel.webview.asWebviewUri(webviewModulePath);

const html = `
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<style>
/* Fill the entire view */
html, body { margin:0; padding:0; overflow:hidden }
svg { position:fixed; top:0; left:0; height:100%; width:100% }
/* Disable the graphviz background and fill the polygons */
.graph > polygon { display:none; }
:is(.node,.edge) polygon { fill: white; }
/* Invert the line colours for dark themes */
body:not(.vscode-light) .edge path { stroke: white; }
</style>
<link href="${webviewModuleUri}/show_crate_graph.css" rel="stylesheet" media="screen"/>
</head>
<body>
<script type="text/javascript" src="${uri}/d3/dist/d3.min.js"></script>
<script type="text/javascript" src="${uri}/@hpcc-js/wasm/dist/graphviz.umd.js"></script>
<script type="text/javascript" src="${uri}/d3-graphviz/build/d3-graphviz.min.js"></script>
<script type="text/javascript" src="${nodeModuleUri}/d3/dist/d3.min.js"></script>
<script type="text/javascript" src="${nodeModuleUri}/@hpcc-js/wasm/dist/graphviz.umd.js"></script>
<script type="text/javascript" src="${nodeModuleUri}/d3-graphviz/build/d3-graphviz.min.js"></script>
<div id="graph"></div>
<script>
let dot = \`${dot}\`;
let graph = d3.select("#graph")
.graphviz({ useWorker: false, useSharedWorker: false })
.fit(true)
.zoomScaleExtent([0.1, Infinity])
.renderDot(dot);
d3.select(window).on("click", (event) => {
if (event.ctrlKey) {
graph.resetZoom(d3.transition().duration(100));
}
});
d3.select(window).on("copy", (event) => {
event.clipboardData.setData("text/plain", dot);
event.preventDefault();
});
<script type="module">
import { showCrateDependencyGraph } from '${webviewModuleUri}/show_crate_graph.js';
const dot = ${JSON.stringify(dot)};
showCrateDependencyGraph(dot);
</script>
</body>
`;
Expand Down
4 changes: 4 additions & 0 deletions editors/code/src/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ function getBundledAssetsUri(ctx: CtxInit, pathname: string): Uri {
export function getWebViewModulePath(ctx: CtxInit) {
return getBundledAssetsUri(ctx, "out/webview");
}

export function getNodeModulePath(ctx: CtxInit) {
return getBundledAssetsUri(ctx, "node_modules");
}
27 changes: 27 additions & 0 deletions editors/code/src/webview/show_crate_graph.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Fill the entire view */
html,
body {
margin: 0;
padding: 0;
overflow: hidden;
}
svg {
position: fixed;
top: 0;
left: 0;
height: 100%;
width: 100%;
}

/* Disable the graphviz background and fill the polygons */
.graph > polygon {
display: none;
}
:is(.node, .edge) polygon {
fill: white;
}

/* Invert the line colours for dark themes */
body:not(.vscode-light) .edge path {
stroke: white;
}
19 changes: 19 additions & 0 deletions editors/code/src/webview/show_crate_graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @ts-nocheck
export function showCrateDependencyGraph(dot): void {
const graph = d3
.select("#graph")
.graphviz({ useWorker: false, useSharedWorker: false })
.fit(true)
.zoomScaleExtent([0.1, Infinity])
.renderDot(dot);

d3.select(window).on("click", (event) => {
if (event.ctrlKey) {
graph.resetZoom(d3.transition().duration(100));
}
});
d3.select(window).on("copy", (event) => {
event.clipboardData.setData("text/plain", dot);
event.preventDefault();
});
}

0 comments on commit 4ef41cb

Please sign in to comment.