@theia/preferences 1.45.1 → 1.46.0-next.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -81
- package/lib/browser/abstract-resource-preference-provider.d.ts +47 -47
- package/lib/browser/abstract-resource-preference-provider.js +240 -240
- package/lib/browser/abstract-resource-preference-provider.spec.d.ts +1 -1
- package/lib/browser/abstract-resource-preference-provider.spec.js +83 -83
- package/lib/browser/folder-preference-provider.d.ts +20 -20
- package/lib/browser/folder-preference-provider.js +59 -59
- package/lib/browser/folders-preferences-provider.d.ts +27 -27
- package/lib/browser/folders-preferences-provider.js +245 -245
- package/lib/browser/index.d.ts +7 -7
- package/lib/browser/index.js +34 -34
- package/lib/browser/monaco-jsonc-editor.d.ts +9 -9
- package/lib/browser/monaco-jsonc-editor.js +80 -80
- package/lib/browser/package.spec.js +25 -25
- package/lib/browser/preference-bindings.d.ts +4 -4
- package/lib/browser/preference-bindings.js +63 -63
- package/lib/browser/preference-frontend-module.d.ts +6 -6
- package/lib/browser/preference-frontend-module.js +52 -52
- package/lib/browser/preference-open-handler.d.ts +9 -9
- package/lib/browser/preference-open-handler.js +64 -64
- package/lib/browser/preference-transaction-manager.d.ts +100 -100
- package/lib/browser/preference-transaction-manager.js +293 -293
- package/lib/browser/preference-tree-model.d.ts +60 -60
- package/lib/browser/preference-tree-model.js +243 -243
- package/lib/browser/preferences-contribution.d.ts +37 -37
- package/lib/browser/preferences-contribution.js +280 -280
- package/lib/browser/preferences-json-schema-contribution.d.ts +17 -17
- package/lib/browser/preferences-json-schema-contribution.js +91 -91
- package/lib/browser/preferences-monaco-contribution.d.ts +1 -1
- package/lib/browser/preferences-monaco-contribution.js +27 -27
- package/lib/browser/section-preference-provider.d.ts +21 -21
- package/lib/browser/section-preference-provider.js +96 -96
- package/lib/browser/user-configs-preference-provider.d.ts +22 -22
- package/lib/browser/user-configs-preference-provider.js +137 -137
- package/lib/browser/user-preference-provider.d.ts +13 -13
- package/lib/browser/user-preference-provider.js +41 -41
- package/lib/browser/util/preference-scope-command-manager.d.ts +17 -17
- package/lib/browser/util/preference-scope-command-manager.js +87 -87
- package/lib/browser/util/preference-tree-generator.d.ts +31 -31
- package/lib/browser/util/preference-tree-generator.js +237 -237
- package/lib/browser/util/preference-tree-label-provider.d.ts +11 -11
- package/lib/browser/util/preference-tree-label-provider.js +77 -77
- package/lib/browser/util/preference-tree-label-provider.spec.d.ts +1 -1
- package/lib/browser/util/preference-tree-label-provider.spec.js +87 -87
- package/lib/browser/util/preference-types.d.ts +62 -62
- package/lib/browser/util/preference-types.js +128 -128
- package/lib/browser/views/components/preference-array-input.d.ts +28 -28
- package/lib/browser/views/components/preference-array-input.js +180 -180
- package/lib/browser/views/components/preference-boolean-input.d.ts +17 -17
- package/lib/browser/views/components/preference-boolean-input.js +79 -79
- package/lib/browser/views/components/preference-file-input.d.ts +29 -29
- package/lib/browser/views/components/preference-file-input.js +110 -110
- package/lib/browser/views/components/preference-json-input.d.ts +19 -19
- package/lib/browser/views/components/preference-json-input.js +93 -93
- package/lib/browser/views/components/preference-markdown-renderer.d.ts +12 -12
- package/lib/browser/views/components/preference-markdown-renderer.js +81 -81
- package/lib/browser/views/components/preference-node-renderer-creator.d.ts +48 -48
- package/lib/browser/views/components/preference-node-renderer-creator.js +132 -132
- package/lib/browser/views/components/preference-node-renderer.d.ts +112 -112
- package/lib/browser/views/components/preference-node-renderer.js +441 -441
- package/lib/browser/views/components/preference-number-input.d.ts +34 -34
- package/lib/browser/views/components/preference-number-input.js +142 -142
- package/lib/browser/views/components/preference-select-input.d.ts +28 -28
- package/lib/browser/views/components/preference-select-input.js +138 -138
- package/lib/browser/views/components/preference-string-input.d.ts +17 -17
- package/lib/browser/views/components/preference-string-input.js +89 -89
- package/lib/browser/views/preference-editor-widget.d.ts +67 -67
- package/lib/browser/views/preference-editor-widget.js +363 -363
- package/lib/browser/views/preference-scope-tabbar-widget.d.ts +54 -54
- package/lib/browser/views/preference-scope-tabbar-widget.js +343 -343
- package/lib/browser/views/preference-searchbar-widget.d.ts +53 -53
- package/lib/browser/views/preference-searchbar-widget.js +173 -173
- package/lib/browser/views/preference-tree-widget.d.ts +17 -17
- package/lib/browser/views/preference-tree-widget.js +104 -104
- package/lib/browser/views/preference-widget-bindings.d.ts +3 -3
- package/lib/browser/views/preference-widget-bindings.js +87 -87
- package/lib/browser/views/preference-widget.d.ts +36 -36
- package/lib/browser/views/preference-widget.js +126 -126
- package/lib/browser/workspace-file-preference-provider.d.ts +23 -23
- package/lib/browser/workspace-file-preference-provider.js +110 -110
- package/lib/browser/workspace-preference-provider.d.ts +28 -28
- package/lib/browser/workspace-preference-provider.js +142 -142
- package/package.json +10 -10
- package/src/browser/abstract-resource-preference-provider.spec.ts +95 -95
- package/src/browser/abstract-resource-preference-provider.ts +232 -232
- package/src/browser/folder-preference-provider.ts +58 -58
- package/src/browser/folders-preferences-provider.ts +244 -244
- package/src/browser/index.ts +23 -23
- package/src/browser/monaco-jsonc-editor.ts +67 -67
- package/src/browser/package.spec.ts +28 -28
- package/src/browser/preference-bindings.ts +65 -65
- package/src/browser/preference-frontend-module.ts +57 -57
- package/src/browser/preference-open-handler.ts +53 -53
- package/src/browser/preference-transaction-manager.ts +287 -287
- package/src/browser/preference-tree-model.ts +250 -250
- package/src/browser/preferences-contribution.ts +263 -263
- package/src/browser/preferences-json-schema-contribution.ts +86 -86
- package/src/browser/preferences-monaco-contribution.ts +27 -27
- package/src/browser/section-preference-provider.ts +83 -83
- package/src/browser/style/index.css +506 -506
- package/src/browser/style/preference-array.css +94 -94
- package/src/browser/style/preference-context-menu.css +74 -74
- package/src/browser/style/preference-file.css +31 -31
- package/src/browser/style/preference-object.css +49 -49
- package/src/browser/style/search-input.css +66 -66
- package/src/browser/user-configs-preference-provider.ts +127 -127
- package/src/browser/user-preference-provider.ts +35 -35
- package/src/browser/util/preference-scope-command-manager.ts +75 -75
- package/src/browser/util/preference-tree-generator.ts +226 -226
- package/src/browser/util/preference-tree-label-provider.spec.ts +108 -108
- package/src/browser/util/preference-tree-label-provider.ts +64 -64
- package/src/browser/util/preference-types.ts +169 -169
- package/src/browser/views/components/preference-array-input.ts +174 -174
- package/src/browser/views/components/preference-boolean-input.ts +69 -69
- package/src/browser/views/components/preference-file-input.ts +104 -104
- package/src/browser/views/components/preference-json-input.ts +78 -78
- package/src/browser/views/components/preference-markdown-renderer.ts +68 -68
- package/src/browser/views/components/preference-node-renderer-creator.ts +141 -141
- package/src/browser/views/components/preference-node-renderer.ts +477 -477
- package/src/browser/views/components/preference-number-input.ts +147 -147
- package/src/browser/views/components/preference-select-input.ts +131 -131
- package/src/browser/views/components/preference-string-input.ts +76 -76
- package/src/browser/views/preference-editor-widget.ts +349 -349
- package/src/browser/views/preference-scope-tabbar-widget.tsx +344 -344
- package/src/browser/views/preference-searchbar-widget.tsx +183 -183
- package/src/browser/views/preference-tree-widget.tsx +93 -93
- package/src/browser/views/preference-widget-bindings.ts +102 -102
- package/src/browser/views/preference-widget.tsx +117 -117
- package/src/browser/workspace-file-preference-provider.ts +100 -100
- package/src/browser/workspace-preference-provider.ts +134 -134
|
@@ -1,287 +1,287 @@
|
|
|
1
|
-
// *****************************************************************************
|
|
2
|
-
// Copyright (C) 2021 Ericsson and others.
|
|
3
|
-
//
|
|
4
|
-
// This program and the accompanying materials are made available under the
|
|
5
|
-
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
-
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
-
//
|
|
8
|
-
// This Source Code may also be made available under the following Secondary
|
|
9
|
-
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
-
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
-
// with the GNU Classpath Exception which is available at
|
|
12
|
-
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
-
//
|
|
14
|
-
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
-
// *****************************************************************************
|
|
16
|
-
|
|
17
|
-
import { CancellationError, Emitter, Event, MaybePromise, MessageService, nls, WaitUntilEvent } from '@theia/core';
|
|
18
|
-
import { Deferred } from '@theia/core/lib/common/promise-util';
|
|
19
|
-
import { inject, injectable, interfaces, postConstruct } from '@theia/core/shared/inversify';
|
|
20
|
-
import { PreferenceScope } from '@theia/core/lib/common/preferences/preference-scope';
|
|
21
|
-
import URI from '@theia/core/lib/common/uri';
|
|
22
|
-
import { MonacoEditorModel } from '@theia/monaco/lib/browser/monaco-editor-model';
|
|
23
|
-
import { Mutex, MutexInterface } from 'async-mutex';
|
|
24
|
-
import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';
|
|
25
|
-
import { MonacoJSONCEditor } from './monaco-jsonc-editor';
|
|
26
|
-
import { EditorManager } from '@theia/editor/lib/browser/editor-manager';
|
|
27
|
-
import { IReference } from '@theia/monaco-editor-core/esm/vs/base/common/lifecycle';
|
|
28
|
-
|
|
29
|
-
export interface OnWillConcludeEvent<T> extends WaitUntilEvent {
|
|
30
|
-
status: T | false;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
@injectable()
|
|
34
|
-
/**
|
|
35
|
-
* Represents a batch of interactions with an underlying resource.
|
|
36
|
-
*/
|
|
37
|
-
export abstract class Transaction<Arguments extends unknown[], Result = unknown, Status = unknown> {
|
|
38
|
-
protected _open = true;
|
|
39
|
-
/**
|
|
40
|
-
* Whether the transaction is still accepting new interactions.
|
|
41
|
-
* Enqueueing an action when the Transaction is no longer open will throw an error.
|
|
42
|
-
*/
|
|
43
|
-
get open(): boolean {
|
|
44
|
-
return this._open;
|
|
45
|
-
}
|
|
46
|
-
protected _result = new Deferred<Result | false>();
|
|
47
|
-
/**
|
|
48
|
-
* The status of the transaction when complete.
|
|
49
|
-
*/
|
|
50
|
-
get result(): Promise<Result | false> {
|
|
51
|
-
return this._result.promise;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* The transaction will self-dispose when the queue is empty, once at least one action has been processed.
|
|
55
|
-
*/
|
|
56
|
-
protected readonly queue = new Mutex(new CancellationError());
|
|
57
|
-
protected readonly onWillConcludeEmitter = new Emitter<OnWillConcludeEvent<Status>>();
|
|
58
|
-
/**
|
|
59
|
-
* An event fired when the transaction is wrapping up.
|
|
60
|
-
* Consumers can call `waitUntil` on the event to delay the resolution of the `result` Promise.
|
|
61
|
-
*/
|
|
62
|
-
get onWillConclude(): Event<OnWillConcludeEvent<Status>> {
|
|
63
|
-
return this.onWillConcludeEmitter.event;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
protected status = new Deferred<Status>();
|
|
67
|
-
/**
|
|
68
|
-
* Whether any actions have been added to the transaction.
|
|
69
|
-
* The Transaction will not self-dispose until at least one action has been performed.
|
|
70
|
-
*/
|
|
71
|
-
protected inUse = false;
|
|
72
|
-
|
|
73
|
-
@postConstruct()
|
|
74
|
-
protected init(): void {
|
|
75
|
-
this.doInit();
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
protected async doInit(): Promise<void> {
|
|
79
|
-
const release = await this.queue.acquire();
|
|
80
|
-
try {
|
|
81
|
-
const status = await this.setUp();
|
|
82
|
-
this.status.resolve(status);
|
|
83
|
-
} catch {
|
|
84
|
-
this.dispose();
|
|
85
|
-
} finally {
|
|
86
|
-
release();
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
async waitFor(delay?: Promise<unknown>, disposeIfRejected?: boolean): Promise<void> {
|
|
91
|
-
try {
|
|
92
|
-
await this.queue.runExclusive(() => delay);
|
|
93
|
-
} catch {
|
|
94
|
-
if (disposeIfRejected) {
|
|
95
|
-
this.dispose();
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* @returns a promise reflecting the result of performing an action. Typically the promise will not resolve until the whole transaction is complete.
|
|
102
|
-
*/
|
|
103
|
-
async enqueueAction(...args: Arguments): Promise<Result | false> {
|
|
104
|
-
if (this._open) {
|
|
105
|
-
let release: MutexInterface.Releaser | undefined;
|
|
106
|
-
try {
|
|
107
|
-
release = await this.queue.acquire();
|
|
108
|
-
if (!this.inUse) {
|
|
109
|
-
this.inUse = true;
|
|
110
|
-
this.disposeWhenDone();
|
|
111
|
-
}
|
|
112
|
-
return this.act(...args);
|
|
113
|
-
} catch (e) {
|
|
114
|
-
if (e instanceof CancellationError) {
|
|
115
|
-
throw e;
|
|
116
|
-
}
|
|
117
|
-
return false;
|
|
118
|
-
} finally {
|
|
119
|
-
release?.();
|
|
120
|
-
}
|
|
121
|
-
} else {
|
|
122
|
-
throw new Error('Transaction used after disposal.');
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
protected disposeWhenDone(): void {
|
|
127
|
-
// Due to properties of the micro task system, it's possible for something to have been enqueued between
|
|
128
|
-
// the resolution of the waitForUnlock() promise and the the time this code runs, so we have to check.
|
|
129
|
-
this.queue.waitForUnlock().then(() => {
|
|
130
|
-
if (!this.queue.isLocked()) {
|
|
131
|
-
this.dispose();
|
|
132
|
-
} else {
|
|
133
|
-
this.disposeWhenDone();
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
protected async conclude(): Promise<void> {
|
|
139
|
-
if (this._open) {
|
|
140
|
-
try {
|
|
141
|
-
this._open = false;
|
|
142
|
-
this.queue.cancel();
|
|
143
|
-
const result = await this.tearDown();
|
|
144
|
-
const status = this.status.state === 'unresolved' || this.status.state === 'rejected' ? false : await this.status.promise;
|
|
145
|
-
await WaitUntilEvent.fire(this.onWillConcludeEmitter, { status });
|
|
146
|
-
this.onWillConcludeEmitter.dispose();
|
|
147
|
-
this._result.resolve(result);
|
|
148
|
-
} catch {
|
|
149
|
-
this._result.resolve(false);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
dispose(): void {
|
|
155
|
-
this.conclude();
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Runs any code necessary to initialize the batch of interactions. No interaction will be run until the setup is complete.
|
|
160
|
-
*
|
|
161
|
-
* @returns a representation of the success of setup specific to a given transaction implementation.
|
|
162
|
-
*/
|
|
163
|
-
protected abstract setUp(): MaybePromise<Status>;
|
|
164
|
-
/**
|
|
165
|
-
* Performs a single interaction
|
|
166
|
-
*
|
|
167
|
-
* @returns the result of that interaction, specific to a given transaction type.
|
|
168
|
-
*/
|
|
169
|
-
protected abstract act(...args: Arguments): MaybePromise<Result>;
|
|
170
|
-
/**
|
|
171
|
-
* Runs any code necessary to complete a transaction and release any resources it holds.
|
|
172
|
-
*
|
|
173
|
-
* @returns implementation-specific information about the success of the transaction. Will be used as the final status of the transaction.
|
|
174
|
-
*/
|
|
175
|
-
protected abstract tearDown(): MaybePromise<Result>;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export interface PreferenceContext {
|
|
179
|
-
getConfigUri(): URI;
|
|
180
|
-
getScope(): PreferenceScope;
|
|
181
|
-
}
|
|
182
|
-
export const PreferenceContext = Symbol('PreferenceContext');
|
|
183
|
-
export const PreferenceTransactionPreludeProvider = Symbol('PreferenceTransactionPreludeProvider');
|
|
184
|
-
export type PreferenceTransactionPreludeProvider = () => Promise<unknown>;
|
|
185
|
-
|
|
186
|
-
@injectable()
|
|
187
|
-
export class PreferenceTransaction extends Transaction<[string, string[], unknown], boolean> {
|
|
188
|
-
reference: IReference<MonacoEditorModel> | undefined;
|
|
189
|
-
@inject(PreferenceContext) protected readonly context: PreferenceContext;
|
|
190
|
-
@inject(PreferenceTransactionPreludeProvider) protected readonly prelude?: PreferenceTransactionPreludeProvider;
|
|
191
|
-
@inject(MonacoTextModelService) protected readonly textModelService: MonacoTextModelService;
|
|
192
|
-
@inject(MonacoJSONCEditor) protected readonly jsoncEditor: MonacoJSONCEditor;
|
|
193
|
-
@inject(MessageService) protected readonly messageService: MessageService;
|
|
194
|
-
@inject(EditorManager) protected readonly editorManager: EditorManager;
|
|
195
|
-
|
|
196
|
-
protected override async doInit(): Promise<void> {
|
|
197
|
-
this.waitFor(this.prelude?.());
|
|
198
|
-
await super.doInit();
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
protected async setUp(): Promise<boolean> {
|
|
202
|
-
const reference = await this.textModelService.createModelReference(this.context.getConfigUri()!);
|
|
203
|
-
if (this._open) {
|
|
204
|
-
this.reference = reference;
|
|
205
|
-
} else {
|
|
206
|
-
reference.dispose();
|
|
207
|
-
return false;
|
|
208
|
-
}
|
|
209
|
-
if (reference.object.dirty) {
|
|
210
|
-
const shouldContinue = await this.handleDirtyEditor();
|
|
211
|
-
if (!shouldContinue) {
|
|
212
|
-
this.dispose();
|
|
213
|
-
return false;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return true;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* @returns whether the setting operation in progress, and any others started in the meantime, should continue.
|
|
221
|
-
*/
|
|
222
|
-
protected async handleDirtyEditor(): Promise<boolean> {
|
|
223
|
-
const saveAndRetry = nls.localizeByDefault('Save and Retry');
|
|
224
|
-
const open = nls.localizeByDefault('Open File');
|
|
225
|
-
const msg = await this.messageService.error(
|
|
226
|
-
// eslint-disable-next-line @theia/localization-check
|
|
227
|
-
nls.localizeByDefault('Unable to write into {0} settings because the file has unsaved changes. Please save the {0} settings file first and then try again.',
|
|
228
|
-
nls.localizeByDefault(PreferenceScope[this.context.getScope()].toLocaleLowerCase())
|
|
229
|
-
),
|
|
230
|
-
saveAndRetry, open);
|
|
231
|
-
|
|
232
|
-
if (this.reference?.object) {
|
|
233
|
-
if (msg === open) {
|
|
234
|
-
this.editorManager.open(new URI(this.reference.object.uri));
|
|
235
|
-
} else if (msg === saveAndRetry) {
|
|
236
|
-
await this.reference.object.save();
|
|
237
|
-
return true;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
return false;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
protected async act(key: string, path: string[], value: unknown): Promise<boolean> {
|
|
244
|
-
const model = this.reference?.object;
|
|
245
|
-
try {
|
|
246
|
-
if (model) {
|
|
247
|
-
await this.jsoncEditor.setValue(model, path, value);
|
|
248
|
-
return this.result;
|
|
249
|
-
}
|
|
250
|
-
return false;
|
|
251
|
-
} catch (e) {
|
|
252
|
-
const message = `Failed to update the value of '${key}' in '${this.context.getConfigUri()}'.`;
|
|
253
|
-
this.messageService.error(`${message} Please check if it is corrupted.`);
|
|
254
|
-
console.error(`${message}`, e);
|
|
255
|
-
return false;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
protected async tearDown(): Promise<boolean> {
|
|
260
|
-
try {
|
|
261
|
-
const model = this.reference?.object;
|
|
262
|
-
if (model) {
|
|
263
|
-
if (this.status.state === 'resolved' && await this.status.promise) {
|
|
264
|
-
await model.save();
|
|
265
|
-
return true;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
return false;
|
|
269
|
-
} finally {
|
|
270
|
-
this.reference?.dispose();
|
|
271
|
-
this.reference = undefined;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
export interface PreferenceTransactionFactory {
|
|
277
|
-
(context: PreferenceContext, waitFor?: Promise<unknown>): PreferenceTransaction;
|
|
278
|
-
}
|
|
279
|
-
export const PreferenceTransactionFactory = Symbol('PreferenceTransactionFactory');
|
|
280
|
-
|
|
281
|
-
export const preferenceTransactionFactoryCreator: interfaces.FactoryCreator<PreferenceTransaction> = ({ container }) =>
|
|
282
|
-
(context: PreferenceContext, waitFor?: Promise<unknown>) => {
|
|
283
|
-
const child = container.createChild();
|
|
284
|
-
child.bind(PreferenceContext).toConstantValue(context);
|
|
285
|
-
child.bind(PreferenceTransactionPreludeProvider).toConstantValue(() => waitFor);
|
|
286
|
-
return child.get(PreferenceTransaction);
|
|
287
|
-
};
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2021 Ericsson and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { CancellationError, Emitter, Event, MaybePromise, MessageService, nls, WaitUntilEvent } from '@theia/core';
|
|
18
|
+
import { Deferred } from '@theia/core/lib/common/promise-util';
|
|
19
|
+
import { inject, injectable, interfaces, postConstruct } from '@theia/core/shared/inversify';
|
|
20
|
+
import { PreferenceScope } from '@theia/core/lib/common/preferences/preference-scope';
|
|
21
|
+
import URI from '@theia/core/lib/common/uri';
|
|
22
|
+
import { MonacoEditorModel } from '@theia/monaco/lib/browser/monaco-editor-model';
|
|
23
|
+
import { Mutex, MutexInterface } from 'async-mutex';
|
|
24
|
+
import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';
|
|
25
|
+
import { MonacoJSONCEditor } from './monaco-jsonc-editor';
|
|
26
|
+
import { EditorManager } from '@theia/editor/lib/browser/editor-manager';
|
|
27
|
+
import { IReference } from '@theia/monaco-editor-core/esm/vs/base/common/lifecycle';
|
|
28
|
+
|
|
29
|
+
export interface OnWillConcludeEvent<T> extends WaitUntilEvent {
|
|
30
|
+
status: T | false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@injectable()
|
|
34
|
+
/**
|
|
35
|
+
* Represents a batch of interactions with an underlying resource.
|
|
36
|
+
*/
|
|
37
|
+
export abstract class Transaction<Arguments extends unknown[], Result = unknown, Status = unknown> {
|
|
38
|
+
protected _open = true;
|
|
39
|
+
/**
|
|
40
|
+
* Whether the transaction is still accepting new interactions.
|
|
41
|
+
* Enqueueing an action when the Transaction is no longer open will throw an error.
|
|
42
|
+
*/
|
|
43
|
+
get open(): boolean {
|
|
44
|
+
return this._open;
|
|
45
|
+
}
|
|
46
|
+
protected _result = new Deferred<Result | false>();
|
|
47
|
+
/**
|
|
48
|
+
* The status of the transaction when complete.
|
|
49
|
+
*/
|
|
50
|
+
get result(): Promise<Result | false> {
|
|
51
|
+
return this._result.promise;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The transaction will self-dispose when the queue is empty, once at least one action has been processed.
|
|
55
|
+
*/
|
|
56
|
+
protected readonly queue = new Mutex(new CancellationError());
|
|
57
|
+
protected readonly onWillConcludeEmitter = new Emitter<OnWillConcludeEvent<Status>>();
|
|
58
|
+
/**
|
|
59
|
+
* An event fired when the transaction is wrapping up.
|
|
60
|
+
* Consumers can call `waitUntil` on the event to delay the resolution of the `result` Promise.
|
|
61
|
+
*/
|
|
62
|
+
get onWillConclude(): Event<OnWillConcludeEvent<Status>> {
|
|
63
|
+
return this.onWillConcludeEmitter.event;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected status = new Deferred<Status>();
|
|
67
|
+
/**
|
|
68
|
+
* Whether any actions have been added to the transaction.
|
|
69
|
+
* The Transaction will not self-dispose until at least one action has been performed.
|
|
70
|
+
*/
|
|
71
|
+
protected inUse = false;
|
|
72
|
+
|
|
73
|
+
@postConstruct()
|
|
74
|
+
protected init(): void {
|
|
75
|
+
this.doInit();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
protected async doInit(): Promise<void> {
|
|
79
|
+
const release = await this.queue.acquire();
|
|
80
|
+
try {
|
|
81
|
+
const status = await this.setUp();
|
|
82
|
+
this.status.resolve(status);
|
|
83
|
+
} catch {
|
|
84
|
+
this.dispose();
|
|
85
|
+
} finally {
|
|
86
|
+
release();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async waitFor(delay?: Promise<unknown>, disposeIfRejected?: boolean): Promise<void> {
|
|
91
|
+
try {
|
|
92
|
+
await this.queue.runExclusive(() => delay);
|
|
93
|
+
} catch {
|
|
94
|
+
if (disposeIfRejected) {
|
|
95
|
+
this.dispose();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @returns a promise reflecting the result of performing an action. Typically the promise will not resolve until the whole transaction is complete.
|
|
102
|
+
*/
|
|
103
|
+
async enqueueAction(...args: Arguments): Promise<Result | false> {
|
|
104
|
+
if (this._open) {
|
|
105
|
+
let release: MutexInterface.Releaser | undefined;
|
|
106
|
+
try {
|
|
107
|
+
release = await this.queue.acquire();
|
|
108
|
+
if (!this.inUse) {
|
|
109
|
+
this.inUse = true;
|
|
110
|
+
this.disposeWhenDone();
|
|
111
|
+
}
|
|
112
|
+
return this.act(...args);
|
|
113
|
+
} catch (e) {
|
|
114
|
+
if (e instanceof CancellationError) {
|
|
115
|
+
throw e;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
} finally {
|
|
119
|
+
release?.();
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
throw new Error('Transaction used after disposal.');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
protected disposeWhenDone(): void {
|
|
127
|
+
// Due to properties of the micro task system, it's possible for something to have been enqueued between
|
|
128
|
+
// the resolution of the waitForUnlock() promise and the the time this code runs, so we have to check.
|
|
129
|
+
this.queue.waitForUnlock().then(() => {
|
|
130
|
+
if (!this.queue.isLocked()) {
|
|
131
|
+
this.dispose();
|
|
132
|
+
} else {
|
|
133
|
+
this.disposeWhenDone();
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
protected async conclude(): Promise<void> {
|
|
139
|
+
if (this._open) {
|
|
140
|
+
try {
|
|
141
|
+
this._open = false;
|
|
142
|
+
this.queue.cancel();
|
|
143
|
+
const result = await this.tearDown();
|
|
144
|
+
const status = this.status.state === 'unresolved' || this.status.state === 'rejected' ? false : await this.status.promise;
|
|
145
|
+
await WaitUntilEvent.fire(this.onWillConcludeEmitter, { status });
|
|
146
|
+
this.onWillConcludeEmitter.dispose();
|
|
147
|
+
this._result.resolve(result);
|
|
148
|
+
} catch {
|
|
149
|
+
this._result.resolve(false);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
dispose(): void {
|
|
155
|
+
this.conclude();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Runs any code necessary to initialize the batch of interactions. No interaction will be run until the setup is complete.
|
|
160
|
+
*
|
|
161
|
+
* @returns a representation of the success of setup specific to a given transaction implementation.
|
|
162
|
+
*/
|
|
163
|
+
protected abstract setUp(): MaybePromise<Status>;
|
|
164
|
+
/**
|
|
165
|
+
* Performs a single interaction
|
|
166
|
+
*
|
|
167
|
+
* @returns the result of that interaction, specific to a given transaction type.
|
|
168
|
+
*/
|
|
169
|
+
protected abstract act(...args: Arguments): MaybePromise<Result>;
|
|
170
|
+
/**
|
|
171
|
+
* Runs any code necessary to complete a transaction and release any resources it holds.
|
|
172
|
+
*
|
|
173
|
+
* @returns implementation-specific information about the success of the transaction. Will be used as the final status of the transaction.
|
|
174
|
+
*/
|
|
175
|
+
protected abstract tearDown(): MaybePromise<Result>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface PreferenceContext {
|
|
179
|
+
getConfigUri(): URI;
|
|
180
|
+
getScope(): PreferenceScope;
|
|
181
|
+
}
|
|
182
|
+
export const PreferenceContext = Symbol('PreferenceContext');
|
|
183
|
+
export const PreferenceTransactionPreludeProvider = Symbol('PreferenceTransactionPreludeProvider');
|
|
184
|
+
export type PreferenceTransactionPreludeProvider = () => Promise<unknown>;
|
|
185
|
+
|
|
186
|
+
@injectable()
|
|
187
|
+
export class PreferenceTransaction extends Transaction<[string, string[], unknown], boolean> {
|
|
188
|
+
reference: IReference<MonacoEditorModel> | undefined;
|
|
189
|
+
@inject(PreferenceContext) protected readonly context: PreferenceContext;
|
|
190
|
+
@inject(PreferenceTransactionPreludeProvider) protected readonly prelude?: PreferenceTransactionPreludeProvider;
|
|
191
|
+
@inject(MonacoTextModelService) protected readonly textModelService: MonacoTextModelService;
|
|
192
|
+
@inject(MonacoJSONCEditor) protected readonly jsoncEditor: MonacoJSONCEditor;
|
|
193
|
+
@inject(MessageService) protected readonly messageService: MessageService;
|
|
194
|
+
@inject(EditorManager) protected readonly editorManager: EditorManager;
|
|
195
|
+
|
|
196
|
+
protected override async doInit(): Promise<void> {
|
|
197
|
+
this.waitFor(this.prelude?.());
|
|
198
|
+
await super.doInit();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
protected async setUp(): Promise<boolean> {
|
|
202
|
+
const reference = await this.textModelService.createModelReference(this.context.getConfigUri()!);
|
|
203
|
+
if (this._open) {
|
|
204
|
+
this.reference = reference;
|
|
205
|
+
} else {
|
|
206
|
+
reference.dispose();
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
if (reference.object.dirty) {
|
|
210
|
+
const shouldContinue = await this.handleDirtyEditor();
|
|
211
|
+
if (!shouldContinue) {
|
|
212
|
+
this.dispose();
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @returns whether the setting operation in progress, and any others started in the meantime, should continue.
|
|
221
|
+
*/
|
|
222
|
+
protected async handleDirtyEditor(): Promise<boolean> {
|
|
223
|
+
const saveAndRetry = nls.localizeByDefault('Save and Retry');
|
|
224
|
+
const open = nls.localizeByDefault('Open File');
|
|
225
|
+
const msg = await this.messageService.error(
|
|
226
|
+
// eslint-disable-next-line @theia/localization-check
|
|
227
|
+
nls.localizeByDefault('Unable to write into {0} settings because the file has unsaved changes. Please save the {0} settings file first and then try again.',
|
|
228
|
+
nls.localizeByDefault(PreferenceScope[this.context.getScope()].toLocaleLowerCase())
|
|
229
|
+
),
|
|
230
|
+
saveAndRetry, open);
|
|
231
|
+
|
|
232
|
+
if (this.reference?.object) {
|
|
233
|
+
if (msg === open) {
|
|
234
|
+
this.editorManager.open(new URI(this.reference.object.uri));
|
|
235
|
+
} else if (msg === saveAndRetry) {
|
|
236
|
+
await this.reference.object.save();
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
protected async act(key: string, path: string[], value: unknown): Promise<boolean> {
|
|
244
|
+
const model = this.reference?.object;
|
|
245
|
+
try {
|
|
246
|
+
if (model) {
|
|
247
|
+
await this.jsoncEditor.setValue(model, path, value);
|
|
248
|
+
return this.result;
|
|
249
|
+
}
|
|
250
|
+
return false;
|
|
251
|
+
} catch (e) {
|
|
252
|
+
const message = `Failed to update the value of '${key}' in '${this.context.getConfigUri()}'.`;
|
|
253
|
+
this.messageService.error(`${message} Please check if it is corrupted.`);
|
|
254
|
+
console.error(`${message}`, e);
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
protected async tearDown(): Promise<boolean> {
|
|
260
|
+
try {
|
|
261
|
+
const model = this.reference?.object;
|
|
262
|
+
if (model) {
|
|
263
|
+
if (this.status.state === 'resolved' && await this.status.promise) {
|
|
264
|
+
await model.save();
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return false;
|
|
269
|
+
} finally {
|
|
270
|
+
this.reference?.dispose();
|
|
271
|
+
this.reference = undefined;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export interface PreferenceTransactionFactory {
|
|
277
|
+
(context: PreferenceContext, waitFor?: Promise<unknown>): PreferenceTransaction;
|
|
278
|
+
}
|
|
279
|
+
export const PreferenceTransactionFactory = Symbol('PreferenceTransactionFactory');
|
|
280
|
+
|
|
281
|
+
export const preferenceTransactionFactoryCreator: interfaces.FactoryCreator<PreferenceTransaction> = ({ container }) =>
|
|
282
|
+
(context: PreferenceContext, waitFor?: Promise<unknown>) => {
|
|
283
|
+
const child = container.createChild();
|
|
284
|
+
child.bind(PreferenceContext).toConstantValue(context);
|
|
285
|
+
child.bind(PreferenceTransactionPreludeProvider).toConstantValue(() => waitFor);
|
|
286
|
+
return child.get(PreferenceTransaction);
|
|
287
|
+
};
|