-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathabstract-resource-preference-provider.ts
273 lines (243 loc) · 11.1 KB
/
abstract-resource-preference-provider.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/********************************************************************************
* Copyright (C) 2018 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable no-null/no-null */
import * as jsoncparser from 'jsonc-parser';
import { JSONExt } from '@phosphor/coreutils/lib/json';
import { inject, injectable, postConstruct } from 'inversify';
import { MessageService } from '@theia/core/lib/common/message-service';
import { Disposable } from '@theia/core/lib/common/disposable';
import { PreferenceProvider, PreferenceSchemaProvider, PreferenceScope, PreferenceProviderDataChange, PreferenceService } from '@theia/core/lib/browser';
import URI from '@theia/core/lib/common/uri';
import { PreferenceConfigurations } from '@theia/core/lib/browser/preferences/preference-configurations';
import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';
import { MonacoEditorModel } from '@theia/monaco/lib/browser/monaco-editor-model';
import { MonacoWorkspace } from '@theia/monaco/lib/browser/monaco-workspace';
import { Deferred } from '@theia/core/lib/common/promise-util';
@injectable()
export abstract class AbstractResourcePreferenceProvider extends PreferenceProvider {
protected preferences: { [key: string]: any } = {};
protected model: MonacoEditorModel | undefined;
protected readonly loading = new Deferred();
@inject(PreferenceService) protected readonly preferenceService: PreferenceService;
@inject(MessageService) protected readonly messageService: MessageService;
@inject(PreferenceSchemaProvider) protected readonly schemaProvider: PreferenceSchemaProvider;
@inject(PreferenceConfigurations)
protected readonly configurations: PreferenceConfigurations;
@inject(MonacoTextModelService)
protected readonly textModelService: MonacoTextModelService;
@inject(MonacoWorkspace)
protected readonly workspace: MonacoWorkspace;
@postConstruct()
protected async init(): Promise<void> {
const uri = this.getUri();
this.toDispose.push(Disposable.create(() => this.loading.reject(new Error(`preference provider for '${uri}' was disposed`))));
this._ready.resolve();
const reference = await this.textModelService.createModelReference(uri);
if (this.toDispose.disposed) {
reference.dispose();
return;
}
this.model = reference.object;
this.loading.resolve();
this.toDispose.push(reference);
this.toDispose.push(Disposable.create(() => this.model = undefined));
this.readPreferences();
this.toDispose.push(this.model.onDidChangeContent(() => this.readPreferences()));
this.toDispose.push(this.model.onDirtyChanged(() => this.readPreferences()));
this.toDispose.push(this.model.onDidChangeValid(() => this.readPreferences()));
this.toDispose.push(Disposable.create(() => this.reset()));
}
protected abstract getUri(): URI;
protected abstract getScope(): PreferenceScope;
protected get valid(): boolean {
return this.model && this.model.valid || false;
}
getConfigUri(): URI;
getConfigUri(resourceUri: string | undefined): URI | undefined;
getConfigUri(resourceUri?: string): URI | undefined {
if (!resourceUri) {
return this.getUri();
}
return this.valid && this.contains(resourceUri) ? this.getUri() : undefined;
}
contains(resourceUri: string | undefined): boolean {
if (!resourceUri) {
return true;
}
const domain = this.getDomain();
if (!domain) {
return true;
}
const resourcePath = new URI(resourceUri).path;
return domain.some(uri => new URI(uri).path.relativity(resourcePath) >= 0);
}
getPreferences(resourceUri?: string): { [key: string]: any } {
return this.valid && this.contains(resourceUri) ? this.preferences : {};
}
async setPreference(key: string, value: any, resourceUri?: string): Promise<boolean> {
await this.loading.promise;
if (!this.model) {
return false;
}
if (!this.contains(resourceUri)) {
return false;
}
const path = this.getPath(key);
if (!path) {
return false;
}
try {
const content = this.model.getText().trim();
if (!content && value === undefined) {
return true;
}
const textModel = this.model.textEditorModel;
const editOperations: monaco.editor.IIdentifiedSingleEditOperation[] = [];
if (path.length || value !== undefined) {
const { insertSpaces, tabSize, defaultEOL } = textModel.getOptions();
for (const edit of jsoncparser.modify(content, path, value, {
formattingOptions: {
insertSpaces,
tabSize,
eol: defaultEOL === monaco.editor.DefaultEndOfLine.LF ? '\n' : '\r\n'
}
})) {
const start = textModel.getPositionAt(edit.offset);
const end = textModel.getPositionAt(edit.offset + edit.length);
editOperations.push({
range: monaco.Range.fromPositions(start, end),
text: edit.content || null,
forceMoveMarkers: false
});
}
} else {
editOperations.push({
range: textModel.getFullModelRange(),
text: null,
forceMoveMarkers: false
});
}
await this.workspace.applyBackgroundEdit(this.model, editOperations);
return await this.pendingChanges;
} catch (e) {
const message = `Failed to update the value of '${key}' in '${this.getUri()}'.`;
this.messageService.error(`${message} Please check if it is corrupted.`);
console.error(`${message}`, e);
return false;
}
}
protected getPath(preferenceName: string): string[] | undefined {
return [preferenceName];
}
/**
* It HAS to be sync to ensure that `setPreference` returns only when values are updated
* or any other operation modifying the monaco model content.
*/
protected readPreferences(): void {
const model = this.model;
if (!model || model.dirty) {
return;
}
try {
let preferences;
if (model.valid) {
const content = model.getText();
preferences = this.getParsedContent(content);
} else {
preferences = {};
}
this.handlePreferenceChanges(preferences);
} catch (e) {
console.error(`Failed to load preferences from '${this.getUri()}'.`, e);
}
}
protected getParsedContent(content: string): { [key: string]: any } {
const jsonData = this.parse(content);
const preferences: { [key: string]: any } = {};
if (typeof jsonData !== 'object') {
return preferences;
}
// eslint-disable-next-line guard-for-in
for (const preferenceName in jsonData) {
const preferenceValue = jsonData[preferenceName];
if (this.schemaProvider.testOverrideValue(preferenceName, preferenceValue)) {
// eslint-disable-next-line guard-for-in
for (const overriddenPreferenceName in preferenceValue) {
const overriddenValue = preferenceValue[overriddenPreferenceName];
preferences[`${preferenceName}.${overriddenPreferenceName}`] = overriddenValue;
}
} else {
preferences[preferenceName] = preferenceValue;
}
}
return preferences;
}
protected parse(content: string): any {
content = content.trim();
if (!content) {
return undefined;
}
const strippedContent = jsoncparser.stripComments(content);
return jsoncparser.parse(strippedContent);
}
protected handlePreferenceChanges(newPrefs: { [key: string]: any }): void {
const oldPrefs = Object.assign({}, this.preferences);
this.preferences = newPrefs;
const prefNames = new Set([...Object.keys(oldPrefs), ...Object.keys(newPrefs)]);
const prefChanges: PreferenceProviderDataChange[] = [];
const uri = this.getUri();
for (const prefName of prefNames.values()) {
const oldValue = oldPrefs[prefName];
const newValue = newPrefs[prefName];
const schemaProperties = this.schemaProvider.getCombinedSchema().properties[prefName];
if (schemaProperties) {
const scope = schemaProperties.scope;
// do not emit the change event if the change is made out of the defined preference scope
if (!this.schemaProvider.isValidInScope(prefName, this.getScope())) {
console.warn(`Preference ${prefName} in ${uri} can only be defined in scopes: ${PreferenceScope.getScopeNames(scope).join(', ')}.`);
continue;
}
}
if (newValue === undefined && oldValue !== newValue
|| oldValue === undefined && newValue !== oldValue // JSONExt.deepEqual() does not support handling `undefined`
|| !JSONExt.deepEqual(oldValue, newValue)) {
prefChanges.push({
preferenceName: prefName, newValue, oldValue, scope: this.getScope(), domain: this.getDomain()
});
}
}
if (prefChanges.length > 0) { // do not emit the change event if the pref value is not changed
this.emitPreferencesChangedEvent(prefChanges);
}
}
protected reset(): void {
const preferences = this.preferences;
this.preferences = {};
const changes: PreferenceProviderDataChange[] = [];
for (const prefName of Object.keys(preferences)) {
const value = preferences[prefName];
if (value !== undefined) {
changes.push({
preferenceName: prefName, newValue: undefined, oldValue: value, scope: this.getScope(), domain: this.getDomain()
});
}
}
if (changes.length > 0) {
this.emitPreferencesChangedEvent(changes);
}
}
}