Add CodeActionTriggerKind api proposal

For #118084
Fixes #118087
This commit is contained in:
Matt Bierner 2021-03-03 17:35:40 -08:00
parent 20f06e0444
commit 49d05f998f
7 changed files with 52 additions and 10 deletions

View file

@ -223,7 +223,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
_range: vscode.Range,
context: vscode.CodeActionContext,
token: vscode.CancellationToken
): Promise<vscode.CodeAction[]> {
): Promise<VsCodeCodeAction[]> {
const file = this.client.toOpenedFilePath(document);
if (!file) {
return [];

View file

@ -284,7 +284,7 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
context: vscode.CodeActionContext,
token: vscode.CancellationToken
): Promise<TsCodeAction[] | undefined> {
if (!this.shouldTrigger(rangeOrSelection, context)) {
if (!this.shouldTrigger(context)) {
return undefined;
}
if (!this.client.toOpenedFilePath(document)) {
@ -335,10 +335,10 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
}
private toTsTriggerReason(context: vscode.CodeActionContext): Proto.RefactorTriggerReason | undefined {
if (!context.only) {
return;
if (context.triggerKind === vscode.CodeActionTriggerKind.Manual) {
return 'invoked';
}
return 'invoked';
return undefined;
}
private convertApplicableRefactors(
@ -384,12 +384,12 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
return codeAction;
}
private shouldTrigger(rangeOrSelection: vscode.Range | vscode.Selection, context: vscode.CodeActionContext) {
private shouldTrigger(context: vscode.CodeActionContext) {
if (context.only && !vscode.CodeActionKind.Refactor.contains(context.only)) {
return false;
}
return rangeOrSelection instanceof vscode.Selection;
return context.triggerKind === vscode.CodeActionTriggerKind.Manual;
}
private static getKind(refactor: Proto.RefactorActionInfo) {

View file

@ -2800,4 +2800,32 @@ declare module 'vscode' {
}
//#endregion
//#region https://github.com/microsoft/vscode/issues/118084
/**
* The reason why code actions were requested.
*/
export enum CodeActionTriggerKind {
/**
* Code actions were requested automatically.
*
* This typically happens when current selection in a file changes, but can
* also be triggered when file content changes.
*/
Automatic = 1,
/**
* Code actions were requested maually by the user or an extension.
*/
Manual = 2,
}
export interface CodeActionContext {
/**
* The reason why code actions were requested.
*/
readonly triggerKind: CodeActionTriggerKind;
}
//#endregion
}

View file

@ -1151,7 +1151,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
CandidatePortSource: CandidatePortSource,
CodeAction: extHostTypes.CodeAction,
CodeActionKind: extHostTypes.CodeActionKind,
CodeActionTrigger: extHostTypes.CodeActionTrigger,
CodeActionTriggerKind: extHostTypes.CodeActionTriggerKind,
CodeLens: extHostTypes.CodeLens,
Color: extHostTypes.Color,
ColorInformation: extHostTypes.ColorInformation,

View file

@ -412,7 +412,8 @@ class CodeActionAdapter {
const codeActionContext: vscode.CodeActionContext = {
diagnostics: allDiagnostics,
only: context.only ? new CodeActionKind(context.only) : undefined
only: context.only ? new CodeActionKind(context.only) : undefined,
triggerKind: typeConvert.CodeActionTriggerKind.to(context.trigger),
};
return asPromise(() => this._provider.provideCodeActions(doc, ran, codeActionContext, token)).then((commandsOrActions): extHostProtocol.ICodeActionListDto | undefined => {

View file

@ -1712,3 +1712,16 @@ export namespace TestResults {
};
}
}
export namespace CodeActionTriggerKind {
export function to(value: modes.CodeActionTriggerType): types.CodeActionTriggerKind {
switch (value) {
case modes.CodeActionTriggerType.Auto:
return types.CodeActionTriggerKind.Automatic;
case modes.CodeActionTriggerType.Manual:
return types.CodeActionTriggerKind.Manual;
}
}
}

View file

@ -1178,7 +1178,7 @@ export class DocumentSymbol {
}
export enum CodeActionTrigger {
export enum CodeActionTriggerKind {
Automatic = 1,
Manual = 2,
}