Skip to content

Commit b26485a

Browse files
testforstephenfbricon
authored andcommitted
Enable to move type to new file (#1041)
Signed-off-by: Jinbo Wang <[email protected]>
1 parent 77b61ab commit b26485a

File tree

1 file changed

+77
-9
lines changed

1 file changed

+77
-9
lines changed

src/refactorAction.ts

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { existsSync } from 'fs';
44
import * as path from 'path';
55
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';
77
import { Commands as javaCommands } from './commands';
88
import { GetRefactorEditRequest, MoveRequest, RefactorWorkspaceEdit, RenamePosition, GetMoveDestinationsRequest, SearchSymbols } from './protocol';
99

@@ -86,6 +86,8 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E
8686
await moveInstanceMethod(languageClient, params, commandInfo);
8787
} else if (command === 'moveStaticMember') {
8888
await moveStaticMember(languageClient, params, commandInfo);
89+
} else if (command === 'moveType') {
90+
await moveType(languageClient, params, commandInfo);
8991
}
9092
}));
9193
}
@@ -124,7 +126,8 @@ async function moveFile(languageClient: LanguageClient, fileUris: Uri[]) {
124126

125127
const moveDestinations = await languageClient.sendRequest(GetMoveDestinationsRequest.type, {
126128
moveKind: 'moveResource',
127-
sourceUris: fileUris.map(uri => uri.toString())
129+
sourceUris: fileUris.map(uri => uri.toString()),
130+
params: null,
128131
});
129132
if (!moveDestinations || !moveDestinations.destinations || !moveDestinations.destinations.length) {
130133
window.showErrorMessage("Cannot find available Java packages to move the selected files to.");
@@ -300,10 +303,25 @@ async function moveStaticMember(languageClient: LanguageClient, params: CodeActi
300303
exclude.add(`${commandInfo.enclosingTypeName}.${commandInfo.displayName}`);
301304
}
302305
}
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> {
303321
const picked = await window.showQuickPick(
304322
languageClient.sendRequest(SearchSymbols.type, {
305323
query: '*',
306-
projectName: commandInfo ? commandInfo.projectName : null,
324+
projectName,
307325
sourceOnly: true,
308326
}).then(types => {
309327
if (types && types.length) {
@@ -333,16 +351,66 @@ async function moveStaticMember(languageClient: LanguageClient, params: CodeActi
333351
}];
334352
}
335353
}), {
336-
placeHolder: `Select the new class for the static member ${memberName}.`
354+
placeHolder,
337355
});
338356

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',
342394
sourceUris: [ params.textDocument.uri ],
343395
params,
344-
destination: picked.symbolNode,
345396
});
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+
}
347413
}
414+
415+
await applyRefactorEdit(languageClient, refactorEdit);
348416
}

0 commit comments

Comments
 (0)