Skip to content

Interactive refactor actions #53915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,14 @@ export interface GetApplicableRefactorsRequest extends Request {
export type GetApplicableRefactorsRequestArgs = FileLocationOrRangeRequestArgs & {
triggerReason?: RefactorTriggerReason;
kind?: string;
/**
* Include refactor actions that require additional arguments to be passed when
* calling 'GetEditsForRefactor'. When true, clients should inspect the
* `isInteractive` property of each returned `RefactorActionInfo`
* and ensure they are able to collect the appropriate arguments for any
* interactive refactor before offering it.
*/
includeInteractiveActions?: boolean;
};

export type RefactorTriggerReason = "implicit" | "invoked";
Expand Down Expand Up @@ -650,6 +658,12 @@ export interface RefactorActionInfo {
* The hierarchical dotted name of the refactor action.
*/
kind?: string;

/**
* Indicates that the action requires additional arguments to be passed
* when calling 'GetEditsForRefactor'.
*/
isInteractive?: boolean;
}

export interface GetEditsForRefactorRequest extends Request {
Expand Down
4 changes: 2 additions & 2 deletions src/services/refactorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export function registerRefactor(name: string, refactor: Refactor) {
}

/** @internal */
export function getApplicableRefactors(context: RefactorContext): ApplicableRefactorInfo[] {
export function getApplicableRefactors(context: RefactorContext, includeInteractiveActions?: boolean): ApplicableRefactorInfo[] {
return arrayFrom(flatMapIterator(refactors.values(), refactor =>
context.cancellationToken && context.cancellationToken.isCancellationRequested() ||
!refactor.kinds?.some(kind => refactorKindBeginsWith(kind, context.kind)) ? undefined :
refactor.getAvailableActions(context)));
refactor.getAvailableActions(context, includeInteractiveActions)));
}

/** @internal */
Expand Down
4 changes: 2 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2971,10 +2971,10 @@ export function createLanguageService(
return SmartSelectionRange.getSmartSelectionRange(position, syntaxTreeCache.getCurrentSourceFile(fileName));
}

function getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences = emptyOptions, triggerReason: RefactorTriggerReason, kind: string): ApplicableRefactorInfo[] {
function getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences = emptyOptions, triggerReason: RefactorTriggerReason, kind: string, includeInteractiveActions?: boolean): ApplicableRefactorInfo[] {
synchronizeHostData();
const file = getValidSourceFile(fileName);
return refactor.getApplicableRefactors(getRefactorContext(file, positionOrRange, preferences, emptyOptions, triggerReason, kind));
return refactor.getApplicableRefactors(getRefactorContext(file, positionOrRange, preferences, emptyOptions, triggerReason, kind), includeInteractiveActions);
}

function getEditsForRefactor(
Expand Down
16 changes: 14 additions & 2 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,13 @@ export interface LanguageService {
/** @deprecated `fileName` will be ignored */
applyCodeActionCommand(fileName: string, action: CodeActionCommand | CodeActionCommand[]): Promise<ApplyCodeActionCommandResult | ApplyCodeActionCommandResult[]>;

getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string): ApplicableRefactorInfo[];
/**
* @param includeInteractiveActions Include refactor actions that require additional arguments to be
* passed when calling `getEditsForRefactor`. When true, clients should inspect the `isInteractive`
* property of each returned `RefactorActionInfo` and ensure they are able to collect the appropriate
* arguments for any interactive action before offering it.
*/
getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string, includeInteractiveActions?: boolean): ApplicableRefactorInfo[];
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined): RefactorEditInfo | undefined;
organizeImports(args: OrganizeImportsArgs, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
getEditsForFileRename(oldFilePath: string, newFilePath: string, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
Expand Down Expand Up @@ -963,6 +969,12 @@ export interface RefactorActionInfo {
* The hierarchical dotted name of the refactor action.
*/
kind?: string;

/**
* Indicates that the action requires additional arguments to be passed
* when calling `getEditsForRefactor`.
*/
isInteractive?: boolean;
}

/**
Expand Down Expand Up @@ -1761,7 +1773,7 @@ export interface Refactor {
getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined;

/** Compute (quickly) which actions are available here */
getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[];
getAvailableActions(context: RefactorContext, includeInteractive?: boolean): readonly ApplicableRefactorInfo[];
}

/** @internal */
Expand Down
26 changes: 25 additions & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,14 @@ declare namespace ts {
type GetApplicableRefactorsRequestArgs = FileLocationOrRangeRequestArgs & {
triggerReason?: RefactorTriggerReason;
kind?: string;
/**
* Include refactor actions that require additional arguments to be passed when
* calling 'GetEditsForRefactor'. When true, clients should inspect the
* `isInteractive` property of each returned `RefactorActionInfo`
* and ensure they are able to collect the appropriate arguments for any
* interactive refactor before offering it.
*/
includeInteractiveActions?: boolean;
};
type RefactorTriggerReason = "implicit" | "invoked";
/**
Expand Down Expand Up @@ -557,6 +565,11 @@ declare namespace ts {
* The hierarchical dotted name of the refactor action.
*/
kind?: string;
/**
* Indicates that the action requires additional arguments to be passed
* when calling 'GetEditsForRefactor'.
*/
isInteractive?: boolean;
}
interface GetEditsForRefactorRequest extends Request {
command: CommandTypes.GetEditsForRefactor;
Expand Down Expand Up @@ -10129,7 +10142,13 @@ declare namespace ts {
applyCodeActionCommand(fileName: string, action: CodeActionCommand[]): Promise<ApplyCodeActionCommandResult[]>;
/** @deprecated `fileName` will be ignored */
applyCodeActionCommand(fileName: string, action: CodeActionCommand | CodeActionCommand[]): Promise<ApplyCodeActionCommandResult | ApplyCodeActionCommandResult[]>;
getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string): ApplicableRefactorInfo[];
/**
* @param includeInteractiveActions Include refactor actions that require additional arguments to be
* passed when calling `getEditsForRefactor`. When true, clients should inspect the `isInteractive`
* property of each returned `RefactorActionInfo` and ensure they are able to collect the appropriate
* arguments for any interactive action before offering it.
*/
getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string, includeInteractiveActions?: boolean): ApplicableRefactorInfo[];
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined): RefactorEditInfo | undefined;
organizeImports(args: OrganizeImportsArgs, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
getEditsForFileRename(oldFilePath: string, newFilePath: string, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
Expand Down Expand Up @@ -10401,6 +10420,11 @@ declare namespace ts {
* The hierarchical dotted name of the refactor action.
*/
kind?: string;
/**
* Indicates that the action requires additional arguments to be passed
* when calling `getEditsForRefactor`.
*/
isInteractive?: boolean;
}
/**
* A set of edits to make in response to a refactor action, plus an optional
Expand Down
13 changes: 12 additions & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6198,7 +6198,13 @@ declare namespace ts {
applyCodeActionCommand(fileName: string, action: CodeActionCommand[]): Promise<ApplyCodeActionCommandResult[]>;
/** @deprecated `fileName` will be ignored */
applyCodeActionCommand(fileName: string, action: CodeActionCommand | CodeActionCommand[]): Promise<ApplyCodeActionCommandResult | ApplyCodeActionCommandResult[]>;
getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string): ApplicableRefactorInfo[];
/**
* @param includeInteractiveActions Include refactor actions that require additional arguments to be
* passed when calling `getEditsForRefactor`. When true, clients should inspect the `isInteractive`
* property of each returned `RefactorActionInfo` and ensure they are able to collect the appropriate
* arguments for any interactive action before offering it.
*/
getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string, includeInteractiveActions?: boolean): ApplicableRefactorInfo[];
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined): RefactorEditInfo | undefined;
organizeImports(args: OrganizeImportsArgs, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
getEditsForFileRename(oldFilePath: string, newFilePath: string, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
Expand Down Expand Up @@ -6470,6 +6476,11 @@ declare namespace ts {
* The hierarchical dotted name of the refactor action.
*/
kind?: string;
/**
* Indicates that the action requires additional arguments to be passed
* when calling `getEditsForRefactor`.
*/
isInteractive?: boolean;
}
/**
* A set of edits to make in response to a refactor action, plus an optional
Expand Down