Skip to content

Commit

Permalink
Fix: Allows Arrays in Setting for Code Actions on Save (#194930)
Browse files Browse the repository at this point in the history
* allows arrays to be used in code actions on save

* revert changes in non-important files

* code cleanup

* adding back in for proper intellisense

* added more specific logic check

* fixes contains issue:

* added additional fix for overwritten duplicate subsets. reverting larger, less necessary code
  • Loading branch information
justschen authored Oct 10, 2023
1 parent df114fc commit f1b07bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const codeActionsOnSaveSchema: IConfigurationPropertySchema = {
}
],
markdownDescription: nls.localize('editor.codeActionsOnSave', 'Run CodeActions for the editor on save. CodeActions must be specified and the editor must not be shutting down. Example: `"source.organizeImports": "explicit" `'),
type: 'object',
type: ['object', 'array'],
additionalProperties: {
type: ['string', 'boolean'],
enum: ['always', 'explicit', 'never', true, false],
Expand Down
28 changes: 14 additions & 14 deletions src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class CodeActionOnSaveParticipant implements ITextFileSaveParticipant {
const settingsOverrides = { overrideIdentifier: textEditorModel.getLanguageId(), resource: textEditorModel.uri };

// Convert boolean values to strings
const setting = this.configurationService.getValue<{ [kind: string]: string | boolean }>('editor.codeActionsOnSave', settingsOverrides);
const setting = this.configurationService.getValue<{ [kind: string]: string | boolean } | string[]>('editor.codeActionsOnSave', settingsOverrides);
if (!setting) {
return undefined;
}
Expand All @@ -290,16 +290,15 @@ class CodeActionOnSaveParticipant implements ITextFileSaveParticipant {
return undefined;
}

const convertedSetting: { [kind: string]: string } = {};
for (const key in setting) {
if (typeof setting[key] === 'boolean') {
convertedSetting[key] = setting[key] ? 'explicit' : 'never';
} else if (typeof setting[key] === 'string') {
convertedSetting[key] = setting[key] as string;
}
if (env.reason !== SaveReason.EXPLICIT && Array.isArray(setting)) {
return undefined;
}

const codeActionsOnSave = this.createCodeActionsOnSave(Object.keys(convertedSetting));
const settingItems: string[] = Array.isArray(setting)
? setting
: Object.keys(setting).filter(x => setting[x] && setting[x] !== 'never');

const codeActionsOnSave = this.createCodeActionsOnSave(settingItems);

if (!Array.isArray(setting)) {
codeActionsOnSave.sort((a, b) => {
Expand All @@ -319,14 +318,15 @@ class CodeActionOnSaveParticipant implements ITextFileSaveParticipant {
if (!codeActionsOnSave.length) {
return undefined;
}

const excludedActions = Object.keys(setting)
.filter(x => convertedSetting[x] === 'never' || false)
.map(x => new CodeActionKind(x));
const excludedActions = Array.isArray(setting)
? []
: Object.keys(setting)
.filter(x => setting[x] === 'never' || false)
.map(x => new CodeActionKind(x));

progress.report({ message: localize('codeaction', "Quick Fixes") });

const filteredSaveList = codeActionsOnSave.filter(x => convertedSetting[x.value] === 'always' || (convertedSetting[x.value] === 'explicit') && env.reason === SaveReason.EXPLICIT);
const filteredSaveList = Array.isArray(setting) ? codeActionsOnSave : codeActionsOnSave.filter(x => setting[x.value] === 'always' || ((setting[x.value] === 'explicit' || setting[x.value] === true) && env.reason === SaveReason.EXPLICIT));

await this.applyOnSaveActions(textEditorModel, filteredSaveList, excludedActions, progress, token);
}
Expand Down

0 comments on commit f1b07bd

Please sign in to comment.