Skip to content

Commit

Permalink
[monaco] properly register CodeActionProviders.
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Efftinge <sven.efftinge@typefox.io>
  • Loading branch information
svenefftinge committed Nov 15, 2019
1 parent d2ac015 commit eb7749e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
5 changes: 4 additions & 1 deletion packages/monaco/src/browser/monaco-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function loadMonaco(vsRequire: any): Promise<void> {
'vs/platform/configuration/common/configurationModels',
'vs/editor/browser/services/codeEditorService',
'vs/editor/browser/services/codeEditorServiceImpl',
'vs/platform/markers/common/markerService',
'vs/platform/contextkey/common/contextkey',
'vs/platform/contextkey/browser/contextKeyService',
'vs/base/common/errors'
Expand All @@ -78,13 +79,15 @@ export function loadMonaco(vsRequire: any): Promise<void> {
filters: any, styler: any, platform: any, modes: any, suggest: any, snippetParser: any,
configuration: any, configurationModels: any,
codeEditorService: any, codeEditorServiceImpl: any,
markerService: any,
contextKey: any, contextKeyService: any,
error: any) => {
const global: any = self;
global.monaco.commands = commands;
global.monaco.actions = actions;
global.monaco.keybindings = Object.assign({}, keybindingsRegistry, keybindingResolver, resolvedKeybinding, keybindingLabels, keyCodes);
global.monaco.services = Object.assign({}, simpleServices, standaloneServices, configuration, configurationModels, codeEditorService, codeEditorServiceImpl);
global.monaco.services = Object.assign({}, simpleServices, standaloneServices, configuration, configurationModels,
codeEditorService, codeEditorServiceImpl, markerService);
global.monaco.quickOpen = Object.assign({}, quickOpenWidget, quickOpenModel);
global.monaco.filters = filters;
global.monaco.theme = styler;
Expand Down
7 changes: 7 additions & 0 deletions packages/monaco/src/typings/monaco/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ declare module monaco.services {
set<T>(id: any, instanceOrDescriptor: T): T;
}

export interface IMarkerService {
read(filter?: { owner?: string; resource?: monaco.Uri; severities?: number, take?: number; }): editor.IMarkerData[];
}

export module StaticServices {
export function init(overrides: monaco.editor.IEditorOverrideServices): [ServiceCollection, monaco.instantiation.IInstantiationService];
export const standaloneThemeService: LazyStaticService<IStandaloneThemeService>;
Expand All @@ -520,6 +524,7 @@ declare module monaco.services {
export const configurationService: LazyStaticService<IConfigurationService>;
export const resourcePropertiesService: LazyStaticService<ITextResourcePropertiesService>;
export const instantiationService: LazyStaticService<monaco.instantiation.IInstantiationService>;
export const markerService: LazyStaticService<IMarkerService>;
}
}

Expand Down Expand Up @@ -844,6 +849,8 @@ declare module monaco.modes {
export const DocumentSymbolProviderRegistry: LanguageFeatureRegistry<monaco.languages.DocumentSymbolProvider>;

export const CompletionProviderRegistry: LanguageFeatureRegistry<monaco.languages.CompletionItemProvider>;

export const CodeActionProviderRegistry: LanguageFeatureRegistry<monaco.languages.CodeActionProvider & { providedCodeActionKinds?: string[] }>;
}

declare module monaco.suggest {
Expand Down
19 changes: 10 additions & 9 deletions packages/plugin-ext/src/main/browser/languages-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,21 +656,22 @@ export class LanguagesMainImpl implements LanguagesMain, Disposable {
}, token);
}

$registerQuickFixProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[], codeActionKinds?: string[]): void {
$registerQuickFixProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[], providedCodeActionKinds?: string[]): void {
const languageSelector = fromLanguageSelector(selector);
const quickFixProvider = this.createQuickFixProvider(handle, codeActionKinds);
this.register(handle, monaco.languages.registerCodeActionProvider(languageSelector, quickFixProvider));
}

protected createQuickFixProvider(handle: number, providedCodeActionKinds?: string[]): monaco.languages.CodeActionProvider {
return {
provideCodeActions: async (model, rangeOrSelection, monacoContext, token) => this.provideCodeActions(handle, model, rangeOrSelection, monacoContext, token)
const quickFixProvider = {
provideCodeActions: (model: monaco.editor.ITextModel, range: monaco.Range,
context: monaco.languages.CodeActionContext, token: monaco.CancellationToken): monaco.languages.CodeActionList | Promise<monaco.languages.CodeActionList> => {
const markers = monaco.services.StaticServices.markerService.get().read({ resource: model.uri }).filter(m => monaco.Range.areIntersectingOrTouching(m, range));
return this.provideCodeActions(handle, model, range, { markers, only: context.only }, token);
},
providedCodeActionKinds
};
this.register(handle, monaco.modes.CodeActionProviderRegistry.register(languageSelector, quickFixProvider));
}

protected async provideCodeActions(handle: number, model: monaco.editor.ITextModel,
rangeOrSelection: Range, context: monaco.languages.CodeActionContext,
token: monaco.CancellationToken): Promise<monaco.languages.CodeActionList | Promise<monaco.languages.CodeActionList>> {
token: monaco.CancellationToken): Promise<monaco.languages.CodeActionList | monaco.languages.CodeActionList> {
const actions = await this.proxy.$provideCodeActions(handle, model.uri, rangeOrSelection, context, token);
if (!actions) {
return undefined!;
Expand Down

0 comments on commit eb7749e

Please sign in to comment.