-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
preference-contribution.ts
413 lines (378 loc) · 17.6 KB
/
preference-contribution.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/********************************************************************************
* 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
********************************************************************************/
import * as Ajv from 'ajv';
import { inject, injectable, interfaces, named, postConstruct } from 'inversify';
import { ContributionProvider, bindContributionProvider, escapeRegExpCharacters, Emitter, Event, Disposable } from '../../common';
import { PreferenceScope } from './preference-scope';
import { PreferenceProvider, PreferenceProviderDataChange } from './preference-provider';
import {
PreferenceSchema, PreferenceSchemaProperties, PreferenceDataSchema, PreferenceItem, PreferenceSchemaProperty, PreferenceDataProperty, JsonType
} from '../../common/preferences/preference-schema';
import { FrontendApplicationConfigProvider } from '../frontend-application-config-provider';
import { FrontendApplicationConfig } from '@theia/application-package/lib/application-props';
import { bindPreferenceConfigurations, PreferenceConfigurations } from './preference-configurations';
export { PreferenceSchema, PreferenceSchemaProperties, PreferenceDataSchema, PreferenceItem, PreferenceSchemaProperty, PreferenceDataProperty, JsonType };
import { Mutable } from '../../common/types';
/* eslint-disable guard-for-in, @typescript-eslint/no-explicit-any */
export const PreferenceContribution = Symbol('PreferenceContribution');
/**
* A {@link PreferenceContribution} allows adding additional custom preferences.
* For this, the {@link PreferenceContribution} has to provide a valid JSON Schema specifying which preferences
* are available including their types and description.
*
* ### Example usage
* ```typescript
* const MyPreferencesSchema: PreferenceSchema = {
* 'type': 'object',
* 'properties': {
* 'myext.decorations.enabled': {
* 'type': 'boolean',
* 'description': 'Show file status',
* 'default': true
* },
* // [...]
* }
* }
* @injectable()
* export class MyPreferenceContribution implements PreferenceContribution{
* schema= MyPreferencesSchema;
* }
* ```
*/
export interface PreferenceContribution {
readonly schema: PreferenceSchema;
}
export function bindPreferenceSchemaProvider(bind: interfaces.Bind): void {
bindPreferenceConfigurations(bind);
bind(PreferenceSchemaProvider).toSelf().inSingletonScope();
bindContributionProvider(bind, PreferenceContribution);
}
export interface OverridePreferenceName {
preferenceName: string
overrideIdentifier: string
}
export namespace OverridePreferenceName {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(arg: any): arg is OverridePreferenceName {
return !!arg && typeof arg === 'object' && 'preferenceName' in arg && 'overrideIdentifier' in arg;
}
}
const OVERRIDE_PROPERTY = '\\[(.*)\\]$';
export const OVERRIDE_PROPERTY_PATTERN = new RegExp(OVERRIDE_PROPERTY);
const OVERRIDE_PATTERN_WITH_SUBSTITUTION = '\\[(${0})\\]$';
/**
* Specialized {@link FrontendApplicationConfig} to configure default
* preference values for the {@link PreferenceSchemaProvider}.
*/
export interface FrontendApplicationPreferenceConfig extends FrontendApplicationConfig {
preferences: {
[preferenceName: string]: any
}
}
export namespace FrontendApplicationPreferenceConfig {
export function is(config: FrontendApplicationConfig): config is FrontendApplicationPreferenceConfig {
return 'preferences' in config && typeof config['preferences'] === 'object';
}
}
/**
* The {@link PreferenceSchemaProvider} collects all {@link PreferenceContribution}s and combines
* the preference schema provided by these contributions into one collective schema. The preferences which
* are provided by this {@link PreferenceProvider} are derived from this combined schema.
*/
@injectable()
export class PreferenceSchemaProvider extends PreferenceProvider {
protected readonly preferences: { [name: string]: any } = {};
protected readonly combinedSchema: PreferenceDataSchema = { properties: {}, patternProperties: {} };
protected readonly workspaceSchema: PreferenceDataSchema = { properties: {}, patternProperties: {} };
protected readonly folderSchema: PreferenceDataSchema = { properties: {}, patternProperties: {} };
@inject(ContributionProvider) @named(PreferenceContribution)
protected readonly preferenceContributions: ContributionProvider<PreferenceContribution>;
@inject(PreferenceConfigurations)
protected readonly configurations: PreferenceConfigurations;
protected readonly onDidPreferenceSchemaChangedEmitter = new Emitter<void>();
readonly onDidPreferenceSchemaChanged: Event<void> = this.onDidPreferenceSchemaChangedEmitter.event;
protected fireDidPreferenceSchemaChanged(): void {
this.onDidPreferenceSchemaChangedEmitter.fire(undefined);
}
@postConstruct()
protected init(): void {
this.preferenceContributions.getContributions().forEach(contrib => {
this.doSetSchema(contrib.schema);
});
this.combinedSchema.additionalProperties = false;
this._ready.resolve();
}
protected readonly overrideIdentifiers = new Set<string>();
/**
* Register a new overrideIdentifier. Existing identifiers are not replaced.
*
* Allows overriding existing values while keeping both values in store.
* For example to store different editor settings, e.g. "[markdown].editor.autoIndent",
* "[json].editor.autoIndent" and "editor.autoIndent"
* @param overrideIdentifier the new overrideIdentifier
*/
registerOverrideIdentifier(overrideIdentifier: string): void {
if (this.overrideIdentifiers.has(overrideIdentifier)) {
return;
}
this.overrideIdentifiers.add(overrideIdentifier);
this.updateOverridePatternPropertiesKey();
}
protected readonly overridePatternProperties: Required<Pick<PreferenceDataProperty, 'properties' | 'additionalProperties'>> & PreferenceDataProperty = {
type: 'object',
description: 'Configure editor settings to be overridden for a language.',
errorMessage: 'Unknown Identifier. Use language identifiers',
properties: {},
additionalProperties: false
};
protected overridePatternPropertiesKey: string | undefined;
protected updateOverridePatternPropertiesKey(): void {
const oldKey = this.overridePatternPropertiesKey;
const newKey = this.computeOverridePatternPropertiesKey();
if (oldKey === newKey) {
return;
}
if (oldKey) {
delete this.combinedSchema.patternProperties[oldKey];
}
this.overridePatternPropertiesKey = newKey;
if (newKey) {
this.combinedSchema.patternProperties[newKey] = this.overridePatternProperties;
}
this.fireDidPreferenceSchemaChanged();
}
protected computeOverridePatternPropertiesKey(): string | undefined {
let param: string = '';
for (const overrideIdentifier of this.overrideIdentifiers.keys()) {
if (param.length) {
param += '|';
}
param += new RegExp(escapeRegExpCharacters(overrideIdentifier)).source;
}
return param.length ? OVERRIDE_PATTERN_WITH_SUBSTITUTION.replace('${0}', param) : undefined;
}
protected doUnsetSchema(changes: PreferenceProviderDataChange[]): PreferenceProviderDataChange[] {
const inverseChanges: PreferenceProviderDataChange[] = [];
for (const change of changes) {
const preferenceName = change.preferenceName;
const overridden = this.overriddenPreferenceName(preferenceName);
if (overridden) {
delete this.overridePatternProperties.properties[`[${overridden.overrideIdentifier}]`];
this.removePropFromSchemas(`[${overridden.overrideIdentifier}]`);
} else {
this.removePropFromSchemas(preferenceName);
}
const newValue = change.oldValue;
const oldValue = change.newValue;
const { scope, domain } = change;
const inverseChange: Mutable<PreferenceProviderDataChange> = { preferenceName, oldValue, scope, domain };
if (typeof newValue === undefined) {
delete this.preferences[preferenceName];
} else {
inverseChange.newValue = newValue;
this.preferences[preferenceName] = newValue;
}
inverseChanges.push(inverseChange);
}
return inverseChanges;
}
protected doSetSchema(schema: PreferenceSchema): PreferenceProviderDataChange[] {
const ajv = new Ajv();
const valid = ajv.validateSchema(schema);
if (!valid) {
const errors = !!ajv.errors ? ajv.errorsText(ajv.errors) : 'unknown validation error';
console.warn('A contributed preference schema has validation issues : ' + errors);
}
const scope = PreferenceScope.Default;
const domain = this.getDomain();
const changes: PreferenceProviderDataChange[] = [];
const defaultScope = PreferenceSchema.getDefaultScope(schema);
const overridable = schema.overridable || false;
for (const preferenceName of Object.keys(schema.properties)) {
if (this.combinedSchema.properties[preferenceName]) {
console.error('Preference name collision detected in the schema for property: ' + preferenceName);
} else {
const schemaProps = PreferenceDataProperty.fromPreferenceSchemaProperty(schema.properties[preferenceName], defaultScope);
if (typeof schemaProps.overridable !== 'boolean' && overridable) {
schemaProps.overridable = true;
}
if (schemaProps.overridable) {
this.overridePatternProperties.properties[preferenceName] = schemaProps;
}
this.updateSchemaProps(preferenceName, schemaProps);
const value = schemaProps.defaultValue = this.getDefaultValue(schemaProps, preferenceName);
if (this.testOverrideValue(preferenceName, value)) {
for (const overriddenPreferenceName in value) {
const overrideValue = value[overriddenPreferenceName];
const overridePreferenceName = `${preferenceName}.${overriddenPreferenceName}`;
changes.push(this.doSetPreferenceValue(overridePreferenceName, overrideValue, { scope, domain }));
}
} else {
changes.push(this.doSetPreferenceValue(preferenceName, value, { scope, domain }));
}
}
}
return changes;
}
protected doSetPreferenceValue(preferenceName: string, newValue: any, { scope, domain }: {
scope: PreferenceScope,
domain?: string[]
}): PreferenceProviderDataChange {
const oldValue = this.preferences[preferenceName];
this.preferences[preferenceName] = newValue;
return { preferenceName, oldValue, newValue, scope, domain };
}
/** @deprecated since 0.6.0 pass preferenceName as the second arg */
protected getDefaultValue(property: PreferenceItem): any;
protected getDefaultValue(property: PreferenceItem, preferenceName: string): any;
protected getDefaultValue(property: PreferenceItem, preferenceName?: string): any {
const config = FrontendApplicationConfigProvider.get();
if (preferenceName && FrontendApplicationPreferenceConfig.is(config) && preferenceName in config.preferences) {
return config.preferences[preferenceName];
}
if (property.defaultValue !== undefined) {
return property.defaultValue;
}
if (property.default !== undefined) {
return property.default;
}
const type = Array.isArray(property.type) ? property.type[0] : property.type;
switch (type) {
case 'boolean':
return false;
case 'integer':
case 'number':
return 0;
case 'string':
return '';
case 'array':
return [];
case 'object':
return {};
}
// eslint-disable-next-line no-null/no-null
return null;
}
getCombinedSchema(): PreferenceDataSchema {
return this.combinedSchema;
}
getSchema(scope: PreferenceScope): PreferenceDataSchema {
switch (scope) {
case PreferenceScope.Default:
case PreferenceScope.User:
return this.combinedSchema;
case PreferenceScope.Workspace:
return this.workspaceSchema;
case PreferenceScope.Folder:
return this.folderSchema;
}
}
setSchema(schema: PreferenceSchema): Disposable {
const changes = this.doSetSchema(schema);
if (!changes.length) {
return Disposable.NULL;
}
this.fireDidPreferenceSchemaChanged();
this.emitPreferencesChangedEvent(changes);
return Disposable.create(() => {
const inverseChanges = this.doUnsetSchema(changes);
if (!inverseChanges.length) {
return;
}
this.fireDidPreferenceSchemaChanged();
this.emitPreferencesChangedEvent(inverseChanges);
});
}
getPreferences(): { [name: string]: any } {
return this.preferences;
}
async setPreference(): Promise<boolean> {
return false;
}
isValidInScope(preferenceName: string, scope: PreferenceScope): boolean {
let property;
const overridden = this.overriddenPreferenceName(preferenceName);
if (overridden) {
// try from overridden schema
property = this.overridePatternProperties[`[${overridden.overrideIdentifier}]`];
property = property && property[overridden.preferenceName];
if (!property) {
// try from overridden identifier
property = this.overridePatternProperties[overridden.preferenceName];
}
if (!property) {
// try from overridden value
property = this.combinedSchema.properties[overridden.preferenceName];
}
} else {
property = this.combinedSchema.properties[preferenceName];
}
return property && property.scope! >= scope;
}
*getPreferenceNames(): IterableIterator<string> {
for (const preferenceName in this.combinedSchema.properties) {
yield preferenceName;
for (const overridePreferenceName of this.getOverridePreferenceNames(preferenceName)) {
yield overridePreferenceName;
}
}
}
*getOverridePreferenceNames(preferenceName: string): IterableIterator<string> {
const preference = this.combinedSchema.properties[preferenceName];
if (preference && preference.overridable) {
for (const overrideIdentifier of this.overrideIdentifiers) {
yield this.overridePreferenceName({ preferenceName, overrideIdentifier });
}
}
}
overridePreferenceName({ preferenceName, overrideIdentifier }: OverridePreferenceName): string {
return `[${overrideIdentifier}].${preferenceName}`;
}
overriddenPreferenceName(name: string): OverridePreferenceName | undefined {
const index = name.indexOf('.');
if (index === -1) {
return undefined;
}
const matches = name.substr(0, index).match(OVERRIDE_PROPERTY_PATTERN);
const overrideIdentifier = matches && matches[1];
if (!overrideIdentifier || !this.overrideIdentifiers.has(overrideIdentifier)) {
return undefined;
}
const preferenceName = name.substr(index + 1);
return { preferenceName, overrideIdentifier };
}
testOverrideValue(name: string, value: any): value is PreferenceSchemaProperties {
return PreferenceSchemaProperties.is(value) && OVERRIDE_PROPERTY_PATTERN.test(name);
}
protected updateSchemaProps(key: string, property: PreferenceDataProperty): void {
this.combinedSchema.properties[key] = property;
switch (property.scope) {
case PreferenceScope.Folder:
this.folderSchema.properties[key] = property;
// Fall through. isValidInScope implies that User ⊃ Workspace ⊃ Folder,
// so anything we add to folder should be added to workspace, but not vice versa.
case PreferenceScope.Workspace:
this.workspaceSchema.properties[key] = property;
break;
}
}
protected removePropFromSchemas(key: string): void {
// If we remove a key from combined, it should also be removed from all narrower scopes.
delete this.combinedSchema.properties[key];
delete this.workspaceSchema.properties[key];
delete this.folderSchema.properties[key];
}
}