/
configuration.ts
76 lines (64 loc) · 2.71 KB
/
configuration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/******************************************************************************
* Copyright 2022 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import type { DidChangeConfigurationParams } from 'vscode-languageserver';
import type { CoreServiceRegistryI } from '../service-registry.js';
import type { LangiumSharedCoreServices } from '../services.js';
/* eslint-disable @typescript-eslint/no-explicit-any */
export interface ConfigurationProvider {
/**
* Returns a configuration value stored for the given language.
*
* @param language The language id
* @param configuration Configuration name
*/
getConfiguration(language: string, configuration: string): Promise<any>;
/**
* Updates the cached configurations using the `change` notification parameters.
*
* @param change The parameters of a change configuration notification.
* `settings` property of the change object could be expressed as `Record<string, Record<string, any>>`
*/
updateConfiguration(change: DidChangeConfigurationParams): void;
}
/**
* Empty Configuration provider, used in the absence of an LSP supported configuration.
* Which is typically required to set this up properly.
*/
export class EmptyConfigurationProvider implements ConfigurationProvider {
protected settings: Record<string, Record<string, any>> = {};
protected workspaceConfig = false;
protected initialized = false;
protected readonly serviceRegistry: CoreServiceRegistryI;
constructor(services: LangiumSharedCoreServices) {
this.serviceRegistry = services.ServiceRegistry;
}
protected async initialize(): Promise<void> {
this.initialized = true;
}
updateConfiguration(change: DidChangeConfigurationParams): void {
if (!change.settings) {
return;
}
Object.keys(change.settings).forEach(section => {
this.updateSectionConfiguration(section, change.settings[section]);
});
}
protected updateSectionConfiguration(section: string, configuration: any): void {
this.settings[section] = configuration;
}
async getConfiguration(language: string, configuration: string): Promise<any> {
if (!this.initialized) {
await this.initialize();
}
const sectionName = this.toSectionName(language);
if (this.settings[sectionName]) {
return this.settings[sectionName][configuration];
}
}
protected toSectionName(languageId: string): string {
return `${languageId}`;
}
}