|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as path from 'path' |
| 3 | +import { CancellationTokenSource, ProgressLocation } from 'vscode' |
| 4 | +import { TastyDecompileRequest, TastyDecompileResult, |
| 5 | + asTastyDecompileParams, } from './protocol' |
| 6 | +import { BaseLanguageClient } from 'vscode-languageclient' |
| 7 | +import { Disposable } from 'vscode-jsonrpc' |
| 8 | + |
| 9 | +const RESULT_OK = 0 |
| 10 | +const ERROR_TASTY_VERSION = 1 |
| 11 | +const ERROR_CLASS_NOT_FOUND = 2 |
| 12 | +const ERROR_OTHER = -1 |
| 13 | + |
| 14 | +export class TastyDecompilerProvider implements Disposable { |
| 15 | + private disposables: Disposable[] = [] |
| 16 | + |
| 17 | + constructor( |
| 18 | + readonly client: BaseLanguageClient, |
| 19 | + readonly documentSelector: vscode.DocumentSelector, |
| 20 | + readonly provider: DecompiledDocumentProvider) { |
| 21 | + this.disposables.push( |
| 22 | + vscode.workspace.onDidOpenTextDocument( textDocument => { |
| 23 | + if (this.isTasty(textDocument)) { |
| 24 | + this.requestDecompile(textDocument).then( decompileResult => { |
| 25 | + switch(decompileResult.error) { |
| 26 | + case RESULT_OK: |
| 27 | + let scalaDocument = provider.makeScalaDocument(textDocument, decompileResult.scala) |
| 28 | + |
| 29 | + vscode.workspace.openTextDocument(scalaDocument).then(doc => { |
| 30 | + vscode.window.showTextDocument(doc, 1) |
| 31 | + }); |
| 32 | + |
| 33 | + let fileName = textDocument.fileName.substring(textDocument.fileName.lastIndexOf(path.sep) + 1) |
| 34 | + |
| 35 | + TastyTreeView.create(fileName, decompileResult.tastyTree) |
| 36 | + break; |
| 37 | + case ERROR_TASTY_VERSION: |
| 38 | + vscode.window.showErrorMessage("Tasty file has unexpected signature.") |
| 39 | + break; |
| 40 | + case ERROR_CLASS_NOT_FOUND: |
| 41 | + vscode.window.showErrorMessage("The class file related to this TASTy file could not be found.") |
| 42 | + break; |
| 43 | + case ERROR_OTHER: |
| 44 | + vscode.window.showErrorMessage("A decompilation error has occurred.") |
| 45 | + break; |
| 46 | + default: |
| 47 | + vscode.window.showErrorMessage("Unknown Error.") |
| 48 | + break; |
| 49 | + } |
| 50 | + }) |
| 51 | + } |
| 52 | + }) |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + dispose(): void { |
| 57 | + this.disposables.forEach(d => d.dispose()) |
| 58 | + this.disposables = [] |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Request the TASTy in `textDocument` to be decompiled |
| 63 | + */ |
| 64 | + private requestDecompile(textDocument: vscode.TextDocument): Promise<TastyDecompileResult> { |
| 65 | + const requestParams = asTastyDecompileParams(textDocument) |
| 66 | + const canceller = new CancellationTokenSource() |
| 67 | + const token = canceller.token |
| 68 | + |
| 69 | + return new Promise<TastyDecompileResult>(resolve => { |
| 70 | + resolve(vscode.window.withProgress({ |
| 71 | + location: ProgressLocation.Notification, |
| 72 | + title: "Decompiling" |
| 73 | + }, () => this.client.sendRequest(TastyDecompileRequest.type, requestParams, token) |
| 74 | + )) |
| 75 | + }).then( decompileResult => { |
| 76 | + canceller.dispose() |
| 77 | + return decompileResult |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + /** Is this document a tasty file? */ |
| 82 | + private isTasty(document: vscode.TextDocument): boolean { |
| 83 | + return vscode.languages.match(this.documentSelector, document) > 0 |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Provider of virtual, read-only, scala documents |
| 90 | + */ |
| 91 | +export class DecompiledDocumentProvider implements vscode.TextDocumentContentProvider { |
| 92 | + static scheme = 'decompiled'; |
| 93 | + |
| 94 | + private _documents = new Map<string, string>(); |
| 95 | + private _subscriptions: vscode.Disposable; |
| 96 | + |
| 97 | + constructor() { |
| 98 | + // Don't keep closed documents in memory |
| 99 | + this._subscriptions = vscode.workspace.onDidCloseTextDocument(doc => this._documents.delete(doc.uri.toString())); |
| 100 | + } |
| 101 | + |
| 102 | + dispose() { |
| 103 | + this._subscriptions.dispose(); |
| 104 | + this._documents.clear(); |
| 105 | + } |
| 106 | + |
| 107 | + provideTextDocumentContent(uri: vscode.Uri): string { |
| 108 | + let document = this._documents.get(uri.toString()); |
| 109 | + if (document) { |
| 110 | + return document; |
| 111 | + } else { |
| 112 | + return 'Failed to load result.'; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Creates a new virtual document ready to be provided and opened. |
| 118 | + * |
| 119 | + * @param textDocument The document containing the TASTy that was decompiled |
| 120 | + * @param content The source code provided by the language server |
| 121 | + */ |
| 122 | + makeScalaDocument(textDocument: vscode.TextDocument, content: string): vscode.Uri { |
| 123 | + let scalaDocument = textDocument.uri.with({ |
| 124 | + scheme: DecompiledDocumentProvider.scheme, |
| 125 | + path: textDocument.uri.path.replace(".tasty", ".scala") |
| 126 | + }) |
| 127 | + this._documents.set(scalaDocument.toString(), content); |
| 128 | + return scalaDocument; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * WebView used as container for preformatted TASTy trees |
| 134 | + */ |
| 135 | +class TastyTreeView { |
| 136 | + public static readonly viewType = 'tastyTree'; |
| 137 | + |
| 138 | + private readonly _panel: vscode.WebviewPanel; |
| 139 | + private _disposables: vscode.Disposable[] = []; |
| 140 | + |
| 141 | + /** |
| 142 | + * Create new panel for a TASTy tree in a new column or column 2 if none is currently open |
| 143 | + * |
| 144 | + * @param title The panel's title |
| 145 | + * @param content The panel's preformatted content |
| 146 | + */ |
| 147 | + public static create(title: string, content: string) { |
| 148 | + const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined; |
| 149 | + |
| 150 | + const panel = vscode.window.createWebviewPanel(TastyTreeView.viewType, "Tasty Tree", (column || vscode.ViewColumn.One) + 1, {}); |
| 151 | + |
| 152 | + new TastyTreeView(panel, title, content); |
| 153 | + } |
| 154 | + |
| 155 | + private constructor( |
| 156 | + panel: vscode.WebviewPanel, |
| 157 | + title: string, |
| 158 | + content: string |
| 159 | + ) { |
| 160 | + this._panel = panel; |
| 161 | + this.setContent(title, content) |
| 162 | + |
| 163 | + // Listen for when the panel is disposed |
| 164 | + // This happens when the user closes the panel or when the panel is closed programmatically |
| 165 | + this._panel.onDidDispose(() => this.dispose(), null, this._disposables); |
| 166 | + } |
| 167 | + |
| 168 | + public dispose() { |
| 169 | + this._panel.dispose(); |
| 170 | + |
| 171 | + while (this._disposables.length) { |
| 172 | + const x = this._disposables.pop(); |
| 173 | + if (x) { |
| 174 | + x.dispose(); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private setContent(name: string, content: string) { |
| 180 | + this._panel.title = name; |
| 181 | + this._panel.webview.html = this._getHtmlForWebview(content); |
| 182 | + } |
| 183 | + |
| 184 | + private _getHtmlForWebview(content: string) { |
| 185 | + return `<!DOCTYPE html> |
| 186 | + <html lang="en"> |
| 187 | + <head> |
| 188 | + <style> |
| 189 | + pre { |
| 190 | + font-family: Consolas, "Andale Mono WT", "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Liberation Mono", "Nimbus Mono L", Monaco, "Courier New", Courier, monospace |
| 191 | + } |
| 192 | + span.name { |
| 193 | + color:magenta; |
| 194 | + } |
| 195 | + span.tree { |
| 196 | + color:yellow; |
| 197 | + } |
| 198 | + span.length { |
| 199 | + color:cyan; |
| 200 | + } |
| 201 | + </style> |
| 202 | + <meta charset="UTF-8"> |
| 203 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 204 | + <title>Tasty Tree</title> |
| 205 | + </head> |
| 206 | + <body> |
| 207 | + <pre> |
| 208 | +${content}</pre> |
| 209 | + </body> |
| 210 | + </html>`; |
| 211 | + } |
| 212 | +} |
0 commit comments