|
| 1 | +import { Disposable, Webview, WebviewPanel, window, Uri, ViewColumn, workspace, WorkspaceEdit, Position } from "vscode"; |
| 2 | +import { LanguageClient } from "vscode-languageclient/node"; |
| 3 | +import { GetRefactorEditRequest, RefactorWorkspaceEdit } from "../protocol"; |
| 4 | +import { getNonce, getUri } from "../webview/utils"; |
| 5 | + |
| 6 | +interface MethodParameter { |
| 7 | + type: string; |
| 8 | + name: string; |
| 9 | + defaultValue: string; |
| 10 | + originalIndex: number; |
| 11 | +} |
| 12 | + |
| 13 | +interface MethodException { |
| 14 | + type: string; |
| 15 | + typeHandleIdentifier: string; |
| 16 | +} |
| 17 | + |
| 18 | +export class ChangeSignaturePanel { |
| 19 | + public static type = "java.refactor.changeSignature"; |
| 20 | + public static title = "Refactor: Change Method Signature"; |
| 21 | + public static currentPanel: ChangeSignaturePanel | undefined; |
| 22 | + private readonly panel: WebviewPanel; |
| 23 | + private disposables: Disposable[] = []; |
| 24 | + |
| 25 | + // method matadata |
| 26 | + private methodIdentifier: string | undefined; |
| 27 | + private methodName: string | undefined; |
| 28 | + private accessType: string | undefined; |
| 29 | + private returnType: string | undefined; |
| 30 | + private parameters: MethodParameter[] | undefined; |
| 31 | + private exceptions: MethodException[] | undefined; |
| 32 | + |
| 33 | + // refactor metadata |
| 34 | + private languageClient: LanguageClient; |
| 35 | + private params: any; |
| 36 | + private formattingOptions: any; |
| 37 | + private command: any; |
| 38 | + |
| 39 | + private constructor(panel: WebviewPanel, extensionUri: Uri) { |
| 40 | + this.panel = panel; |
| 41 | + this.panel.onDidDispose(() => this.dispose(), null, this.disposables); |
| 42 | + this.panel.webview.html = this.getWebviewContent(this.panel.webview, extensionUri); |
| 43 | + this.setWebviewMessageListener(this.panel.webview); |
| 44 | + } |
| 45 | + |
| 46 | + public static render(extensionUri: Uri, languageClient: LanguageClient, command: any, params: any, formattingOptions: any, commandInfo: any) { |
| 47 | + if (ChangeSignaturePanel.currentPanel) { |
| 48 | + ChangeSignaturePanel.currentPanel.panel.reveal(ViewColumn.Beside); |
| 49 | + } else { |
| 50 | + const panel = window.createWebviewPanel( |
| 51 | + ChangeSignaturePanel.type, |
| 52 | + ChangeSignaturePanel.title, |
| 53 | + ViewColumn.Beside, |
| 54 | + { |
| 55 | + enableCommandUris: true, |
| 56 | + enableScripts: true, |
| 57 | + localResourceRoots: [Uri.joinPath(extensionUri, "dist"), Uri.joinPath(extensionUri, "webview-ui/build")], |
| 58 | + } |
| 59 | + ); |
| 60 | + ChangeSignaturePanel.currentPanel = new ChangeSignaturePanel(panel, extensionUri); |
| 61 | + ChangeSignaturePanel.currentPanel.setMetadata(languageClient, command, params, formattingOptions, commandInfo); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public setMetadata(languageClient: LanguageClient, command: any, params: any, formattingOptions: any, commandInfo: any) { |
| 66 | + this.languageClient = languageClient; |
| 67 | + this.command = command; |
| 68 | + this.params = params; |
| 69 | + this.formattingOptions = formattingOptions; |
| 70 | + this.methodIdentifier = commandInfo.methodIdentifier; |
| 71 | + this.methodName = commandInfo.methodName as string; |
| 72 | + this.accessType = commandInfo.accessType as string; |
| 73 | + this.returnType = commandInfo.returnType as string; |
| 74 | + this.parameters = commandInfo.parameters as MethodParameter[]; |
| 75 | + this.exceptions = commandInfo.exceptions as MethodException[]; |
| 76 | + } |
| 77 | + |
| 78 | + public dispose() { |
| 79 | + ChangeSignaturePanel.currentPanel = undefined; |
| 80 | + this.panel.dispose(); |
| 81 | + while (this.disposables.length) { |
| 82 | + const disposable = this.disposables.pop(); |
| 83 | + if (disposable) { |
| 84 | + disposable.dispose(); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private getWebviewContent(webview: Webview, extensionUri: Uri) { |
| 90 | + const stylesUri = getUri(webview, extensionUri, [ |
| 91 | + "webview-ui", |
| 92 | + "build", |
| 93 | + "static", |
| 94 | + "css", |
| 95 | + "main.css", |
| 96 | + ]); |
| 97 | + const scriptUri = getUri(webview, extensionUri, [ |
| 98 | + "webview-ui", |
| 99 | + "build", |
| 100 | + "static", |
| 101 | + "js", |
| 102 | + "main.js", |
| 103 | + ]); |
| 104 | + |
| 105 | + const nonce = getNonce(); |
| 106 | + |
| 107 | + return /* html*/ ` |
| 108 | + <!DOCTYPE html> |
| 109 | + <html lang="en"> |
| 110 | + <head> |
| 111 | + <meta charset="utf-8"> |
| 112 | + <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"> |
| 113 | + <meta name="theme-color" content="#000000"> |
| 114 | + <link rel="stylesheet" type="text/css" href="${stylesUri}"> |
| 115 | + <title>Change Signature</title> |
| 116 | + </head> |
| 117 | + <body> |
| 118 | + <div id="root"></div> |
| 119 | + <script nonce="${nonce}" src="${scriptUri}"></script> |
| 120 | + </body> |
| 121 | + </html> |
| 122 | + `; |
| 123 | + } |
| 124 | + |
| 125 | + private setWebviewMessageListener(webview: Webview) { |
| 126 | + webview.onDidReceiveMessage( |
| 127 | + async (message: any) => { |
| 128 | + const command = message.command; |
| 129 | + switch (command) { |
| 130 | + case "webviewReady": |
| 131 | + await this.panel.webview.postMessage({ |
| 132 | + command: "setInitialState", |
| 133 | + methodIdentifier: this.methodIdentifier, |
| 134 | + methodName: this.methodName, |
| 135 | + accessType: this.accessType, |
| 136 | + returnType: this.returnType, |
| 137 | + parameters: this.parameters, |
| 138 | + exceptions: this.exceptions |
| 139 | + }); |
| 140 | + break; |
| 141 | + case "doRefactor": |
| 142 | + await this.doRefactor(message.methodIdentifier, message.isDelegate, message.methodName, message.accessType, message.returnType, message.parameters, message.exceptions); |
| 143 | + this.dispose(); |
| 144 | + break; |
| 145 | + } |
| 146 | + }, |
| 147 | + undefined, |
| 148 | + this.disposables |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + private async doRefactor(methodIdentifier: string, isDelegate: boolean, methodName: string, accessType: string, returnType: string, parameters: MethodParameter[], exceptions: MethodException[]) { |
| 153 | + const clientWorkspaceEdit: RefactorWorkspaceEdit = await this.languageClient.sendRequest(GetRefactorEditRequest.type, { |
| 154 | + command: this.command, |
| 155 | + context: this.params, |
| 156 | + options: this.formattingOptions, |
| 157 | + commandArguments: [methodIdentifier, isDelegate, methodName, accessType, returnType, parameters, exceptions] |
| 158 | + }); |
| 159 | + if (!clientWorkspaceEdit) { |
| 160 | + return; |
| 161 | + } |
| 162 | + if (clientWorkspaceEdit.edit) { |
| 163 | + const codeEdit: WorkspaceEdit = await this.languageClient.protocol2CodeConverter.asWorkspaceEdit(clientWorkspaceEdit.edit); |
| 164 | + |
| 165 | + /** |
| 166 | + * See the issue https://github.com/microsoft/vscode/issues/94650. |
| 167 | + * The current vscode doesn't provide a way for the extension to pre-select all changes. |
| 168 | + * |
| 169 | + * As a workaround, this extension would append a dummy text edit that needs a confirm, |
| 170 | + * and then make all others text edits not need a confirm. This will ensure that |
| 171 | + * the REFACTOR PREVIEW panel can be triggered and all valid changes pre-selected. |
| 172 | + */ |
| 173 | + const textEditEntries = codeEdit.entries(); |
| 174 | + if (textEditEntries && textEditEntries.length) { |
| 175 | + const dummyNodeUri: Uri = textEditEntries[textEditEntries.length - 1][0]; |
| 176 | + codeEdit.insert(dummyNodeUri, new Position(0, 0), "", { |
| 177 | + needsConfirmation: true, |
| 178 | + label: "Dummy node used to enable preview" |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + if (codeEdit) { |
| 183 | + await workspace.applyEdit(codeEdit); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments