Skip to content

Commit b7ff4c5

Browse files
authored
intellisense for code actions on save (microsoft#215475)
* language registry and code actions on save new filter * fixes logic * more clean up * remove static contribution
1 parent 445a0cf commit b7ff4c5

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/vs/editor/common/languageFeatureRegistry.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ export class LanguageFeatureRegistry<T> {
109109
return result;
110110
}
111111

112+
allNoModel(): T[] {
113+
return this._entries.map(entry => entry.provider);
114+
}
115+
112116
ordered(model: ITextModel): T[] {
113117
const result: T[] = [];
114118
this._orderedForEach(model, entry => result.push(entry.provider));

src/vs/workbench/contrib/codeActions/browser/codeActionsContribution.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { HierarchicalKind } from 'vs/base/common/hierarchicalKind';
88
import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema';
99
import { Disposable } from 'vs/base/common/lifecycle';
1010
import { editorConfigurationBaseNode } from 'vs/editor/common/config/editorConfigurationSchema';
11+
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
1112
import { codeActionCommandId, refactorCommandId, sourceActionCommandId } from 'vs/editor/contrib/codeAction/browser/codeAction';
1213
import { CodeActionKind } from 'vs/editor/contrib/codeAction/common/types';
1314
import * as nls from 'vs/nls';
@@ -34,15 +35,11 @@ const createCodeActionsAutoSave = (description: string): IJSONSchema => {
3435
};
3536
};
3637

37-
const codeActionsOnSaveDefaultProperties = Object.freeze<IJSONSchemaMap>({
38-
'source.fixAll': createCodeActionsAutoSave(nls.localize('codeActionsOnSave.fixAll', "Controls whether auto fix action should be run on file save.")),
39-
});
4038

4139
const codeActionsOnSaveSchema: IConfigurationPropertySchema = {
4240
oneOf: [
4341
{
4442
type: 'object',
45-
properties: codeActionsOnSaveDefaultProperties,
4643
additionalProperties: {
4744
type: 'string'
4845
},
@@ -72,15 +69,24 @@ export const editorConfiguration = Object.freeze<IConfigurationNode>({
7269
export class CodeActionsContribution extends Disposable implements IWorkbenchContribution {
7370

7471
private _contributedCodeActions: CodeActionsExtensionPoint[] = [];
72+
private settings: Set<string> = new Set<string>();
7573

7674
private readonly _onDidChangeContributions = this._register(new Emitter<void>());
7775

7876
constructor(
7977
codeActionsExtensionPoint: IExtensionPoint<CodeActionsExtensionPoint[]>,
8078
@IKeybindingService keybindingService: IKeybindingService,
79+
@ILanguageFeaturesService private readonly languageFeatures: ILanguageFeaturesService
8180
) {
8281
super();
8382

83+
// TODO: @justschen caching of code actions based on extensions loaded: https://github.com/microsoft/vscode/issues/216019
84+
85+
languageFeatures.codeActionProvider.onDidChange(() => {
86+
this.updateSettingsFromCodeActionProviders();
87+
this.updateConfigurationSchemaFromContribs();
88+
}, 2000);
89+
8490
codeActionsExtensionPoint.setHandler(extensionPoints => {
8591
this._contributedCodeActions = extensionPoints.flatMap(x => x.value).filter(x => Array.isArray(x.actions));
8692
this.updateConfigurationSchema(this._contributedCodeActions);
@@ -93,26 +99,48 @@ export class CodeActionsContribution extends Disposable implements IWorkbenchCon
9399
});
94100
}
95101

102+
private updateSettingsFromCodeActionProviders(): void {
103+
const providers = this.languageFeatures.codeActionProvider.allNoModel();
104+
providers.forEach(provider => {
105+
if (provider.providedCodeActionKinds) {
106+
provider.providedCodeActionKinds.forEach(kind => {
107+
if (!this.settings.has(kind) && CodeActionKind.Source.contains(new HierarchicalKind(kind))) {
108+
this.settings.add(kind);
109+
}
110+
});
111+
}
112+
});
113+
}
114+
96115
private updateConfigurationSchema(codeActionContributions: readonly CodeActionsExtensionPoint[]) {
97-
const newProperties: IJSONSchemaMap = { ...codeActionsOnSaveDefaultProperties };
116+
const newProperties: IJSONSchemaMap = {};
98117
for (const [sourceAction, props] of this.getSourceActions(codeActionContributions)) {
118+
this.settings.add(sourceAction);
99119
newProperties[sourceAction] = createCodeActionsAutoSave(nls.localize('codeActionsOnSave.generic', "Controls whether '{0}' actions should be run on file save.", props.title));
100120
}
101121
codeActionsOnSaveSchema.properties = newProperties;
102122
Registry.as<IConfigurationRegistry>(Extensions.Configuration)
103123
.notifyConfigurationSchemaUpdated(editorConfiguration);
104124
}
105125

126+
private updateConfigurationSchemaFromContribs() {
127+
const properties: IJSONSchemaMap = { ...codeActionsOnSaveSchema.properties };
128+
for (const codeActionKind of this.settings) {
129+
if (!properties[codeActionKind]) {
130+
properties[codeActionKind] = createCodeActionsAutoSave(nls.localize('codeActionsOnSave.generic', "Controls whether '{0}' actions should be run on file save.", codeActionKind));
131+
}
132+
}
133+
codeActionsOnSaveSchema.properties = properties;
134+
Registry.as<IConfigurationRegistry>(Extensions.Configuration)
135+
.notifyConfigurationSchemaUpdated(editorConfiguration);
136+
}
137+
106138
private getSourceActions(contributions: readonly CodeActionsExtensionPoint[]) {
107-
const defaultKinds = Object.keys(codeActionsOnSaveDefaultProperties).map(value => new HierarchicalKind(value));
108139
const sourceActions = new Map<string, { readonly title: string }>();
109140
for (const contribution of contributions) {
110141
for (const action of contribution.actions) {
111142
const kind = new HierarchicalKind(action.kind);
112-
if (CodeActionKind.Source.contains(kind)
113-
// Exclude any we already included by default
114-
&& !defaultKinds.some(defaultKind => defaultKind.contains(kind))
115-
) {
143+
if (CodeActionKind.Source.contains(kind)) {
116144
sourceActions.set(kind.value, action);
117145
}
118146
}

0 commit comments

Comments
 (0)