@theia/preferences 1.34.1 → 1.34.3

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.
Files changed (128) hide show
  1. package/LICENSE +641 -641
  2. package/README.md +81 -81
  3. package/lib/browser/abstract-resource-preference-provider.d.ts +46 -46
  4. package/lib/browser/abstract-resource-preference-provider.js +237 -237
  5. package/lib/browser/abstract-resource-preference-provider.spec.d.ts +1 -1
  6. package/lib/browser/abstract-resource-preference-provider.spec.js +83 -83
  7. package/lib/browser/folder-preference-provider.d.ts +20 -20
  8. package/lib/browser/folder-preference-provider.js +59 -59
  9. package/lib/browser/folders-preferences-provider.d.ts +25 -25
  10. package/lib/browser/folders-preferences-provider.js +239 -239
  11. package/lib/browser/index.d.ts +7 -7
  12. package/lib/browser/index.js +34 -34
  13. package/lib/browser/monaco-jsonc-editor.d.ts +9 -9
  14. package/lib/browser/monaco-jsonc-editor.js +80 -80
  15. package/lib/browser/package.spec.js +25 -25
  16. package/lib/browser/preference-bindings.d.ts +4 -4
  17. package/lib/browser/preference-bindings.js +63 -63
  18. package/lib/browser/preference-frontend-module.d.ts +6 -6
  19. package/lib/browser/preference-frontend-module.js +52 -52
  20. package/lib/browser/preference-open-handler.d.ts +9 -9
  21. package/lib/browser/preference-open-handler.js +64 -64
  22. package/lib/browser/preference-transaction-manager.d.ts +98 -98
  23. package/lib/browser/preference-transaction-manager.js +295 -295
  24. package/lib/browser/preference-tree-model.d.ts +59 -59
  25. package/lib/browser/preference-tree-model.js +240 -240
  26. package/lib/browser/preferences-contribution.d.ts +37 -37
  27. package/lib/browser/preferences-contribution.js +279 -279
  28. package/lib/browser/preferences-json-schema-contribution.d.ts +17 -17
  29. package/lib/browser/preferences-json-schema-contribution.js +91 -91
  30. package/lib/browser/preferences-monaco-contribution.d.ts +1 -1
  31. package/lib/browser/preferences-monaco-contribution.js +30 -30
  32. package/lib/browser/section-preference-provider.d.ts +21 -21
  33. package/lib/browser/section-preference-provider.js +96 -96
  34. package/lib/browser/user-configs-preference-provider.d.ts +21 -21
  35. package/lib/browser/user-configs-preference-provider.js +134 -134
  36. package/lib/browser/user-preference-provider.d.ts +13 -13
  37. package/lib/browser/user-preference-provider.js +41 -41
  38. package/lib/browser/util/preference-scope-command-manager.d.ts +17 -17
  39. package/lib/browser/util/preference-scope-command-manager.js +87 -87
  40. package/lib/browser/util/preference-tree-generator.d.ts +30 -30
  41. package/lib/browser/util/preference-tree-generator.js +234 -234
  42. package/lib/browser/util/preference-tree-label-provider.d.ts +11 -11
  43. package/lib/browser/util/preference-tree-label-provider.js +77 -77
  44. package/lib/browser/util/preference-tree-label-provider.spec.d.ts +1 -1
  45. package/lib/browser/util/preference-tree-label-provider.spec.js +87 -87
  46. package/lib/browser/util/preference-types.d.ts +62 -62
  47. package/lib/browser/util/preference-types.js +128 -128
  48. package/lib/browser/views/components/preference-array-input.d.ts +28 -28
  49. package/lib/browser/views/components/preference-array-input.js +180 -180
  50. package/lib/browser/views/components/preference-boolean-input.d.ts +17 -17
  51. package/lib/browser/views/components/preference-boolean-input.js +79 -79
  52. package/lib/browser/views/components/preference-file-input.d.ts +29 -29
  53. package/lib/browser/views/components/preference-file-input.js +110 -110
  54. package/lib/browser/views/components/preference-json-input.d.ts +19 -19
  55. package/lib/browser/views/components/preference-json-input.js +93 -93
  56. package/lib/browser/views/components/preference-node-renderer-creator.d.ts +48 -48
  57. package/lib/browser/views/components/preference-node-renderer-creator.js +132 -132
  58. package/lib/browser/views/components/preference-node-renderer.d.ts +111 -111
  59. package/lib/browser/views/components/preference-node-renderer.js +460 -460
  60. package/lib/browser/views/components/preference-number-input.d.ts +34 -34
  61. package/lib/browser/views/components/preference-number-input.js +142 -142
  62. package/lib/browser/views/components/preference-select-input.d.ts +27 -27
  63. package/lib/browser/views/components/preference-select-input.js +135 -135
  64. package/lib/browser/views/components/preference-string-input.d.ts +17 -17
  65. package/lib/browser/views/components/preference-string-input.js +89 -89
  66. package/lib/browser/views/preference-editor-widget.d.ts +67 -67
  67. package/lib/browser/views/preference-editor-widget.js +376 -376
  68. package/lib/browser/views/preference-scope-tabbar-widget.d.ts +54 -54
  69. package/lib/browser/views/preference-scope-tabbar-widget.js +343 -343
  70. package/lib/browser/views/preference-searchbar-widget.d.ts +53 -53
  71. package/lib/browser/views/preference-searchbar-widget.js +173 -173
  72. package/lib/browser/views/preference-tree-widget.d.ts +17 -17
  73. package/lib/browser/views/preference-tree-widget.js +104 -104
  74. package/lib/browser/views/preference-widget-bindings.d.ts +3 -3
  75. package/lib/browser/views/preference-widget-bindings.js +85 -85
  76. package/lib/browser/views/preference-widget.d.ts +35 -35
  77. package/lib/browser/views/preference-widget.js +123 -123
  78. package/lib/browser/workspace-file-preference-provider.d.ts +23 -23
  79. package/lib/browser/workspace-file-preference-provider.js +110 -110
  80. package/lib/browser/workspace-preference-provider.d.ts +28 -28
  81. package/lib/browser/workspace-preference-provider.js +142 -142
  82. package/package.json +9 -9
  83. package/src/browser/abstract-resource-preference-provider.spec.ts +95 -95
  84. package/src/browser/abstract-resource-preference-provider.ts +228 -228
  85. package/src/browser/folder-preference-provider.ts +58 -58
  86. package/src/browser/folders-preferences-provider.ts +236 -236
  87. package/src/browser/index.ts +23 -23
  88. package/src/browser/monaco-jsonc-editor.ts +67 -67
  89. package/src/browser/package.spec.ts +28 -28
  90. package/src/browser/preference-bindings.ts +65 -65
  91. package/src/browser/preference-frontend-module.ts +57 -57
  92. package/src/browser/preference-open-handler.ts +53 -53
  93. package/src/browser/preference-transaction-manager.ts +283 -283
  94. package/src/browser/preference-tree-model.ts +246 -246
  95. package/src/browser/preferences-contribution.ts +263 -263
  96. package/src/browser/preferences-json-schema-contribution.ts +86 -86
  97. package/src/browser/preferences-monaco-contribution.ts +30 -30
  98. package/src/browser/section-preference-provider.ts +83 -83
  99. package/src/browser/style/index.css +456 -456
  100. package/src/browser/style/preference-array.css +90 -90
  101. package/src/browser/style/preference-context-menu.css +74 -74
  102. package/src/browser/style/preference-file.css +32 -32
  103. package/src/browser/style/preference-object.css +49 -49
  104. package/src/browser/style/search-input.css +66 -66
  105. package/src/browser/user-configs-preference-provider.ts +123 -123
  106. package/src/browser/user-preference-provider.ts +35 -35
  107. package/src/browser/util/preference-scope-command-manager.ts +75 -75
  108. package/src/browser/util/preference-tree-generator.ts +222 -222
  109. package/src/browser/util/preference-tree-label-provider.spec.ts +108 -108
  110. package/src/browser/util/preference-tree-label-provider.ts +64 -64
  111. package/src/browser/util/preference-types.ts +169 -169
  112. package/src/browser/views/components/preference-array-input.ts +174 -174
  113. package/src/browser/views/components/preference-boolean-input.ts +69 -69
  114. package/src/browser/views/components/preference-file-input.ts +104 -104
  115. package/src/browser/views/components/preference-json-input.ts +78 -78
  116. package/src/browser/views/components/preference-node-renderer-creator.ts +141 -141
  117. package/src/browser/views/components/preference-node-renderer.ts +499 -499
  118. package/src/browser/views/components/preference-number-input.ts +147 -147
  119. package/src/browser/views/components/preference-select-input.ts +127 -127
  120. package/src/browser/views/components/preference-string-input.ts +76 -76
  121. package/src/browser/views/preference-editor-widget.ts +361 -361
  122. package/src/browser/views/preference-scope-tabbar-widget.tsx +344 -344
  123. package/src/browser/views/preference-searchbar-widget.tsx +183 -183
  124. package/src/browser/views/preference-tree-widget.tsx +93 -93
  125. package/src/browser/views/preference-widget-bindings.ts +99 -99
  126. package/src/browser/views/preference-widget.tsx +113 -113
  127. package/src/browser/workspace-file-preference-provider.ts +100 -100
  128. package/src/browser/workspace-preference-provider.ts +134 -134
@@ -1,283 +1,283 @@
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 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 async init(): Promise<void> {
75
- const release = await this.queue.acquire();
76
- try {
77
- const status = await this.setUp();
78
- this.status.resolve(status);
79
- } catch {
80
- this.dispose();
81
- } finally {
82
- release();
83
- }
84
- }
85
-
86
- async waitFor(delay?: Promise<unknown>, disposeIfRejected?: boolean): Promise<void> {
87
- try {
88
- await this.queue.runExclusive(() => delay);
89
- } catch {
90
- if (disposeIfRejected) {
91
- this.dispose();
92
- }
93
- }
94
- }
95
-
96
- /**
97
- * @returns a promise reflecting the result of performing an action. Typically the promise will not resolve until the whole transaction is complete.
98
- */
99
- async enqueueAction(...args: Arguments): Promise<Result | false> {
100
- if (this._open) {
101
- let release: MutexInterface.Releaser | undefined;
102
- try {
103
- release = await this.queue.acquire();
104
- if (!this.inUse) {
105
- this.inUse = true;
106
- this.disposeWhenDone();
107
- }
108
- return this.act(...args);
109
- } catch (e) {
110
- if (e instanceof CancellationError) {
111
- throw e;
112
- }
113
- return false;
114
- } finally {
115
- release?.();
116
- }
117
- } else {
118
- throw new Error('Transaction used after disposal.');
119
- }
120
- }
121
-
122
- protected disposeWhenDone(): void {
123
- // Due to properties of the micro task system, it's possible for something to have been enqueued between
124
- // the resolution of the waitForUnlock() promise and the the time this code runs, so we have to check.
125
- this.queue.waitForUnlock().then(() => {
126
- if (!this.queue.isLocked()) {
127
- this.dispose();
128
- } else {
129
- this.disposeWhenDone();
130
- }
131
- });
132
- }
133
-
134
- protected async conclude(): Promise<void> {
135
- if (this._open) {
136
- try {
137
- this._open = false;
138
- this.queue.cancel();
139
- const result = await this.tearDown();
140
- const status = this.status.state === 'unresolved' || this.status.state === 'rejected' ? false : await this.status.promise;
141
- await WaitUntilEvent.fire(this.onWillConcludeEmitter, { status });
142
- this.onWillConcludeEmitter.dispose();
143
- this._result.resolve(result);
144
- } catch {
145
- this._result.resolve(false);
146
- }
147
- }
148
- }
149
-
150
- dispose(): void {
151
- this.conclude();
152
- }
153
-
154
- /**
155
- * Runs any code necessary to initialize the batch of interactions. No interaction will be run until the setup is complete.
156
- *
157
- * @returns a representation of the success of setup specific to a given transaction implementation.
158
- */
159
- protected abstract setUp(): MaybePromise<Status>;
160
- /**
161
- * Performs a single interaction
162
- *
163
- * @returns the result of that interaction, specific to a given transaction type.
164
- */
165
- protected abstract act(...args: Arguments): MaybePromise<Result>;
166
- /**
167
- * Runs any code necessary to complete a transaction and release any resources it holds.
168
- *
169
- * @returns implementation-specific information about the success of the transaction. Will be used as the final status of the transaction.
170
- */
171
- protected abstract tearDown(): MaybePromise<Result>;
172
- }
173
-
174
- export interface PreferenceContext {
175
- getConfigUri(): URI;
176
- getScope(): PreferenceScope;
177
- }
178
- export const PreferenceContext = Symbol('PreferenceContext');
179
- export const PreferenceTransactionPrelude = Symbol('PreferenceTransactionPrelude');
180
-
181
- @injectable()
182
- export class PreferenceTransaction extends Transaction<[string, string[], unknown], boolean> {
183
- reference: IReference<MonacoEditorModel> | undefined;
184
- @inject(PreferenceContext) protected readonly context: PreferenceContext;
185
- @inject(PreferenceTransactionPrelude) protected readonly prelude?: Promise<unknown>;
186
- @inject(MonacoTextModelService) protected readonly textModelService: MonacoTextModelService;
187
- @inject(MonacoJSONCEditor) protected readonly jsoncEditor: MonacoJSONCEditor;
188
- @inject(MessageService) protected readonly messageService: MessageService;
189
- @inject(EditorManager) protected readonly editorManager: EditorManager;
190
-
191
- @postConstruct()
192
- protected override init(): Promise<void> {
193
- this.waitFor(this.prelude);
194
- return super.init();
195
- }
196
-
197
- protected async setUp(): Promise<boolean> {
198
- const reference = await this.textModelService.createModelReference(this.context.getConfigUri()!);
199
- if (this._open) {
200
- this.reference = reference;
201
- } else {
202
- reference.dispose();
203
- return false;
204
- }
205
- if (reference.object.dirty) {
206
- const shouldContinue = await this.handleDirtyEditor();
207
- if (!shouldContinue) {
208
- this.dispose();
209
- return false;
210
- }
211
- }
212
- return true;
213
- }
214
-
215
- /**
216
- * @returns whether the setting operation in progress, and any others started in the meantime, should continue.
217
- */
218
- protected async handleDirtyEditor(): Promise<boolean> {
219
- const saveAndRetry = nls.localizeByDefault('Save and Retry');
220
- const open = nls.localizeByDefault('Open File');
221
- const msg = await this.messageService.error(
222
- // eslint-disable-next-line @theia/localization-check
223
- 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.',
224
- nls.localizeByDefault(PreferenceScope[this.context.getScope()].toLocaleLowerCase())
225
- ),
226
- saveAndRetry, open);
227
-
228
- if (this.reference?.object) {
229
- if (msg === open) {
230
- this.editorManager.open(new URI(this.reference.object.uri));
231
- } else if (msg === saveAndRetry) {
232
- await this.reference.object.save();
233
- return true;
234
- }
235
- }
236
- return false;
237
- }
238
-
239
- protected async act(key: string, path: string[], value: unknown): Promise<boolean> {
240
- const model = this.reference?.object;
241
- try {
242
- if (model) {
243
- await this.jsoncEditor.setValue(model, path, value);
244
- return this.result;
245
- }
246
- return false;
247
- } catch (e) {
248
- const message = `Failed to update the value of '${key}' in '${this.context.getConfigUri()}'.`;
249
- this.messageService.error(`${message} Please check if it is corrupted.`);
250
- console.error(`${message}`, e);
251
- return false;
252
- }
253
- }
254
-
255
- protected async tearDown(): Promise<boolean> {
256
- try {
257
- const model = this.reference?.object;
258
- if (model) {
259
- if (this.status.state === 'resolved' && await this.status.promise) {
260
- await model.save();
261
- return true;
262
- }
263
- }
264
- return false;
265
- } finally {
266
- this.reference?.dispose();
267
- this.reference = undefined;
268
- }
269
- }
270
- }
271
-
272
- export interface PreferenceTransactionFactory {
273
- (context: PreferenceContext, waitFor?: Promise<unknown>): PreferenceTransaction;
274
- }
275
- export const PreferenceTransactionFactory = Symbol('PreferenceTransactionFactory');
276
-
277
- export const preferenceTransactionFactoryCreator: interfaces.FactoryCreator<PreferenceTransaction> = ({ container }) =>
278
- (context: PreferenceContext, waitFor?: Promise<unknown>) => {
279
- const child = container.createChild();
280
- child.bind(PreferenceContext).toConstantValue(context);
281
- child.bind(PreferenceTransactionPrelude).toConstantValue(waitFor);
282
- return child.get(PreferenceTransaction);
283
- };
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 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 async init(): Promise<void> {
75
+ const release = await this.queue.acquire();
76
+ try {
77
+ const status = await this.setUp();
78
+ this.status.resolve(status);
79
+ } catch {
80
+ this.dispose();
81
+ } finally {
82
+ release();
83
+ }
84
+ }
85
+
86
+ async waitFor(delay?: Promise<unknown>, disposeIfRejected?: boolean): Promise<void> {
87
+ try {
88
+ await this.queue.runExclusive(() => delay);
89
+ } catch {
90
+ if (disposeIfRejected) {
91
+ this.dispose();
92
+ }
93
+ }
94
+ }
95
+
96
+ /**
97
+ * @returns a promise reflecting the result of performing an action. Typically the promise will not resolve until the whole transaction is complete.
98
+ */
99
+ async enqueueAction(...args: Arguments): Promise<Result | false> {
100
+ if (this._open) {
101
+ let release: MutexInterface.Releaser | undefined;
102
+ try {
103
+ release = await this.queue.acquire();
104
+ if (!this.inUse) {
105
+ this.inUse = true;
106
+ this.disposeWhenDone();
107
+ }
108
+ return this.act(...args);
109
+ } catch (e) {
110
+ if (e instanceof CancellationError) {
111
+ throw e;
112
+ }
113
+ return false;
114
+ } finally {
115
+ release?.();
116
+ }
117
+ } else {
118
+ throw new Error('Transaction used after disposal.');
119
+ }
120
+ }
121
+
122
+ protected disposeWhenDone(): void {
123
+ // Due to properties of the micro task system, it's possible for something to have been enqueued between
124
+ // the resolution of the waitForUnlock() promise and the the time this code runs, so we have to check.
125
+ this.queue.waitForUnlock().then(() => {
126
+ if (!this.queue.isLocked()) {
127
+ this.dispose();
128
+ } else {
129
+ this.disposeWhenDone();
130
+ }
131
+ });
132
+ }
133
+
134
+ protected async conclude(): Promise<void> {
135
+ if (this._open) {
136
+ try {
137
+ this._open = false;
138
+ this.queue.cancel();
139
+ const result = await this.tearDown();
140
+ const status = this.status.state === 'unresolved' || this.status.state === 'rejected' ? false : await this.status.promise;
141
+ await WaitUntilEvent.fire(this.onWillConcludeEmitter, { status });
142
+ this.onWillConcludeEmitter.dispose();
143
+ this._result.resolve(result);
144
+ } catch {
145
+ this._result.resolve(false);
146
+ }
147
+ }
148
+ }
149
+
150
+ dispose(): void {
151
+ this.conclude();
152
+ }
153
+
154
+ /**
155
+ * Runs any code necessary to initialize the batch of interactions. No interaction will be run until the setup is complete.
156
+ *
157
+ * @returns a representation of the success of setup specific to a given transaction implementation.
158
+ */
159
+ protected abstract setUp(): MaybePromise<Status>;
160
+ /**
161
+ * Performs a single interaction
162
+ *
163
+ * @returns the result of that interaction, specific to a given transaction type.
164
+ */
165
+ protected abstract act(...args: Arguments): MaybePromise<Result>;
166
+ /**
167
+ * Runs any code necessary to complete a transaction and release any resources it holds.
168
+ *
169
+ * @returns implementation-specific information about the success of the transaction. Will be used as the final status of the transaction.
170
+ */
171
+ protected abstract tearDown(): MaybePromise<Result>;
172
+ }
173
+
174
+ export interface PreferenceContext {
175
+ getConfigUri(): URI;
176
+ getScope(): PreferenceScope;
177
+ }
178
+ export const PreferenceContext = Symbol('PreferenceContext');
179
+ export const PreferenceTransactionPrelude = Symbol('PreferenceTransactionPrelude');
180
+
181
+ @injectable()
182
+ export class PreferenceTransaction extends Transaction<[string, string[], unknown], boolean> {
183
+ reference: IReference<MonacoEditorModel> | undefined;
184
+ @inject(PreferenceContext) protected readonly context: PreferenceContext;
185
+ @inject(PreferenceTransactionPrelude) protected readonly prelude?: Promise<unknown>;
186
+ @inject(MonacoTextModelService) protected readonly textModelService: MonacoTextModelService;
187
+ @inject(MonacoJSONCEditor) protected readonly jsoncEditor: MonacoJSONCEditor;
188
+ @inject(MessageService) protected readonly messageService: MessageService;
189
+ @inject(EditorManager) protected readonly editorManager: EditorManager;
190
+
191
+ @postConstruct()
192
+ protected override init(): Promise<void> {
193
+ this.waitFor(this.prelude);
194
+ return super.init();
195
+ }
196
+
197
+ protected async setUp(): Promise<boolean> {
198
+ const reference = await this.textModelService.createModelReference(this.context.getConfigUri()!);
199
+ if (this._open) {
200
+ this.reference = reference;
201
+ } else {
202
+ reference.dispose();
203
+ return false;
204
+ }
205
+ if (reference.object.dirty) {
206
+ const shouldContinue = await this.handleDirtyEditor();
207
+ if (!shouldContinue) {
208
+ this.dispose();
209
+ return false;
210
+ }
211
+ }
212
+ return true;
213
+ }
214
+
215
+ /**
216
+ * @returns whether the setting operation in progress, and any others started in the meantime, should continue.
217
+ */
218
+ protected async handleDirtyEditor(): Promise<boolean> {
219
+ const saveAndRetry = nls.localizeByDefault('Save and Retry');
220
+ const open = nls.localizeByDefault('Open File');
221
+ const msg = await this.messageService.error(
222
+ // eslint-disable-next-line @theia/localization-check
223
+ 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.',
224
+ nls.localizeByDefault(PreferenceScope[this.context.getScope()].toLocaleLowerCase())
225
+ ),
226
+ saveAndRetry, open);
227
+
228
+ if (this.reference?.object) {
229
+ if (msg === open) {
230
+ this.editorManager.open(new URI(this.reference.object.uri));
231
+ } else if (msg === saveAndRetry) {
232
+ await this.reference.object.save();
233
+ return true;
234
+ }
235
+ }
236
+ return false;
237
+ }
238
+
239
+ protected async act(key: string, path: string[], value: unknown): Promise<boolean> {
240
+ const model = this.reference?.object;
241
+ try {
242
+ if (model) {
243
+ await this.jsoncEditor.setValue(model, path, value);
244
+ return this.result;
245
+ }
246
+ return false;
247
+ } catch (e) {
248
+ const message = `Failed to update the value of '${key}' in '${this.context.getConfigUri()}'.`;
249
+ this.messageService.error(`${message} Please check if it is corrupted.`);
250
+ console.error(`${message}`, e);
251
+ return false;
252
+ }
253
+ }
254
+
255
+ protected async tearDown(): Promise<boolean> {
256
+ try {
257
+ const model = this.reference?.object;
258
+ if (model) {
259
+ if (this.status.state === 'resolved' && await this.status.promise) {
260
+ await model.save();
261
+ return true;
262
+ }
263
+ }
264
+ return false;
265
+ } finally {
266
+ this.reference?.dispose();
267
+ this.reference = undefined;
268
+ }
269
+ }
270
+ }
271
+
272
+ export interface PreferenceTransactionFactory {
273
+ (context: PreferenceContext, waitFor?: Promise<unknown>): PreferenceTransaction;
274
+ }
275
+ export const PreferenceTransactionFactory = Symbol('PreferenceTransactionFactory');
276
+
277
+ export const preferenceTransactionFactoryCreator: interfaces.FactoryCreator<PreferenceTransaction> = ({ container }) =>
278
+ (context: PreferenceContext, waitFor?: Promise<unknown>) => {
279
+ const child = container.createChild();
280
+ child.bind(PreferenceContext).toConstantValue(context);
281
+ child.bind(PreferenceTransactionPrelude).toConstantValue(waitFor);
282
+ return child.get(PreferenceTransaction);
283
+ };