Skip to content

Commit

Permalink
Apply auto fix on save actions sequentially and make sure we request …
Browse files Browse the repository at this point in the history
…fixes of the specific type we are interested in
  • Loading branch information
mjbvz committed Jan 22, 2019
1 parent 2ebdac4 commit c994fc2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TypeScriptAutoFixProvider implements vscode.CodeActionProvider {
context: vscode.CodeActionContext,
token: vscode.CancellationToken
): Promise<vscode.CodeAction[] | undefined> {
if (!context.only || !context.only.contains(vscode.CodeActionKind.Source)) {
if (!context.only || !(context.only.contains(vscode.CodeActionKind.SourceAutoFix) || vscode.CodeActionKind.SourceAutoFix.contains(context.only))) {
return undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ export class OrganizeImportsCodeActionProvider implements vscode.CodeActionProvi
return [];
}

if (!context.only || !context.only.contains(vscode.CodeActionKind.SourceOrganizeImports)) {
if (!context.only || !(context.only.contains(vscode.CodeActionKind.SourceOrganizeImports) || vscode.CodeActionKind.SourceOrganizeImports.contains(context.only))) {
return [];
}

this.fileConfigManager.ensureConfigurationForDocument(document, token);

const action = new vscode.CodeAction(
localize('oraganizeImportsAction.title', "Organize Imports"),
localize('organizeImportsAction.title', "Organize Imports"),
vscode.CodeActionKind.SourceOrganizeImports);
action.command = { title: '', command: OrganizeImportsCommand.Id, arguments: [file] };
return [action];
Expand Down
31 changes: 19 additions & 12 deletions src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,22 @@ class CodeActionOnParticipant implements ISaveParticipant {

const timeout = this._configurationService.getValue<number>('editor.codeActionsOnSaveTimeout', settingsOverrides);

return new Promise<CodeAction[]>((resolve, reject) => {
setTimeout(() => reject(localize('codeActionsOnSave.didTimeout', "Aborted codeActionsOnSave after {0}ms", timeout)), timeout);
this.getActionsToRun(model, codeActionsOnSave).then(resolve);
}).then(actionsToRun => {
// Failure to apply a code action should not block other on save actions
return this.applyCodeActions(actionsToRun).catch(() => undefined);
});
return Promise.race([
new Promise<void>((_resolve, reject) =>
setTimeout(() => reject(localize('codeActionsOnSave.didTimeout', "Aborted codeActionsOnSave after {0}ms", timeout)), timeout)),
this.applyOnSaveActions(model, codeActionsOnSave)
]).then(() => undefined);
}

private async applyOnSaveActions(model: ITextModel, codeActionsOnSave: CodeActionKind[]): Promise<void> {
for (const codeActionKind of codeActionsOnSave) {
const actionsToRun = await this.getActionsToRun(model, codeActionKind);
try {
await this.applyCodeActions(actionsToRun);
} catch {
// Failure to apply a code action should not block other on save actions
}
}
}

private async applyCodeActions(actionsToRun: CodeAction[]) {
Expand All @@ -333,13 +342,11 @@ class CodeActionOnParticipant implements ISaveParticipant {
}
}

private async getActionsToRun(model: ITextModel, codeActionsOnSave: CodeActionKind[]) {
const actions = await getCodeActions(model, model.getFullModelRange(), {
private getActionsToRun(model: ITextModel, codeActionKind: CodeActionKind) {
return getCodeActions(model, model.getFullModelRange(), {
type: 'auto',
filter: { kind: CodeActionKind.Source, includeSourceActions: true },
filter: { kind: codeActionKind, includeSourceActions: true },
});
const actionsToRun = actions.filter(returnedAction => returnedAction.kind && codeActionsOnSave.some(onSaveKind => onSaveKind.contains(returnedAction.kind)));
return actionsToRun;
}
}

Expand Down

0 comments on commit c994fc2

Please sign in to comment.