From f44a5ee4f7249633d734b636fbadeffd990d2963 Mon Sep 17 00:00:00 2001 From: turara Date: Wed, 7 Oct 2020 00:52:35 +0900 Subject: [PATCH 1/2] Update CodeActionOnSaveParticipant Add filtering CodeActionKind.SourceFixAll derivatives for codeActionsOnSave --- .../codeEditor/browser/saveParticipants.ts | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts b/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts index 698828c06d76e9..a7d2caec95f49d 100644 --- a/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts +++ b/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts @@ -287,9 +287,26 @@ class CodeActionOnSaveParticipant implements ITextFileSaveParticipant { ? setting : Object.keys(setting).filter(x => setting[x]); - const codeActionsOnSave = settingItems + if (!settingItems.length) { + return undefined; + } + + const excludedActions = Array.isArray(setting) + ? [] + : Object.keys(setting) + .filter(x => setting[x] === false) + .map(x => new CodeActionKind(x)); + + let codeActionsOnSave = settingItems .map(x => new CodeActionKind(x)); + if (!excludedActions.some(a => a.equals(CodeActionKind.SourceFixAll))) { + codeActionsOnSave = [ + CodeActionKind.SourceFixAll, + ...codeActionsOnSave.filter(a => !CodeActionKind.SourceFixAll.contains(a)) + ]; + } + if (!Array.isArray(setting)) { codeActionsOnSave.sort((a, b) => { if (CodeActionKind.SourceFixAll.contains(a)) { @@ -305,16 +322,6 @@ class CodeActionOnSaveParticipant implements ITextFileSaveParticipant { }); } - if (!codeActionsOnSave.length) { - return undefined; - } - - const excludedActions = Array.isArray(setting) - ? [] - : Object.keys(setting) - .filter(x => setting[x] === false) - .map(x => new CodeActionKind(x)); - progress.report({ message: localize('codeaction', "Quick Fixes") }); await this.applyOnSaveActions(textEditorModel, codeActionsOnSave, excludedActions, progress, token); } From b7a090244d38391d7143f18d8ebb115aac0fae21 Mon Sep 17 00:00:00 2001 From: turara Date: Tue, 27 Oct 2020 12:14:03 +0900 Subject: [PATCH 2/2] Add CodeActionOnSaveParticipant#createCodeActionsOnSave method to remove subsets. Fixes #106924. --- .../codeEditor/browser/saveParticipants.ts | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts b/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts index a7d2caec95f49d..940af70ff65c88 100644 --- a/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts +++ b/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts @@ -287,25 +287,7 @@ class CodeActionOnSaveParticipant implements ITextFileSaveParticipant { ? setting : Object.keys(setting).filter(x => setting[x]); - if (!settingItems.length) { - return undefined; - } - - const excludedActions = Array.isArray(setting) - ? [] - : Object.keys(setting) - .filter(x => setting[x] === false) - .map(x => new CodeActionKind(x)); - - let codeActionsOnSave = settingItems - .map(x => new CodeActionKind(x)); - - if (!excludedActions.some(a => a.equals(CodeActionKind.SourceFixAll))) { - codeActionsOnSave = [ - CodeActionKind.SourceFixAll, - ...codeActionsOnSave.filter(a => !CodeActionKind.SourceFixAll.contains(a)) - ]; - } + const codeActionsOnSave = this.createCodeActionsOnSave(settingItems); if (!Array.isArray(setting)) { codeActionsOnSave.sort((a, b) => { @@ -322,10 +304,42 @@ class CodeActionOnSaveParticipant implements ITextFileSaveParticipant { }); } + if (!codeActionsOnSave.length) { + return undefined; + } + + const excludedActions = Array.isArray(setting) + ? [] + : Object.keys(setting) + .filter(x => setting[x] === false) + .map(x => new CodeActionKind(x)); + progress.report({ message: localize('codeaction', "Quick Fixes") }); await this.applyOnSaveActions(textEditorModel, codeActionsOnSave, excludedActions, progress, token); } + private createCodeActionsOnSave(settingItems: string[]): CodeActionKind[] { + const actionSeeds = new Set(); + + // Remove subsets + const len = settingItems.length; + outer: for (let i = 0; i < len; i++) { + const s1 = settingItems[i]; + for (let j = 0; j < len; j++) { + if (j === i) { + continue; + } + const s2 = settingItems[j]; + if (s1.startsWith(s2) && s1.length > s2.length) { + continue outer; + } + } + actionSeeds.add(s1); + } + + return Array.from(actionSeeds).map(x => new CodeActionKind(x)); + } + private async applyOnSaveActions(model: ITextModel, codeActionsOnSave: readonly CodeActionKind[], excludes: readonly CodeActionKind[], progress: IProgress, token: CancellationToken): Promise { const getActionProgress = new class implements IProgress {