|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { flatten } from 'vs/base/common/arrays'; |
| 7 | +import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema'; |
| 8 | +import { CodeActionKind } from 'vs/editor/contrib/codeAction/codeActionTrigger'; |
| 9 | +import * as nls from 'vs/nls'; |
| 10 | +import { Extensions, IConfigurationNode, IConfigurationRegistry } from 'vs/platform/configuration/common/configurationRegistry'; |
| 11 | +import { Registry } from 'vs/platform/registry/common/platform'; |
| 12 | +import { IWorkbenchContribution } from 'vs/workbench/common/contributions'; |
| 13 | +import { CodeActionExtensionPointFields, CodeActionsExtensionPoint } from 'vs/workbench/contrib/codeActions/common/extensionPoint'; |
| 14 | +import { IExtensionPoint, IExtensionPointUser } from 'vs/workbench/services/extensions/common/extensionsRegistry'; |
| 15 | + |
| 16 | +const codeActionsOnSaveDefaultProperties = Object.freeze<IJSONSchemaMap>({ |
| 17 | + 'source.fixAll': { |
| 18 | + type: 'boolean', |
| 19 | + description: nls.localize('codeActionsOnSave.fixAll', "Controls whether auto fix action should be run on file save.") |
| 20 | + } |
| 21 | +}); |
| 22 | + |
| 23 | +const codeActionsOnSaveSchema: IJSONSchema = { |
| 24 | + type: 'object', |
| 25 | + properties: codeActionsOnSaveDefaultProperties, |
| 26 | + 'additionalProperties': { |
| 27 | + type: 'boolean' |
| 28 | + }, |
| 29 | + default: {}, |
| 30 | + description: nls.localize('codeActionsOnSave', "Code action kinds to be run on save.") |
| 31 | +}; |
| 32 | + |
| 33 | +export const editorConfiguration = Object.freeze<IConfigurationNode>({ |
| 34 | + id: 'editor', |
| 35 | + order: 5, |
| 36 | + type: 'object', |
| 37 | + title: nls.localize('editorConfigurationTitle', "Editor"), |
| 38 | + overridable: true, |
| 39 | + properties: { |
| 40 | + 'editor.codeActionsOnSave': codeActionsOnSaveSchema, |
| 41 | + 'editor.codeActionsOnSaveTimeout': { |
| 42 | + type: 'number', |
| 43 | + default: 750, |
| 44 | + description: nls.localize('codeActionsOnSaveTimeout', "Timeout in milliseconds after which the code actions that are run on save are cancelled.") |
| 45 | + }, |
| 46 | + } |
| 47 | +}); |
| 48 | + |
| 49 | +export class CodeActionConfigurationManager implements IWorkbenchContribution { |
| 50 | + constructor( |
| 51 | + codeActionsExtensionPoint: IExtensionPoint<CodeActionsExtensionPoint[]> |
| 52 | + ) { |
| 53 | + codeActionsExtensionPoint.setHandler(extensionPoints => { |
| 54 | + const newProperties: IJSONSchemaMap = { ...codeActionsOnSaveDefaultProperties }; |
| 55 | + for (const [sourceAction, props] of this.getSourceActions(extensionPoints)) { |
| 56 | + newProperties[sourceAction] = { |
| 57 | + type: 'boolean', |
| 58 | + description: nls.localize( |
| 59 | + 'codeActionsOnSave.generic', |
| 60 | + "Controls whether '{0}' actions should be run on file save.", |
| 61 | + props.title) |
| 62 | + }; |
| 63 | + } |
| 64 | + codeActionsOnSaveSchema.properties = newProperties; |
| 65 | + |
| 66 | + Registry.as<IConfigurationRegistry>(Extensions.Configuration) |
| 67 | + .notifyConfigurationSchemaUpdated(editorConfiguration); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + private getSourceActions(extensionPoints: readonly IExtensionPointUser<CodeActionsExtensionPoint[]>[]) { |
| 72 | + const sourceActions = new Map<string, { readonly title: string }>(); |
| 73 | + for (const contribution of flatten(extensionPoints.map(x => x.value))) { |
| 74 | + const kind = new CodeActionKind(contribution[CodeActionExtensionPointFields.kind]); |
| 75 | + const defaultKinds = Object.keys(codeActionsOnSaveDefaultProperties).map(value => new CodeActionKind(value)); |
| 76 | + if (CodeActionKind.Source.contains(kind) |
| 77 | + // Exclude any we already included by default |
| 78 | + && !defaultKinds.some(defaultKind => defaultKind.contains(kind)) |
| 79 | + ) { |
| 80 | + sourceActions.set(kind.value, contribution); |
| 81 | + } |
| 82 | + } |
| 83 | + return sourceActions; |
| 84 | + } |
| 85 | +} |
0 commit comments