|
3 | 3 | import { existsSync } from 'fs'; |
4 | 4 | import * as path from 'path'; |
5 | 5 | import { commands, ExtensionContext, Position, TextDocument, Uri, window, workspace } from 'vscode'; |
6 | | -import { FormattingOptions, LanguageClient, WorkspaceEdit, CreateFile, RenameFile, DeleteFile, TextDocumentEdit, CodeActionParams } from 'vscode-languageclient'; |
| 6 | +import { FormattingOptions, LanguageClient, WorkspaceEdit, CreateFile, RenameFile, DeleteFile, TextDocumentEdit, CodeActionParams, SymbolInformation } from 'vscode-languageclient'; |
7 | 7 | import { Commands as javaCommands } from './commands'; |
8 | 8 | import { GetRefactorEditRequest, MoveRequest, RefactorWorkspaceEdit, RenamePosition, GetMoveDestinationsRequest, SearchSymbols } from './protocol'; |
9 | 9 |
|
@@ -86,6 +86,8 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E |
86 | 86 | await moveInstanceMethod(languageClient, params, commandInfo); |
87 | 87 | } else if (command === 'moveStaticMember') { |
88 | 88 | await moveStaticMember(languageClient, params, commandInfo); |
| 89 | + } else if (command === 'moveType') { |
| 90 | + await moveType(languageClient, params, commandInfo); |
89 | 91 | } |
90 | 92 | })); |
91 | 93 | } |
@@ -124,7 +126,8 @@ async function moveFile(languageClient: LanguageClient, fileUris: Uri[]) { |
124 | 126 |
|
125 | 127 | const moveDestinations = await languageClient.sendRequest(GetMoveDestinationsRequest.type, { |
126 | 128 | moveKind: 'moveResource', |
127 | | - sourceUris: fileUris.map(uri => uri.toString()) |
| 129 | + sourceUris: fileUris.map(uri => uri.toString()), |
| 130 | + params: null, |
128 | 131 | }); |
129 | 132 | if (!moveDestinations || !moveDestinations.destinations || !moveDestinations.destinations.length) { |
130 | 133 | window.showErrorMessage("Cannot find available Java packages to move the selected files to."); |
@@ -300,10 +303,25 @@ async function moveStaticMember(languageClient: LanguageClient, params: CodeActi |
300 | 303 | exclude.add(`${commandInfo.enclosingTypeName}.${commandInfo.displayName}`); |
301 | 304 | } |
302 | 305 | } |
| 306 | + |
| 307 | + const projectName = commandInfo ? commandInfo.projectName : null; |
| 308 | + const picked = await selectTargetClass(languageClient, `Select the new class for the static member ${memberName}.`, projectName, exclude); |
| 309 | + if (picked) { |
| 310 | + const refactorEdit: RefactorWorkspaceEdit = await languageClient.sendRequest(MoveRequest.type, { |
| 311 | + moveKind: 'moveStaticMember', |
| 312 | + sourceUris: [ params.textDocument.uri ], |
| 313 | + params, |
| 314 | + destination: picked, |
| 315 | + }); |
| 316 | + await applyRefactorEdit(languageClient, refactorEdit); |
| 317 | + } |
| 318 | +} |
| 319 | + |
| 320 | +async function selectTargetClass(languageClient: LanguageClient, placeHolder: string, projectName: string, exclude: Set<string>): Promise<SymbolInformation> { |
303 | 321 | const picked = await window.showQuickPick( |
304 | 322 | languageClient.sendRequest(SearchSymbols.type, { |
305 | 323 | query: '*', |
306 | | - projectName: commandInfo ? commandInfo.projectName : null, |
| 324 | + projectName, |
307 | 325 | sourceOnly: true, |
308 | 326 | }).then(types => { |
309 | 327 | if (types && types.length) { |
@@ -333,16 +351,66 @@ async function moveStaticMember(languageClient: LanguageClient, params: CodeActi |
333 | 351 | }]; |
334 | 352 | } |
335 | 353 | }), { |
336 | | - placeHolder: `Select the new class for the static member ${memberName}.` |
| 354 | + placeHolder, |
337 | 355 | }); |
338 | 356 |
|
339 | | - if (picked && picked.symbolNode) { |
340 | | - const refactorEdit: RefactorWorkspaceEdit = await languageClient.sendRequest(MoveRequest.type, { |
341 | | - moveKind: 'moveStaticMember', |
| 357 | + return picked ? picked.symbolNode : null; |
| 358 | +} |
| 359 | + |
| 360 | +async function moveType(languageClient: LanguageClient, params: CodeActionParams, commandInfo: any) { |
| 361 | + if (!commandInfo || !commandInfo.supportedDestinationKinds) { |
| 362 | + return; |
| 363 | + } |
| 364 | + |
| 365 | + const destinationPickItems: any[] = commandInfo.supportedDestinationKinds.map((kind) => { |
| 366 | + if (kind === 'newFile') { |
| 367 | + return { |
| 368 | + label: `Move type ${commandInfo.displayName} to new file`, |
| 369 | + kind, |
| 370 | + }; |
| 371 | + } else { |
| 372 | + return { |
| 373 | + label: `Move type ${commandInfo.displayName} to another class`, |
| 374 | + kind, |
| 375 | + }; |
| 376 | + } |
| 377 | + }); |
| 378 | + |
| 379 | + if (!destinationPickItems.length) { |
| 380 | + return; |
| 381 | + } |
| 382 | + |
| 383 | + const picked = await window.showQuickPick(destinationPickItems, { |
| 384 | + placeHolder: 'What would you like to do?', |
| 385 | + }); |
| 386 | + if (!picked) { |
| 387 | + return; |
| 388 | + } |
| 389 | + |
| 390 | + let refactorEdit: RefactorWorkspaceEdit; |
| 391 | + if (picked.kind === 'newFile') { |
| 392 | + refactorEdit = await languageClient.sendRequest(MoveRequest.type, { |
| 393 | + moveKind: 'moveTypeToNewFile', |
342 | 394 | sourceUris: [ params.textDocument.uri ], |
343 | 395 | params, |
344 | | - destination: picked.symbolNode, |
345 | 396 | }); |
346 | | - await applyRefactorEdit(languageClient, refactorEdit); |
| 397 | + } else { |
| 398 | + const exclude: Set<string> = new Set(); |
| 399 | + if (commandInfo.enclosingTypeName) { |
| 400 | + exclude.add(commandInfo.enclosingTypeName); |
| 401 | + exclude.add(`${commandInfo.enclosingTypeName}.${commandInfo.displayName}`); |
| 402 | + } |
| 403 | + |
| 404 | + const picked = await selectTargetClass(languageClient, `Select the new class for the type ${commandInfo.displayName}.`, commandInfo.projectName, exclude); |
| 405 | + if (picked) { |
| 406 | + refactorEdit = await languageClient.sendRequest(MoveRequest.type, { |
| 407 | + moveKind: 'moveTypeToClass', |
| 408 | + sourceUris: [ params.textDocument.uri ], |
| 409 | + params, |
| 410 | + destination: picked, |
| 411 | + }); |
| 412 | + } |
347 | 413 | } |
| 414 | + |
| 415 | + await applyRefactorEdit(languageClient, refactorEdit); |
348 | 416 | } |
0 commit comments