-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathpreference-registry.ts
253 lines (231 loc) · 10.6 KB
/
preference-registry.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
/********************************************************************************
* Copyright (C) 2018 Red Hat, Inc. 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
********************************************************************************/
/* tslint:disable:no-any */
import { Emitter, Event } from '@theia/core/lib/common/event';
import * as theia from '@theia/plugin';
import {
PLUGIN_RPC_CONTEXT,
PreferenceRegistryExt,
PreferenceRegistryMain,
PreferenceData
} from '../api/plugin-api';
import { RPCProtocol } from '../api/rpc-protocol';
import { isObject } from '../common/types';
import { PreferenceChange } from '@theia/core/lib/browser';
import { Configuration, ConfigurationModelImpl } from './preferences/configuration';
import { WorkspaceExtImpl } from './workspace';
import cloneDeep = require('lodash.clonedeep');
enum ConfigurationTarget {
Global = 1,
Workspace = 2,
WorkspaceFolder = 3
}
enum PreferenceScope {
Default,
User,
Workspace,
}
interface ConfigurationInspect<T> {
key: string;
defaultValue?: T;
globalValue?: T;
workspaceValue?: T;
workspaceFolderValue?: T;
}
// tslint:disable-next-line:no-any
function lookUp(tree: any, key: string): any {
if (!key) {
return;
}
const parts = key.split('.');
let node = tree;
for (let i = 0; node && i < parts.length; i++) {
node = node[parts[i]];
}
return node;
}
export class PreferenceRegistryExtImpl implements PreferenceRegistryExt {
private proxy: PreferenceRegistryMain;
private _preferences: Configuration;
private readonly _onDidChangeConfiguration = new Emitter<theia.ConfigurationChangeEvent>();
readonly onDidChangeConfiguration: Event<theia.ConfigurationChangeEvent> = this._onDidChangeConfiguration.event;
constructor(
rpc: RPCProtocol,
private readonly workspace: WorkspaceExtImpl
) {
this.proxy = rpc.getProxy(PLUGIN_RPC_CONTEXT.PREFERENCE_REGISTRY_MAIN);
}
init(data: PreferenceData): void {
this._preferences = this.parse(data);
}
$acceptConfigurationChanged(data: PreferenceData, eventData: PreferenceChange): void {
this.init(data);
this._onDidChangeConfiguration.fire(this.toConfigurationChangeEvent(eventData));
}
getConfiguration(section?: string, resource?: theia.Uri | null, extensionId?: string): theia.WorkspaceConfiguration {
resource = resource === null ? undefined : resource;
const preferences = this.toReadonlyValue(section
? lookUp(this._preferences.getValue(undefined!, { resource }, this.workspace), section)
: this._preferences.getValue(undefined!, { resource }, this.workspace));
const configuration: theia.WorkspaceConfiguration = {
has(key: string): boolean {
return typeof lookUp(preferences, key) !== 'undefined';
},
get: <T>(key: string, defaultValue?: T) => {
const result = lookUp(preferences, key);
if (typeof result === 'undefined') {
return defaultValue;
} else {
let clonedConfig: any = undefined;
const cloneOnWriteProxy = (target: any, accessor: string): any => {
let clonedTarget: any = undefined;
const cloneTarget = () => {
clonedConfig = clonedConfig ? clonedConfig : cloneDeep(preferences);
clonedTarget = clonedTarget ? cloneTarget : lookUp(clonedConfig, accessor);
};
if (!isObject(target)) {
return target;
}
return new Proxy(target, {
get: (targ: any, prop: string) => {
if (typeof prop === 'string' && prop.toLowerCase() === 'tojson') {
cloneTarget();
return () => clonedTarget;
}
if (clonedConfig) {
clonedTarget = cloneTarget ? cloneTarget : lookUp(clonedConfig, accessor);
return clonedTarget[prop];
}
const res = targ[prop];
if (typeof prop === 'string') {
return cloneOnWriteProxy(res, `${accessor}.${prop}`);
}
return res;
},
set: (targ: any, prop: string, val: any) => {
cloneTarget();
clonedTarget[prop] = val;
return true;
},
deleteProperty: (targ: any, prop: string) => {
cloneTarget();
delete clonedTarget[prop];
return true;
},
defineProperty: (targ: any, prop: string, descr: any) => {
cloneTarget();
Object.defineProperty(clonedTarget, prop, descr);
return true;
}
});
};
return cloneOnWriteProxy(result, key);
}
},
update: (key: string, value: any, arg?: ConfigurationTarget | boolean): PromiseLike<void> => {
key = section ? `${section}.${key}` : key;
if (typeof value !== 'undefined') {
return this.proxy.$updateConfigurationOption(arg, key, value, resource);
} else {
return this.proxy.$removeConfigurationOption(arg, key, resource);
}
},
inspect: <T>(key: string): ConfigurationInspect<T> => {
key = section ? `${section}.${key}` : key;
resource = resource === null ? undefined : resource;
const result = cloneDeep(this._preferences.inspect<T>(key, { resource }, this.workspace));
if (!result) {
return undefined!;
}
const configInspect: ConfigurationInspect<T> = { key };
if (result.default) {
configInspect.defaultValue = result.default;
}
if (result.user) {
configInspect.globalValue = result.user;
}
if (result.workspace) {
configInspect.workspaceValue = result.workspace;
}
if (result.workspaceFolder) {
configInspect.workspaceFolderValue = result.workspaceFolder;
}
return configInspect;
}
};
return configuration;
}
private toReadonlyValue(data: any): any {
const readonlyProxy = (target: any): any => isObject(target)
? new Proxy(target, {
get: (targ: any, prop: string) => readonlyProxy(targ[prop]),
set: (targ: any, prop: string, val: any) => {
throw new Error(`TypeError: Cannot assign to read only property '${prop}' of object`);
},
deleteProperty: (targ: any, prop: string) => {
throw new Error(`TypeError: Cannot delete read only property '${prop}' of object`);
},
defineProperty: (targ: any, prop: string) => {
throw new Error(`TypeError: Cannot define property '${prop}' of a readonly object`);
},
setPrototypeOf: (targ: any) => {
throw new Error('TypeError: Cannot set prototype for a readonly object');
},
isExtensible: () => false,
preventExtensions: () => true
})
: target;
return readonlyProxy(data);
}
private parse(data: PreferenceData): Configuration {
const defaultConfigurationModel = new ConfigurationModelImpl(this.parseConfigurationData(data[PreferenceScope.Default]), Object.keys(data[PreferenceScope.Default]));
const userConfiguration = new ConfigurationModelImpl(this.parseConfigurationData(data[PreferenceScope.User]), Object.keys(data[PreferenceScope.User]));
const workspaceConfiguration = new ConfigurationModelImpl(this.parseConfigurationData(data[PreferenceScope.Workspace]), Object.keys(data[PreferenceScope.Workspace]));
return new Configuration(defaultConfigurationModel, userConfiguration, workspaceConfiguration);
}
private parseConfigurationData(data: { [key: string]: any }): { [key: string]: any } {
let res;
try {
res = Object.keys(data).reduce((result: any, key: string) => {
const parts = key.split('.');
let branch = result;
for (let i = 0; i < parts.length; i++) {
if (i === parts.length - 1) {
branch[parts[i]] = data[key];
continue;
}
if (!branch[parts[i]]) {
branch[parts[i]] = {};
}
branch = branch[parts[i]];
}
return result;
}, {});
} catch (e) { }
return res;
}
private toConfigurationChangeEvent(eventData: PreferenceChange): theia.ConfigurationChangeEvent {
return Object.freeze({
affectsConfiguration: (section: string, uri?: theia.Uri): boolean => {
const tree = eventData.preferenceName
.split('.')
.reverse()
.reduce((prevValue: any, curValue: any) => ({ [curValue]: prevValue }), eventData.newValue);
return !!lookUp(tree, section);
}
});
}
}