forked from redhat-developer/vscode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaClassEditor.ts
More file actions
67 lines (60 loc) · 2.34 KB
/
javaClassEditor.ts
File metadata and controls
67 lines (60 loc) · 2.34 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
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 classUri = Uri.parse((document.uri.toString()).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>
<div class="center">
<p>This file is not displayed in the text editor because it is a Java class file. Click here to decompile and open.</p>
<button id="btn"><center>Decompile Class File</center></button>
<div>
<script nonce="${nonce}">
const vscode = acquireVsCodeApi();
document.getElementById("btn").addEventListener("click", decompiled);
function decompiled() {
vscode.postMessage({ command: 'decompiled' });
}
</script>
</body>
</html>
`;
webviewPanel.webview.onDidReceiveMessage(message => {
switch (message.command) {
case 'decompiled':
webviewPanel.dispose();
window.showTextDocument(classUri, { preview: false });
return;
}
}, undefined, this.context.subscriptions);
}
}