Skip to content

Commit

Permalink
Enable to move type to new file (#1041)
Browse files Browse the repository at this point in the history
Signed-off-by: Jinbo Wang <jinbwan@microsoft.com>
  • Loading branch information
testforstephen authored and fbricon committed Sep 2, 2019
1 parent 77b61ab commit b26485a
Showing 1 changed file with 77 additions and 9 deletions.
86 changes: 77 additions & 9 deletions src/refactorAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { existsSync } from 'fs';
import * as path from 'path';
import { commands, ExtensionContext, Position, TextDocument, Uri, window, workspace } from 'vscode';
import { FormattingOptions, LanguageClient, WorkspaceEdit, CreateFile, RenameFile, DeleteFile, TextDocumentEdit, CodeActionParams } from 'vscode-languageclient';
import { FormattingOptions, LanguageClient, WorkspaceEdit, CreateFile, RenameFile, DeleteFile, TextDocumentEdit, CodeActionParams, SymbolInformation } from 'vscode-languageclient';
import { Commands as javaCommands } from './commands';
import { GetRefactorEditRequest, MoveRequest, RefactorWorkspaceEdit, RenamePosition, GetMoveDestinationsRequest, SearchSymbols } from './protocol';

Expand Down Expand Up @@ -86,6 +86,8 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E
await moveInstanceMethod(languageClient, params, commandInfo);
} else if (command === 'moveStaticMember') {
await moveStaticMember(languageClient, params, commandInfo);
} else if (command === 'moveType') {
await moveType(languageClient, params, commandInfo);
}
}));
}
Expand Down Expand Up @@ -124,7 +126,8 @@ async function moveFile(languageClient: LanguageClient, fileUris: Uri[]) {

const moveDestinations = await languageClient.sendRequest(GetMoveDestinationsRequest.type, {
moveKind: 'moveResource',
sourceUris: fileUris.map(uri => uri.toString())
sourceUris: fileUris.map(uri => uri.toString()),
params: null,
});
if (!moveDestinations || !moveDestinations.destinations || !moveDestinations.destinations.length) {
window.showErrorMessage("Cannot find available Java packages to move the selected files to.");
Expand Down Expand Up @@ -300,10 +303,25 @@ async function moveStaticMember(languageClient: LanguageClient, params: CodeActi
exclude.add(`${commandInfo.enclosingTypeName}.${commandInfo.displayName}`);
}
}

const projectName = commandInfo ? commandInfo.projectName : null;
const picked = await selectTargetClass(languageClient, `Select the new class for the static member ${memberName}.`, projectName, exclude);
if (picked) {
const refactorEdit: RefactorWorkspaceEdit = await languageClient.sendRequest(MoveRequest.type, {
moveKind: 'moveStaticMember',
sourceUris: [ params.textDocument.uri ],
params,
destination: picked,
});
await applyRefactorEdit(languageClient, refactorEdit);
}
}

async function selectTargetClass(languageClient: LanguageClient, placeHolder: string, projectName: string, exclude: Set<string>): Promise<SymbolInformation> {
const picked = await window.showQuickPick(
languageClient.sendRequest(SearchSymbols.type, {
query: '*',
projectName: commandInfo ? commandInfo.projectName : null,
projectName,
sourceOnly: true,
}).then(types => {
if (types && types.length) {
Expand Down Expand Up @@ -333,16 +351,66 @@ async function moveStaticMember(languageClient: LanguageClient, params: CodeActi
}];
}
}), {
placeHolder: `Select the new class for the static member ${memberName}.`
placeHolder,
});

if (picked && picked.symbolNode) {
const refactorEdit: RefactorWorkspaceEdit = await languageClient.sendRequest(MoveRequest.type, {
moveKind: 'moveStaticMember',
return picked ? picked.symbolNode : null;
}

async function moveType(languageClient: LanguageClient, params: CodeActionParams, commandInfo: any) {
if (!commandInfo || !commandInfo.supportedDestinationKinds) {
return;
}

const destinationPickItems: any[] = commandInfo.supportedDestinationKinds.map((kind) => {
if (kind === 'newFile') {
return {
label: `Move type ${commandInfo.displayName} to new file`,
kind,
};
} else {
return {
label: `Move type ${commandInfo.displayName} to another class`,
kind,
};
}
});

if (!destinationPickItems.length) {
return;
}

const picked = await window.showQuickPick(destinationPickItems, {
placeHolder: 'What would you like to do?',
});
if (!picked) {
return;
}

let refactorEdit: RefactorWorkspaceEdit;
if (picked.kind === 'newFile') {
refactorEdit = await languageClient.sendRequest(MoveRequest.type, {
moveKind: 'moveTypeToNewFile',
sourceUris: [ params.textDocument.uri ],
params,
destination: picked.symbolNode,
});
await applyRefactorEdit(languageClient, refactorEdit);
} else {
const exclude: Set<string> = new Set();
if (commandInfo.enclosingTypeName) {
exclude.add(commandInfo.enclosingTypeName);
exclude.add(`${commandInfo.enclosingTypeName}.${commandInfo.displayName}`);
}

const picked = await selectTargetClass(languageClient, `Select the new class for the type ${commandInfo.displayName}.`, commandInfo.projectName, exclude);
if (picked) {
refactorEdit = await languageClient.sendRequest(MoveRequest.type, {
moveKind: 'moveTypeToClass',
sourceUris: [ params.textDocument.uri ],
params,
destination: picked,
});
}
}

await applyRefactorEdit(languageClient, refactorEdit);
}

0 comments on commit b26485a

Please sign in to comment.