|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | import * as path from 'path'; |
4 | | -import { workspace, FileCreateEvent, ExtensionContext, window, TextDocument, SnippetString, commands, Uri, FileRenameEvent, ProgressLocation } from 'vscode'; |
5 | | -import { LanguageClient } from 'vscode-languageclient'; |
| 4 | +import { workspace, FileCreateEvent, ExtensionContext, window, TextDocument, SnippetString, commands, Uri, FileRenameEvent, ProgressLocation, WorkspaceEdit as CodeWorkspaceEdit } from 'vscode'; |
| 5 | +import { LanguageClient, WorkspaceEdit as LsWorkspaceEdit, CreateFile, RenameFile, DeleteFile, TextDocumentEdit } from 'vscode-languageclient'; |
6 | 6 | import { ListCommandResult } from './buildpath'; |
7 | 7 | import { Commands } from './commands'; |
8 | 8 | import { DidRenameFiles } from './protocol'; |
| 9 | +import { Converter as ProtocolConverter } from 'vscode-languageclient/lib/protocolConverter'; |
9 | 10 |
|
10 | 11 | let serverReady: boolean = false; |
11 | 12 |
|
@@ -98,7 +99,8 @@ async function handleRenameFiles(e: FileRenameEvent, client: LanguageClient) { |
98 | 99 | const edit = await client.sendRequest(DidRenameFiles.type, { |
99 | 100 | files: javaRenameEvents |
100 | 101 | }); |
101 | | - const codeEdit = client.protocol2CodeConverter.asWorkspaceEdit(edit); |
| 102 | + |
| 103 | + const codeEdit = asPreviewWorkspaceEdit(edit, client.protocol2CodeConverter, "Rename updates"); |
102 | 104 | if (codeEdit) { |
103 | 105 | workspace.applyEdit(codeEdit); |
104 | 106 | } |
@@ -173,3 +175,58 @@ async function isVersionLessThan(fileUri: string, targetVersion: number): Promis |
173 | 175 |
|
174 | 176 | return javaVersion < targetVersion; |
175 | 177 | } |
| 178 | + |
| 179 | +/** |
| 180 | + * This function reference the implementation of asWorkspaceEdit() from 'vscode-languageclient/lib/protocolConverter'. |
| 181 | + */ |
| 182 | +function asPreviewWorkspaceEdit(item: LsWorkspaceEdit, converter: ProtocolConverter, label: string) { |
| 183 | + if (!item) { |
| 184 | + return undefined; |
| 185 | + } |
| 186 | + |
| 187 | + const result = new CodeWorkspaceEdit(); |
| 188 | + if (item.documentChanges) { |
| 189 | + item.documentChanges.forEach(change => { |
| 190 | + if (CreateFile.is(change)) { |
| 191 | + result.createFile(converter.asUri(change.uri), change.options, { |
| 192 | + needsConfirmation: true, |
| 193 | + label, |
| 194 | + }); |
| 195 | + } else if (RenameFile.is(change)) { |
| 196 | + result.renameFile(converter.asUri(change.oldUri), converter.asUri(change.newUri), change.options, { |
| 197 | + needsConfirmation: true, |
| 198 | + label, |
| 199 | + }); |
| 200 | + } else if (DeleteFile.is(change)) { |
| 201 | + result.deleteFile(converter.asUri(change.uri), change.options, { |
| 202 | + needsConfirmation: true, |
| 203 | + label, |
| 204 | + }); |
| 205 | + } else if (TextDocumentEdit.is(change)) { |
| 206 | + if (change.edits) { |
| 207 | + change.edits.forEach(edit => { |
| 208 | + result.replace(converter.asUri(change.textDocument.uri), converter.asRange(edit.range), edit.newText, { |
| 209 | + needsConfirmation: true, |
| 210 | + label, |
| 211 | + }); |
| 212 | + }); |
| 213 | + } |
| 214 | + } else { |
| 215 | + console.error(`Unknown workspace edit change received:\n${JSON.stringify(change, undefined, 4)}`); |
| 216 | + } |
| 217 | + }); |
| 218 | + } else if (item.changes) { |
| 219 | + Object.keys(item.changes).forEach(key => { |
| 220 | + if (item.changes[key]) { |
| 221 | + item.changes[key].forEach(edit => { |
| 222 | + result.replace(converter.asUri(key), converter.asRange(edit.range), edit.newText, { |
| 223 | + needsConfirmation: true, |
| 224 | + label, |
| 225 | + }); |
| 226 | + }); |
| 227 | + } |
| 228 | + }); |
| 229 | + } |
| 230 | + |
| 231 | + return result; |
| 232 | +} |
0 commit comments