-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathConfigurationManager.ts
214 lines (173 loc) · 7.29 KB
/
ConfigurationManager.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2022 - 2024 The MathWorks, Inc.
import { ClientCapabilities, DidChangeConfigurationNotification, DidChangeConfigurationParams } from 'vscode-languageserver'
import { reportTelemetrySettingsChange } from '../logging/TelemetryUtils'
import { getCliArgs } from '../utils/CliUtils'
import ClientConnection from '../ClientConnection'
export enum Argument {
// Basic arguments
MatlabLaunchCommandArguments = 'matlabLaunchCommandArgs',
MatlabInstallationPath = 'matlabInstallPath',
MatlabConnectionTiming = 'matlabConnectionTiming',
ShouldIndexWorkspace = 'indexWorkspace',
// Advanced arguments
MatlabUrl = 'matlabUrl',
SnippetIgnoreList = 'snippetIgnoreList'
}
export enum ConnectionTiming {
OnStart = 'onStart',
OnDemand = 'onDemand',
Never = 'never'
}
interface CliArguments {
[Argument.MatlabLaunchCommandArguments]: string
[Argument.MatlabUrl]: string
[Argument.SnippetIgnoreList]: string
}
export interface Settings {
installPath: string
matlabConnectionTiming: ConnectionTiming
indexWorkspace: boolean
telemetry: boolean
maxFileSizeForAnalysis: number
signIn: boolean
}
type SettingName = 'installPath' | 'matlabConnectionTiming' | 'indexWorkspace' | 'telemetry' | 'maxFileSizeForAnalysis' | 'signIn'
const SETTING_NAMES: SettingName[] = [
'installPath',
'matlabConnectionTiming',
'indexWorkspace',
'telemetry',
'maxFileSizeForAnalysis',
'signIn'
]
class ConfigurationManager {
private static instance: ConfigurationManager
private configuration: Settings | null = null
private readonly defaultConfiguration: Settings
private globalSettings: Settings
// Holds additional command line arguments that are not part of the configuration
private readonly additionalArguments: CliArguments
private hasConfigurationCapability = false
// Map to keep track of callbacks to execute when a specific setting changes
private readonly settingChangeCallbacks: Map<SettingName, (configuration: Settings) => void> = new Map();
constructor () {
const cliArgs = getCliArgs()
this.defaultConfiguration = {
installPath: '',
matlabConnectionTiming: ConnectionTiming.OnStart,
indexWorkspace: false,
telemetry: true,
maxFileSizeForAnalysis: 0,
signIn: false
}
this.globalSettings = {
installPath: cliArgs[Argument.MatlabInstallationPath] ?? this.defaultConfiguration.installPath,
matlabConnectionTiming: cliArgs[Argument.MatlabConnectionTiming] as ConnectionTiming ?? this.defaultConfiguration.matlabConnectionTiming,
indexWorkspace: cliArgs[Argument.ShouldIndexWorkspace] ?? this.defaultConfiguration.indexWorkspace,
telemetry: this.defaultConfiguration.telemetry,
maxFileSizeForAnalysis: this.defaultConfiguration.maxFileSizeForAnalysis,
signIn: this.defaultConfiguration.signIn
}
this.additionalArguments = {
[Argument.MatlabLaunchCommandArguments]: cliArgs[Argument.MatlabLaunchCommandArguments] ?? '',
[Argument.MatlabUrl]: cliArgs[Argument.MatlabUrl] ?? '',
[Argument.SnippetIgnoreList]: cliArgs[Argument.SnippetIgnoreList] ?? ''
}
}
public static getInstance (): ConfigurationManager {
if (ConfigurationManager.instance == null) {
ConfigurationManager.instance = new ConfigurationManager()
}
return ConfigurationManager.instance
}
/**
* Sets up the configuration manager
*
* @param capabilities The client capabilities
*/
setup (capabilities: ClientCapabilities): void {
const connection = ClientConnection.getConnection()
this.hasConfigurationCapability = capabilities.workspace?.configuration != null
if (this.hasConfigurationCapability) {
// Register for configuration changes
void connection.client.register(DidChangeConfigurationNotification.type)
}
connection.onDidChangeConfiguration(params => { void this.handleConfigurationChanged(params) })
}
/**
* Registers a callback for setting changes.
*
* @param settingName - The setting to listen for.
* @param onSettingChangeCallback - The callback invoked on setting change.
* @throws {Error} For invalid setting names.
*/
addSettingCallback (settingName: SettingName, onSettingChangeCallback: (configuration: Settings) => void | Promise<void>): void {
if (this.settingChangeCallbacks.get(settingName) == null) {
this.settingChangeCallbacks.set(settingName, onSettingChangeCallback)
}
}
/**
* Gets the configuration for the langauge server
*
* @returns The current configuration
*/
async getConfiguration (): Promise<Settings> {
if (this.hasConfigurationCapability) {
if (this.configuration == null) {
const connection = ClientConnection.getConnection()
this.configuration = await connection.workspace.getConfiguration('MATLAB') as Settings
}
return Object.assign(this.defaultConfiguration, this.configuration)
}
return Object.assign(this.defaultConfiguration, this.globalSettings)
}
/**
* Gets the value of the given argument
*
* @param argument The argument
* @returns The argument's value
*/
getArgument (argument: Argument.MatlabLaunchCommandArguments | Argument.MatlabUrl | Argument.SnippetIgnoreList): string {
return this.additionalArguments[argument]
}
/**
* Handles a change in the configuration
* @param params The configuration changed params
*/
private async handleConfigurationChanged (params: DidChangeConfigurationParams): Promise<void> {
let oldConfig: Settings | null
let newConfig: Settings
if (this.hasConfigurationCapability) {
oldConfig = this.configuration
// Clear cached configuration
this.configuration = null
// Force load new configuration
newConfig = await this.getConfiguration()
} else {
oldConfig = this.globalSettings
this.globalSettings = params.settings?.matlab ?? this.defaultConfiguration
newConfig = this.globalSettings
}
this.compareSettingChanges(oldConfig, newConfig)
}
private compareSettingChanges (oldConfiguration: Settings | null, newConfiguration: Settings): void {
if (oldConfiguration == null) {
// Not yet initialized
return
}
for (let i = 0; i < SETTING_NAMES.length; i++) {
const settingName = SETTING_NAMES[i]
const oldValue = oldConfiguration[settingName]
const newValue = newConfiguration[settingName]
if (oldValue !== newValue) {
reportTelemetrySettingsChange(settingName, newValue.toString(), oldValue.toString())
// As the setting changed, execute the corresponding callback for it.
const callback = this.settingChangeCallbacks.get(settingName);
if (callback != null) {
callback(newConfiguration)
}
}
}
}
}
export default ConfigurationManager.getInstance()