-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathjavaClassEditor.ts
More file actions
75 lines (67 loc) · 2.59 KB
/
javaClassEditor.ts
File metadata and controls
75 lines (67 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import path = require('path');
import * as vscode from 'vscode';
import { Uri, window, ExtensionContext} from "vscode";
import { getNonce } from "./webviewUtils";
class JavaClassDocument implements vscode.CustomDocument {
constructor(uri: Uri) { this.uri = uri; }
uri: Uri;
dispose(): void { }
}
export class JavaClassEditorProvider implements vscode.CustomReadonlyEditorProvider {
private context: ExtensionContext;
openCustomDocument(uri: Uri, openContext: vscode.CustomDocumentOpenContext, token: vscode.CancellationToken): JavaClassDocument {
return new JavaClassDocument(uri);
}
constructor (context: ExtensionContext) {
this.context = context;
}
public static readonly viewType = 'decompiled.javaClass';
async resolveCustomEditor(document: vscode.CustomDocument, webviewPanel: vscode.WebviewPanel, token: vscode.CancellationToken): Promise<void> {
const nonce: string = getNonce();
webviewPanel.webview.options = {
enableScripts: true,
localResourceRoots: [Uri.joinPath(Uri.parse(this.context.extensionPath), 'webview-resources')]
};
const uriString = document.uri?.toString();
let targetUri: Uri;
if (!uriString) {
targetUri = document.uri; // Fallback safety for null/undefined URIs
} else {
const fileUri = Uri.parse(uriString.replace(/^class/, "file"));
try {
// Attempt to open as a standard text document first
await vscode.workspace.openTextDocument(fileUri);
targetUri = fileUri;
} catch {
// Fallback to the 'class' scheme if the file document can't be opened
targetUri = Uri.parse(uriString.replace(/^file/, "class"));
}
}
const styleUri = Uri.file(
path.join(this.context.extensionPath, 'webview-resources', 'button.css')
);
const style: string = `<link rel="stylesheet" type="text/css" href="${webviewPanel.webview.asWebviewUri(styleUri).toString()}">`;
webviewPanel.webview.html = `
<html lang="en">
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'nonce-${nonce}'; style-src ${webviewPanel.webview.cspSource};">
${style}
</head>
<body>
<script nonce="${nonce}">
const vscode = acquireVsCodeApi();
vscode.postMessage({ command: 'decompiled' });
</script>
</body>
</html>
`;
webviewPanel.webview.onDidReceiveMessage(message => {
switch (message.command) {
case 'decompiled':
webviewPanel.dispose();
window.showTextDocument(targetUri, { preview: false });
return;
}
}, undefined, this.context.subscriptions);
}
}