mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 21:07:27 -05:00
'Move to file' refactor (#53542)
This commit is contained in:
@@ -4,6 +4,7 @@ import type {
|
||||
EndOfLineState,
|
||||
FileExtensionInfo,
|
||||
HighlightSpanKind,
|
||||
InteractiveRefactorArguments,
|
||||
MapLike,
|
||||
OutliningSpanKind,
|
||||
OutputFile,
|
||||
@@ -142,6 +143,7 @@ export const enum CommandTypes {
|
||||
|
||||
GetApplicableRefactors = "getApplicableRefactors",
|
||||
GetEditsForRefactor = "getEditsForRefactor",
|
||||
GetMoveToRefactoringFileSuggestions = "getMoveToRefactoringFileSuggestions",
|
||||
/** @internal */
|
||||
GetEditsForRefactorFull = "getEditsForRefactor-full",
|
||||
|
||||
@@ -606,6 +608,27 @@ export interface GetApplicableRefactorsResponse extends Response {
|
||||
body?: ApplicableRefactorInfo[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Request refactorings at a given position or selection area to move to an existing file.
|
||||
*/
|
||||
export interface GetMoveToRefactoringFileSuggestionsRequest extends Request {
|
||||
command: CommandTypes.GetMoveToRefactoringFileSuggestions;
|
||||
arguments: GetMoveToRefactoringFileSuggestionsRequestArgs;
|
||||
}
|
||||
export type GetMoveToRefactoringFileSuggestionsRequestArgs = FileLocationOrRangeRequestArgs & {
|
||||
kind?: string;
|
||||
};
|
||||
/**
|
||||
* Response is a list of available files.
|
||||
* Each refactoring exposes one or more "Actions"; a user selects one action to invoke a refactoring
|
||||
*/
|
||||
export interface GetMoveToRefactoringFileSuggestions extends Response {
|
||||
body: {
|
||||
newFileName: string;
|
||||
files: string[];
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A set of one or more available refactoring actions, grouped under a parent refactoring.
|
||||
*/
|
||||
@@ -680,6 +703,8 @@ export type GetEditsForRefactorRequestArgs = FileLocationOrRangeRequestArgs & {
|
||||
refactor: string;
|
||||
/* The 'name' property from the refactoring action */
|
||||
action: string;
|
||||
/* Arguments for interactive action */
|
||||
interactiveRefactorArguments?: InteractiveRefactorArguments;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -881,6 +881,7 @@ const invalidPartialSemanticModeCommands: readonly protocol.CommandTypes[] = [
|
||||
protocol.CommandTypes.ApplyCodeActionCommand,
|
||||
protocol.CommandTypes.GetSupportedCodeFixes,
|
||||
protocol.CommandTypes.GetApplicableRefactors,
|
||||
protocol.CommandTypes.GetMoveToRefactoringFileSuggestions,
|
||||
protocol.CommandTypes.GetEditsForRefactor,
|
||||
protocol.CommandTypes.GetEditsForRefactorFull,
|
||||
protocol.CommandTypes.OrganizeImports,
|
||||
@@ -2687,6 +2688,7 @@ export class Session<TMessage = string> implements EventSender {
|
||||
args.refactor,
|
||||
args.action,
|
||||
this.getPreferences(file),
|
||||
args.interactiveRefactorArguments
|
||||
);
|
||||
|
||||
if (result === undefined) {
|
||||
@@ -2702,11 +2704,19 @@ export class Session<TMessage = string> implements EventSender {
|
||||
const renameScriptInfo = project.getScriptInfoForNormalizedPath(toNormalizedPath(renameFilename))!;
|
||||
mappedRenameLocation = getLocationInNewDocument(getSnapshotText(renameScriptInfo.getSnapshot()), renameFilename, renameLocation, edits);
|
||||
}
|
||||
return { renameLocation: mappedRenameLocation, renameFilename, edits: this.mapTextChangesToCodeEdits(edits) };
|
||||
}
|
||||
else {
|
||||
return result;
|
||||
return {
|
||||
renameLocation: mappedRenameLocation,
|
||||
renameFilename,
|
||||
edits: this.mapTextChangesToCodeEdits(edits)
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private getMoveToRefactoringFileSuggestions(args: protocol.GetMoveToRefactoringFileSuggestionsRequestArgs): { newFileName: string, files: string[] }{
|
||||
const { file, project } = this.getFileAndProject(args);
|
||||
const scriptInfo = project.getScriptInfoForNormalizedPath(file)!;
|
||||
return project.getLanguageService().getMoveToRefactoringFileSuggestions(file, this.extractPositionOrRange(args, scriptInfo), this.getPreferences(file));
|
||||
}
|
||||
|
||||
private organizeImports(args: protocol.OrganizeImportsRequestArgs, simplifiedResult: boolean): readonly protocol.FileCodeEdits[] | readonly FileTextChanges[] {
|
||||
@@ -3429,6 +3439,9 @@ export class Session<TMessage = string> implements EventSender {
|
||||
[protocol.CommandTypes.GetEditsForRefactor]: (request: protocol.GetEditsForRefactorRequest) => {
|
||||
return this.requiredResponse(this.getEditsForRefactor(request.arguments, /*simplifiedResult*/ true));
|
||||
},
|
||||
[protocol.CommandTypes.GetMoveToRefactoringFileSuggestions]: (request: protocol.GetMoveToRefactoringFileSuggestionsRequest) => {
|
||||
return this.requiredResponse(this.getMoveToRefactoringFileSuggestions(request.arguments));
|
||||
},
|
||||
[protocol.CommandTypes.GetEditsForRefactorFull]: (request: protocol.GetEditsForRefactorRequest) => {
|
||||
return this.requiredResponse(this.getEditsForRefactor(request.arguments, /*simplifiedResult*/ false));
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user