Skip to content

Commit 8a563e4

Browse files
committed
Use local node_modules to allow the graph to load offline
- Fix some warnings
1 parent f4f68e6 commit 8a563e4

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

crates/rust-analyzer/src/handlers.rs

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

1010
use ide::{

editors/code/src/commands.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode';
22
import * as lc from 'vscode-languageclient';
33
import * as ra from './lsp_ext';
4+
import * as path from 'path';
45

56
import { Ctx, Cmd } from './ctx';
67
import { applySnippetWorkspaceEdit, applySnippetTextEdits } from './snippets';
@@ -474,28 +475,47 @@ function crateGraph(ctx: Ctx, full: boolean): Cmd {
474475
return async () => {
475476
const panel = vscode.window.createWebviewPanel("rust-analyzer.crate-graph", "rust-analyzer crate graph", vscode.ViewColumn.Two, {
476477
enableScripts: true,
477-
retainContextWhenHidden: true
478-
});
478+
retainContextWhenHidden: true,
479+
localResourceRoots: [vscode.Uri.joinPath(vscode.Uri.parse(ctx.extensionPath), "node_modules")]
480+
});
479481
const params = {
480482
full: full,
481483
};
482484
const dot = await ctx.client.sendRequest(ra.viewCrateGraph, params);
483485

484486
console.log(dot);
485487

488+
let script_d3 = vscode.Uri.file(path.join(ctx.extensionPath, 'node_modules', 'd3', 'dist', 'd3.min.js'));
489+
let script_d3_gv = vscode.Uri.file(path.join(ctx.extensionPath, 'node_modules', 'd3-graphviz', 'build', 'd3-graphviz.min.js'));
490+
let script_wasm = vscode.Uri.file(path.join(ctx.extensionPath, 'node_modules', '@hpcc-js', 'wasm', 'dist', 'index.min.js'));
491+
492+
console.log(script_d3, script_d3_gv, script_wasm);
493+
486494
const html = `
487495
<!DOCTYPE html>
488496
<meta charset="utf-8">
489-
497+
<head>
498+
<style>
499+
body {
500+
padding: 0px
501+
}
502+
::-webkit-scrollbar {
503+
display: none;
504+
}
505+
</style>
506+
</head>
490507
<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>
508+
<script src="${panel.webview.asWebviewUri(script_d3)}"></script>
509+
<script src="${panel.webview.asWebviewUri(script_wasm)}"></script>
510+
<script src="${panel.webview.asWebviewUri(script_d3_gv)}"></script>
511+
<div id="graph"></div>
495512
<script>
496-
let margin = 0;
497-
d3.select("#graph").graphviz().fit(true).width(window.innerWidth - margin).height(window.innerHeight - margin)
498-
.renderDot(\`${dot}\`);
513+
d3.select("#graph")
514+
.graphviz()
515+
.fit(true)
516+
.width(window.innerWidth)
517+
.height(window.innerHeight)
518+
.renderDot(\`${dot}\`);
499519
</script>
500520
</body>
501521
`;

editors/code/src/ctx.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ export class Ctx {
6868
this.pushCleanup(d);
6969
}
7070

71+
get extensionPath(): string {
72+
return this.extCtx.extensionPath;
73+
}
74+
7175
get globalState(): vscode.Memento {
7276
return this.extCtx.globalState;
7377
}

0 commit comments

Comments
 (0)