Skip to content

Commit

Permalink
feat: emit an event after a configuration section was updated
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Apr 7, 2024
1 parent d873ebe commit 7419f30
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
39 changes: 37 additions & 2 deletions packages/langium/src/workspace/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import type { ConfigurationItem, DidChangeConfigurationParams, DidChangeConfigurationRegistrationOptions, InitializeParams, InitializedParams } from 'vscode-languageserver-protocol';
import { Emitter } from 'vscode-languageserver-protocol';
import type {
ConfigurationItem,
DidChangeConfigurationParams,
DidChangeConfigurationRegistrationOptions,
Disposable,
Event,
InitializeParams,
InitializedParams
} from 'vscode-languageserver-protocol';
import type { ServiceRegistry } from '../service-registry.js';
import type { LangiumSharedCoreServices } from '../services.js';
import { Deferred } from '../utils/promise-utils.js';
Expand Down Expand Up @@ -45,13 +54,32 @@ export interface ConfigurationProvider {
* `settings` property of the change object could be expressed as `Record<string, Record<string, any>>`
*/
updateConfiguration(change: DidChangeConfigurationParams): void;

/**
* Get notified after a configuration section has been updated.
*/
onConfigurationSectionUpdate(callback: ConfigurationSectionUpdateListener): Disposable
}

export interface ConfigurationInitializedParams extends InitializedParams {
register?: (params: DidChangeConfigurationRegistrationOptions) => void,
fetchConfiguration?: (configuration: ConfigurationItem[]) => Promise<any>
}

export interface ConfigurationSectionUpdate {
/**
* The name of the configuration section that has been updated.
*/
section: string;

/**
* The updated configuration section.
*/
configuration: any;
}

export type ConfigurationSectionUpdateListener = (update: ConfigurationSectionUpdate) => void;

/**
* Base configuration provider for building up other configuration providers
*/
Expand All @@ -61,6 +89,7 @@ export class DefaultConfigurationProvider implements ConfigurationProvider {
protected readonly _ready = new Deferred<void>();
protected settings: Record<string, Record<string, any>> = {};
protected workspaceConfig = false;
protected onConfigurationSectionUpdateEmitter = new Emitter<ConfigurationSectionUpdate>();

constructor(services: LangiumSharedCoreServices) {
this.serviceRegistry = services.ServiceRegistry;
Expand Down Expand Up @@ -116,7 +145,9 @@ export class DefaultConfigurationProvider implements ConfigurationProvider {
return;
}
Object.keys(change.settings).forEach(section => {
this.updateSectionConfiguration(section, change.settings[section]);
const configuration = change.settings[section];
this.updateSectionConfiguration(section, configuration);
this.onConfigurationSectionUpdateEmitter.fire({ section, configuration });
});
}

Expand All @@ -142,4 +173,8 @@ export class DefaultConfigurationProvider implements ConfigurationProvider {
protected toSectionName(languageId: string): string {
return `${languageId}`;
}

get onConfigurationSectionUpdate(): Event<ConfigurationSectionUpdate> {
return this.onConfigurationSectionUpdateEmitter.event;
}
}
10 changes: 10 additions & 0 deletions packages/langium/test/workspace/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ describe('ConfigurationProvider', () => {
configs.updateConfiguration({ settings: { 'someLang': { 'prop': 'bar2' } } });
expect(await configs.getConfiguration('someLang', 'prop')).toBe('bar2');
});

test('emits `onConfigurationSectionUpdate` on `updateConfiguration` call', async () => {
let called = false;
configs.onConfigurationSectionUpdate(() => {
called = true;
});

configs.updateConfiguration({ settings: { 'someLang': { 'prop': 'bar' } } });
expect(called).toBe(true);
});
});

0 comments on commit 7419f30

Please sign in to comment.