-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jessica He <[email protected]>
- Loading branch information
1 parent
de77dc8
commit 80f9e86
Showing
6 changed files
with
187 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import path = require('path'); | ||
import * as vscode from 'vscode'; | ||
import { Disposable, Uri, window, ExtensionContext} from "vscode"; | ||
|
||
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); | ||
} | ||
|
||
public register(context: ExtensionContext): Disposable { | ||
this.context = context; | ||
const providerRegistration = vscode.window.registerCustomEditorProvider(JavaClassEditorProvider.viewType, javaClassEditorProvider); | ||
return providerRegistration; | ||
} | ||
|
||
private static readonly viewType = 'decompiled.javaClass'; | ||
|
||
async resolveCustomEditor(document: vscode.CustomDocument, webviewPanel: vscode.WebviewPanel, token: vscode.CancellationToken): Promise<void> { | ||
const nonce: string = this.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); | ||
} | ||
|
||
private getNonce(): string { | ||
let text = ""; | ||
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
for (let i = 0; i < 32; i++) { | ||
text += possible.charAt(Math.floor(Math.random() * possible.length)); | ||
} | ||
return text; | ||
} | ||
|
||
} | ||
|
||
export const javaClassEditorProvider: JavaClassEditorProvider = new JavaClassEditorProvider(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* https://css-tricks.com/overriding-default-button-styles/ */ | ||
|
||
button { | ||
display: inline-block; | ||
border: none; | ||
padding: 12px 16px; | ||
margin: 0; | ||
text-decoration: none; | ||
background: var(--vscode-button-background); | ||
color: #ffffff; | ||
font-family: sans-serif; | ||
font-size: 14px; | ||
cursor: pointer; | ||
text-align: center; | ||
} | ||
|
||
button:hover, | ||
button:focus { | ||
background: var(--vscode-button-hoverBackground); | ||
} | ||
button:focus { | ||
outline: 1px solid var(--vscode-button-hoverBackground); | ||
/* outline-offset: -4px; */ | ||
} | ||
|
||
.center { | ||
text-align: center; | ||
margin: 0; | ||
position: absolute; | ||
top: 50%; | ||
left: 50%; | ||
-ms-transform: translate(-50%, -50%); | ||
transform: translate(-50%, -50%); | ||
} |