/
default-configuration.ts
81 lines (71 loc) · 3.46 KB
/
default-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
77
78
79
80
81
/******************************************************************************
* Copyright 2023 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 { Connection, DidChangeConfigurationParams } from 'vscode-languageserver';
import type { ConfigurationItem } from 'vscode-languageserver-protocol';
import type { CoreServiceRegistryI } from '../../service-registry.js';
import { DidChangeConfigurationNotification } from 'vscode-languageserver-protocol';
import type { ConfigurationProvider } from '../../workspace/configuration.js';
import type { LangiumSharedServices } from '../lsp-services.js';
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Configuration provider that uses LSP services to function
*/
export class LSPConfigurationProvider implements ConfigurationProvider {
protected settings: Record<string, Record<string, any>> = {};
protected workspaceConfig = false;
protected initialized = false;
protected readonly serviceRegistry: CoreServiceRegistryI;
protected readonly connection: Connection | undefined;
constructor(services: LangiumSharedServices) {
this.serviceRegistry = services.ServiceRegistry;
this.connection = services.lsp.Connection;
services.lsp.LanguageServer.onInitialize(params => {
this.workspaceConfig = params.capabilities.workspace?.configuration ?? false;
});
services.lsp.LanguageServer.onInitialized(_params => {
const languages = this.serviceRegistry.all;
services.lsp.Connection?.client.register(DidChangeConfigurationNotification.type, {
// Listen to configuration changes for all languages
section: languages.map(lang => this.toSectionName(lang.LanguageMetaData.languageId))
});
});
}
protected async initialize(): Promise<void> {
if (this.workspaceConfig && this.connection) {
const languages = this.serviceRegistry.all;
const configToUpdate: ConfigurationItem[] = languages.map(lang => { return { section: this.toSectionName(lang.LanguageMetaData.languageId) }; });
// get workspace configurations (default scope URI)
const configs = await this.connection.workspace.getConfiguration(configToUpdate);
configToUpdate.forEach((conf, idx) => {
this.updateSectionConfiguration(conf.section!, configs[idx]);
});
}
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}`;
}
}