Skip to content
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

Fix: Allows Arrays in Setting for Code Actions on Save #194930

Merged
merged 7 commits into from
Oct 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I am warning and we will see

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything will be known


// 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