@theia/preferences 1.34.3 → 1.34.4

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,263 +1,263 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2018 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 { injectable, inject, named, optional } from '@theia/core/shared/inversify';
18
- import { MenuModelRegistry, CommandRegistry } from '@theia/core';
19
- import {
20
- CommonMenus,
21
- AbstractViewContribution,
22
- CommonCommands,
23
- KeybindingRegistry,
24
- Widget,
25
- PreferenceScope,
26
- PreferenceProvider,
27
- PreferenceService,
28
- QuickInputService,
29
- QuickPickItem,
30
- isFirefox,
31
- PreferenceSchemaProvider,
32
- } from '@theia/core/lib/browser';
33
- import { isOSX } from '@theia/core/lib/common/os';
34
- import { TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
35
- import { EditorManager, EditorWidget } from '@theia/editor/lib/browser';
36
- import URI from '@theia/core/lib/common/uri';
37
- import { PreferencesWidget } from './views/preference-widget';
38
- import { WorkspacePreferenceProvider } from './workspace-preference-provider';
39
- import { Preference, PreferencesCommands, PreferenceMenus } from './util/preference-types';
40
- import { ClipboardService } from '@theia/core/lib/browser/clipboard-service';
41
- import { FileService } from '@theia/filesystem/lib/browser/file-service';
42
- import { WorkspaceService } from '@theia/workspace/lib/browser';
43
- import { FileStat } from '@theia/filesystem/lib/common/files';
44
-
45
- @injectable()
46
- export class PreferencesContribution extends AbstractViewContribution<PreferencesWidget> {
47
-
48
- @inject(FileService) protected readonly fileService: FileService;
49
- @inject(PreferenceProvider) @named(PreferenceScope.Workspace) protected readonly workspacePreferenceProvider: WorkspacePreferenceProvider;
50
- @inject(EditorManager) protected readonly editorManager: EditorManager;
51
- @inject(PreferenceService) protected readonly preferenceService: PreferenceService;
52
- @inject(ClipboardService) protected readonly clipboardService: ClipboardService;
53
- @inject(PreferencesWidget) protected readonly scopeTracker: PreferencesWidget;
54
- @inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
55
- @inject(QuickInputService) @optional() protected readonly quickInputService: QuickInputService;
56
- @inject(PreferenceSchemaProvider) protected readonly schema: PreferenceSchemaProvider;
57
-
58
- constructor() {
59
- super({
60
- widgetId: PreferencesWidget.ID,
61
- widgetName: PreferencesWidget.LABEL,
62
- defaultWidgetOptions: {
63
- area: 'main',
64
- },
65
- });
66
- }
67
-
68
- override registerCommands(commands: CommandRegistry): void {
69
- commands.registerCommand(CommonCommands.OPEN_PREFERENCES, {
70
- execute: async (query?: string) => {
71
- const widget = await this.openView({ activate: true });
72
- if (typeof query === 'string') {
73
- widget.setSearchTerm(query);
74
- }
75
- },
76
- });
77
- commands.registerCommand(PreferencesCommands.OPEN_PREFERENCES_JSON_TOOLBAR, {
78
- isEnabled: () => true,
79
- isVisible: w => this.withWidget(w, () => true),
80
- execute: (preferenceId: string) => {
81
- this.openPreferencesJSON(preferenceId);
82
- }
83
- });
84
- commands.registerCommand(PreferencesCommands.COPY_JSON_NAME, {
85
- isEnabled: Preference.EditorCommandArgs.is,
86
- isVisible: Preference.EditorCommandArgs.is,
87
- execute: ({ id, value }: Preference.EditorCommandArgs) => {
88
- this.clipboardService.writeText(id);
89
- }
90
- });
91
- commands.registerCommand(PreferencesCommands.COPY_JSON_VALUE, {
92
- isEnabled: Preference.EditorCommandArgs.is,
93
- isVisible: Preference.EditorCommandArgs.is,
94
- execute: ({ id, value }: { id: string, value: string; }) => {
95
- const jsonString = `"${id}": ${JSON.stringify(value)}`;
96
- this.clipboardService.writeText(jsonString);
97
- }
98
- });
99
- commands.registerCommand(PreferencesCommands.RESET_PREFERENCE, {
100
- isEnabled: Preference.EditorCommandArgs.is,
101
- isVisible: Preference.EditorCommandArgs.is,
102
- execute: ({ id }: Preference.EditorCommandArgs) => {
103
- this.preferenceService.set(id, undefined, Number(this.scopeTracker.currentScope.scope), this.scopeTracker.currentScope.uri);
104
- }
105
- });
106
- commands.registerCommand(PreferencesCommands.OPEN_USER_PREFERENCES, {
107
- execute: async () => {
108
- const widget = await this.openView({ activate: true });
109
- widget.setScope(PreferenceScope.User);
110
- }
111
- });
112
- commands.registerCommand(PreferencesCommands.OPEN_WORKSPACE_PREFERENCES, {
113
- isEnabled: () => !!this.workspaceService.workspace,
114
- isVisible: () => !!this.workspaceService.workspace,
115
- execute: async () => {
116
- const widget = await this.openView({ activate: true });
117
- widget.setScope(PreferenceScope.Workspace);
118
- }
119
- });
120
- commands.registerCommand(PreferencesCommands.OPEN_FOLDER_PREFERENCES, {
121
- isEnabled: () => !!this.workspaceService.isMultiRootWorkspaceOpened && this.workspaceService.tryGetRoots().length > 0,
122
- isVisible: () => !!this.workspaceService.isMultiRootWorkspaceOpened && this.workspaceService.tryGetRoots().length > 0,
123
- execute: () => this.openFolderPreferences(root => {
124
- this.openView({ activate: true });
125
- this.scopeTracker.setScope(root.resource);
126
- })
127
- });
128
- commands.registerCommand(PreferencesCommands.OPEN_USER_PREFERENCES_JSON, {
129
- execute: async () => this.openJson(PreferenceScope.User)
130
- });
131
- commands.registerCommand(PreferencesCommands.OPEN_WORKSPACE_PREFERENCES_JSON, {
132
- isEnabled: () => !!this.workspaceService.workspace,
133
- isVisible: () => !!this.workspaceService.workspace,
134
- execute: async () => this.openJson(PreferenceScope.Workspace)
135
- });
136
- commands.registerCommand(PreferencesCommands.OPEN_FOLDER_PREFERENCES_JSON, {
137
- isEnabled: () => !!this.workspaceService.isMultiRootWorkspaceOpened && this.workspaceService.tryGetRoots().length > 0,
138
- isVisible: () => !!this.workspaceService.isMultiRootWorkspaceOpened && this.workspaceService.tryGetRoots().length > 0,
139
- execute: () => this.openFolderPreferences(root => this.openJson(PreferenceScope.Folder, root.resource.toString()))
140
- });
141
- }
142
-
143
- override registerMenus(menus: MenuModelRegistry): void {
144
- menus.registerMenuAction(CommonMenus.FILE_SETTINGS_SUBMENU_OPEN, {
145
- commandId: CommonCommands.OPEN_PREFERENCES.id,
146
- label: CommonCommands.OPEN_PREFERENCES.label,
147
- order: 'a10',
148
- });
149
- menus.registerMenuAction(CommonMenus.SETTINGS_OPEN, {
150
- commandId: CommonCommands.OPEN_PREFERENCES.id,
151
- label: CommonCommands.OPEN_PREFERENCES.label,
152
- order: 'a10',
153
- });
154
- menus.registerMenuAction(PreferenceMenus.PREFERENCE_EDITOR_CONTEXT_MENU, {
155
- commandId: PreferencesCommands.RESET_PREFERENCE.id,
156
- label: PreferencesCommands.RESET_PREFERENCE.label,
157
- order: 'a'
158
- });
159
- menus.registerMenuAction(PreferenceMenus.PREFERENCE_EDITOR_COPY_ACTIONS, {
160
- commandId: PreferencesCommands.COPY_JSON_VALUE.id,
161
- label: PreferencesCommands.COPY_JSON_VALUE.label,
162
- order: 'b'
163
- });
164
- menus.registerMenuAction(PreferenceMenus.PREFERENCE_EDITOR_COPY_ACTIONS, {
165
- commandId: PreferencesCommands.COPY_JSON_NAME.id,
166
- label: PreferencesCommands.COPY_JSON_NAME.label,
167
- order: 'c'
168
- });
169
- }
170
-
171
- override registerKeybindings(keybindings: KeybindingRegistry): void {
172
- keybindings.registerKeybinding({
173
- command: CommonCommands.OPEN_PREFERENCES.id,
174
- keybinding: (isOSX && !isFirefox) ? 'cmd+,' : 'ctrl+,'
175
- });
176
- }
177
-
178
- registerToolbarItems(toolbar: TabBarToolbarRegistry): void {
179
- toolbar.registerItem({
180
- id: PreferencesCommands.OPEN_PREFERENCES_JSON_TOOLBAR.id,
181
- command: PreferencesCommands.OPEN_PREFERENCES_JSON_TOOLBAR.id,
182
- tooltip: PreferencesCommands.OPEN_USER_PREFERENCES_JSON.label,
183
- priority: 0,
184
- });
185
- }
186
-
187
- protected async openPreferencesJSON(opener: string | PreferencesWidget): Promise<void> {
188
- const { scope, activeScopeIsFolder, uri } = this.scopeTracker.currentScope;
189
- const scopeID = Number(scope);
190
- let preferenceId = '';
191
- if (typeof opener === 'string') {
192
- preferenceId = opener;
193
- const currentPreferenceValue = this.preferenceService.inspect(preferenceId, uri);
194
- const valueInCurrentScope = Preference.getValueInScope(currentPreferenceValue, scopeID) ?? currentPreferenceValue?.defaultValue;
195
- this.preferenceService.set(preferenceId, valueInCurrentScope, scopeID, uri);
196
- }
197
-
198
- let jsonEditorWidget: EditorWidget;
199
- const jsonUriToOpen = await this.obtainConfigUri(scopeID, activeScopeIsFolder, uri);
200
- if (jsonUriToOpen) {
201
- jsonEditorWidget = await this.editorManager.open(jsonUriToOpen);
202
-
203
- if (preferenceId) {
204
- const text = jsonEditorWidget.editor.document.getText();
205
- if (preferenceId) {
206
- const { index } = text.match(preferenceId)!;
207
- const numReturns = text.slice(0, index).match(new RegExp('\n', 'g'))!.length;
208
- jsonEditorWidget.editor.cursor = { line: numReturns, character: 4 + preferenceId.length + 4 };
209
- }
210
- }
211
- }
212
- }
213
-
214
- protected async openJson(scope: PreferenceScope, resource?: string): Promise<void> {
215
- const jsonUriToOpen = await this.obtainConfigUri(scope, false, resource);
216
- if (jsonUriToOpen) {
217
- await this.editorManager.open(jsonUriToOpen);
218
- }
219
- }
220
-
221
- /**
222
- * Prompts which workspace root folder to open the JSON settings.
223
- */
224
- protected async openFolderPreferences(callback: (root: FileStat) => unknown): Promise<void> {
225
- const roots = this.workspaceService.tryGetRoots();
226
- if (roots.length === 1) {
227
- callback(roots[0]);
228
- } else {
229
- const items: QuickPickItem[] = roots.map(root => ({
230
- label: root.name,
231
- description: root.resource.path.fsPath(),
232
- execute: () => callback(root)
233
- }));
234
- this.quickInputService?.showQuickPick(items, { placeholder: 'Select workspace folder' });
235
- }
236
- }
237
-
238
- private async obtainConfigUri(serializedScope: number, activeScopeIsFolder: boolean, resource?: string): Promise<URI | undefined> {
239
- let scope: PreferenceScope = serializedScope;
240
- if (activeScopeIsFolder) {
241
- scope = PreferenceScope.Folder;
242
- }
243
- const resourceUri = !!resource ? resource : undefined;
244
- const configUri = this.preferenceService.getConfigUri(scope, resourceUri);
245
- if (!configUri) {
246
- return undefined;
247
- }
248
- if (configUri && !await this.fileService.exists(configUri)) {
249
- await this.fileService.create(configUri);
250
- }
251
- return configUri;
252
- }
253
-
254
- /**
255
- * Determine if the current widget is the PreferencesWidget.
256
- */
257
- protected withWidget<T>(widget: Widget | undefined = this.tryGetWidget(), fn: (widget: PreferencesWidget) => T): T | false {
258
- if (widget instanceof PreferencesWidget && widget.id === PreferencesWidget.ID) {
259
- return fn(widget);
260
- }
261
- return false;
262
- }
263
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2018 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 { injectable, inject, named, optional } from '@theia/core/shared/inversify';
18
+ import { MenuModelRegistry, CommandRegistry } from '@theia/core';
19
+ import {
20
+ CommonMenus,
21
+ AbstractViewContribution,
22
+ CommonCommands,
23
+ KeybindingRegistry,
24
+ Widget,
25
+ PreferenceScope,
26
+ PreferenceProvider,
27
+ PreferenceService,
28
+ QuickInputService,
29
+ QuickPickItem,
30
+ isFirefox,
31
+ PreferenceSchemaProvider,
32
+ } from '@theia/core/lib/browser';
33
+ import { isOSX } from '@theia/core/lib/common/os';
34
+ import { TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
35
+ import { EditorManager, EditorWidget } from '@theia/editor/lib/browser';
36
+ import URI from '@theia/core/lib/common/uri';
37
+ import { PreferencesWidget } from './views/preference-widget';
38
+ import { WorkspacePreferenceProvider } from './workspace-preference-provider';
39
+ import { Preference, PreferencesCommands, PreferenceMenus } from './util/preference-types';
40
+ import { ClipboardService } from '@theia/core/lib/browser/clipboard-service';
41
+ import { FileService } from '@theia/filesystem/lib/browser/file-service';
42
+ import { WorkspaceService } from '@theia/workspace/lib/browser';
43
+ import { FileStat } from '@theia/filesystem/lib/common/files';
44
+
45
+ @injectable()
46
+ export class PreferencesContribution extends AbstractViewContribution<PreferencesWidget> {
47
+
48
+ @inject(FileService) protected readonly fileService: FileService;
49
+ @inject(PreferenceProvider) @named(PreferenceScope.Workspace) protected readonly workspacePreferenceProvider: WorkspacePreferenceProvider;
50
+ @inject(EditorManager) protected readonly editorManager: EditorManager;
51
+ @inject(PreferenceService) protected readonly preferenceService: PreferenceService;
52
+ @inject(ClipboardService) protected readonly clipboardService: ClipboardService;
53
+ @inject(PreferencesWidget) protected readonly scopeTracker: PreferencesWidget;
54
+ @inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
55
+ @inject(QuickInputService) @optional() protected readonly quickInputService: QuickInputService;
56
+ @inject(PreferenceSchemaProvider) protected readonly schema: PreferenceSchemaProvider;
57
+
58
+ constructor() {
59
+ super({
60
+ widgetId: PreferencesWidget.ID,
61
+ widgetName: PreferencesWidget.LABEL,
62
+ defaultWidgetOptions: {
63
+ area: 'main',
64
+ },
65
+ });
66
+ }
67
+
68
+ override registerCommands(commands: CommandRegistry): void {
69
+ commands.registerCommand(CommonCommands.OPEN_PREFERENCES, {
70
+ execute: async (query?: string) => {
71
+ const widget = await this.openView({ activate: true });
72
+ if (typeof query === 'string') {
73
+ widget.setSearchTerm(query);
74
+ }
75
+ },
76
+ });
77
+ commands.registerCommand(PreferencesCommands.OPEN_PREFERENCES_JSON_TOOLBAR, {
78
+ isEnabled: () => true,
79
+ isVisible: w => this.withWidget(w, () => true),
80
+ execute: (preferenceId: string) => {
81
+ this.openPreferencesJSON(preferenceId);
82
+ }
83
+ });
84
+ commands.registerCommand(PreferencesCommands.COPY_JSON_NAME, {
85
+ isEnabled: Preference.EditorCommandArgs.is,
86
+ isVisible: Preference.EditorCommandArgs.is,
87
+ execute: ({ id, value }: Preference.EditorCommandArgs) => {
88
+ this.clipboardService.writeText(id);
89
+ }
90
+ });
91
+ commands.registerCommand(PreferencesCommands.COPY_JSON_VALUE, {
92
+ isEnabled: Preference.EditorCommandArgs.is,
93
+ isVisible: Preference.EditorCommandArgs.is,
94
+ execute: ({ id, value }: { id: string, value: string; }) => {
95
+ const jsonString = `"${id}": ${JSON.stringify(value)}`;
96
+ this.clipboardService.writeText(jsonString);
97
+ }
98
+ });
99
+ commands.registerCommand(PreferencesCommands.RESET_PREFERENCE, {
100
+ isEnabled: Preference.EditorCommandArgs.is,
101
+ isVisible: Preference.EditorCommandArgs.is,
102
+ execute: ({ id }: Preference.EditorCommandArgs) => {
103
+ this.preferenceService.set(id, undefined, Number(this.scopeTracker.currentScope.scope), this.scopeTracker.currentScope.uri);
104
+ }
105
+ });
106
+ commands.registerCommand(PreferencesCommands.OPEN_USER_PREFERENCES, {
107
+ execute: async () => {
108
+ const widget = await this.openView({ activate: true });
109
+ widget.setScope(PreferenceScope.User);
110
+ }
111
+ });
112
+ commands.registerCommand(PreferencesCommands.OPEN_WORKSPACE_PREFERENCES, {
113
+ isEnabled: () => !!this.workspaceService.workspace,
114
+ isVisible: () => !!this.workspaceService.workspace,
115
+ execute: async () => {
116
+ const widget = await this.openView({ activate: true });
117
+ widget.setScope(PreferenceScope.Workspace);
118
+ }
119
+ });
120
+ commands.registerCommand(PreferencesCommands.OPEN_FOLDER_PREFERENCES, {
121
+ isEnabled: () => !!this.workspaceService.isMultiRootWorkspaceOpened && this.workspaceService.tryGetRoots().length > 0,
122
+ isVisible: () => !!this.workspaceService.isMultiRootWorkspaceOpened && this.workspaceService.tryGetRoots().length > 0,
123
+ execute: () => this.openFolderPreferences(root => {
124
+ this.openView({ activate: true });
125
+ this.scopeTracker.setScope(root.resource);
126
+ })
127
+ });
128
+ commands.registerCommand(PreferencesCommands.OPEN_USER_PREFERENCES_JSON, {
129
+ execute: async () => this.openJson(PreferenceScope.User)
130
+ });
131
+ commands.registerCommand(PreferencesCommands.OPEN_WORKSPACE_PREFERENCES_JSON, {
132
+ isEnabled: () => !!this.workspaceService.workspace,
133
+ isVisible: () => !!this.workspaceService.workspace,
134
+ execute: async () => this.openJson(PreferenceScope.Workspace)
135
+ });
136
+ commands.registerCommand(PreferencesCommands.OPEN_FOLDER_PREFERENCES_JSON, {
137
+ isEnabled: () => !!this.workspaceService.isMultiRootWorkspaceOpened && this.workspaceService.tryGetRoots().length > 0,
138
+ isVisible: () => !!this.workspaceService.isMultiRootWorkspaceOpened && this.workspaceService.tryGetRoots().length > 0,
139
+ execute: () => this.openFolderPreferences(root => this.openJson(PreferenceScope.Folder, root.resource.toString()))
140
+ });
141
+ }
142
+
143
+ override registerMenus(menus: MenuModelRegistry): void {
144
+ menus.registerMenuAction(CommonMenus.FILE_SETTINGS_SUBMENU_OPEN, {
145
+ commandId: CommonCommands.OPEN_PREFERENCES.id,
146
+ label: CommonCommands.OPEN_PREFERENCES.label,
147
+ order: 'a10',
148
+ });
149
+ menus.registerMenuAction(CommonMenus.SETTINGS_OPEN, {
150
+ commandId: CommonCommands.OPEN_PREFERENCES.id,
151
+ label: CommonCommands.OPEN_PREFERENCES.label,
152
+ order: 'a10',
153
+ });
154
+ menus.registerMenuAction(PreferenceMenus.PREFERENCE_EDITOR_CONTEXT_MENU, {
155
+ commandId: PreferencesCommands.RESET_PREFERENCE.id,
156
+ label: PreferencesCommands.RESET_PREFERENCE.label,
157
+ order: 'a'
158
+ });
159
+ menus.registerMenuAction(PreferenceMenus.PREFERENCE_EDITOR_COPY_ACTIONS, {
160
+ commandId: PreferencesCommands.COPY_JSON_VALUE.id,
161
+ label: PreferencesCommands.COPY_JSON_VALUE.label,
162
+ order: 'b'
163
+ });
164
+ menus.registerMenuAction(PreferenceMenus.PREFERENCE_EDITOR_COPY_ACTIONS, {
165
+ commandId: PreferencesCommands.COPY_JSON_NAME.id,
166
+ label: PreferencesCommands.COPY_JSON_NAME.label,
167
+ order: 'c'
168
+ });
169
+ }
170
+
171
+ override registerKeybindings(keybindings: KeybindingRegistry): void {
172
+ keybindings.registerKeybinding({
173
+ command: CommonCommands.OPEN_PREFERENCES.id,
174
+ keybinding: (isOSX && !isFirefox) ? 'cmd+,' : 'ctrl+,'
175
+ });
176
+ }
177
+
178
+ registerToolbarItems(toolbar: TabBarToolbarRegistry): void {
179
+ toolbar.registerItem({
180
+ id: PreferencesCommands.OPEN_PREFERENCES_JSON_TOOLBAR.id,
181
+ command: PreferencesCommands.OPEN_PREFERENCES_JSON_TOOLBAR.id,
182
+ tooltip: PreferencesCommands.OPEN_USER_PREFERENCES_JSON.label,
183
+ priority: 0,
184
+ });
185
+ }
186
+
187
+ protected async openPreferencesJSON(opener: string | PreferencesWidget): Promise<void> {
188
+ const { scope, activeScopeIsFolder, uri } = this.scopeTracker.currentScope;
189
+ const scopeID = Number(scope);
190
+ let preferenceId = '';
191
+ if (typeof opener === 'string') {
192
+ preferenceId = opener;
193
+ const currentPreferenceValue = this.preferenceService.inspect(preferenceId, uri);
194
+ const valueInCurrentScope = Preference.getValueInScope(currentPreferenceValue, scopeID) ?? currentPreferenceValue?.defaultValue;
195
+ this.preferenceService.set(preferenceId, valueInCurrentScope, scopeID, uri);
196
+ }
197
+
198
+ let jsonEditorWidget: EditorWidget;
199
+ const jsonUriToOpen = await this.obtainConfigUri(scopeID, activeScopeIsFolder, uri);
200
+ if (jsonUriToOpen) {
201
+ jsonEditorWidget = await this.editorManager.open(jsonUriToOpen);
202
+
203
+ if (preferenceId) {
204
+ const text = jsonEditorWidget.editor.document.getText();
205
+ if (preferenceId) {
206
+ const { index } = text.match(preferenceId)!;
207
+ const numReturns = text.slice(0, index).match(new RegExp('\n', 'g'))!.length;
208
+ jsonEditorWidget.editor.cursor = { line: numReturns, character: 4 + preferenceId.length + 4 };
209
+ }
210
+ }
211
+ }
212
+ }
213
+
214
+ protected async openJson(scope: PreferenceScope, resource?: string): Promise<void> {
215
+ const jsonUriToOpen = await this.obtainConfigUri(scope, false, resource);
216
+ if (jsonUriToOpen) {
217
+ await this.editorManager.open(jsonUriToOpen);
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Prompts which workspace root folder to open the JSON settings.
223
+ */
224
+ protected async openFolderPreferences(callback: (root: FileStat) => unknown): Promise<void> {
225
+ const roots = this.workspaceService.tryGetRoots();
226
+ if (roots.length === 1) {
227
+ callback(roots[0]);
228
+ } else {
229
+ const items: QuickPickItem[] = roots.map(root => ({
230
+ label: root.name,
231
+ description: root.resource.path.fsPath(),
232
+ execute: () => callback(root)
233
+ }));
234
+ this.quickInputService?.showQuickPick(items, { placeholder: 'Select workspace folder' });
235
+ }
236
+ }
237
+
238
+ private async obtainConfigUri(serializedScope: number, activeScopeIsFolder: boolean, resource?: string): Promise<URI | undefined> {
239
+ let scope: PreferenceScope = serializedScope;
240
+ if (activeScopeIsFolder) {
241
+ scope = PreferenceScope.Folder;
242
+ }
243
+ const resourceUri = !!resource ? resource : undefined;
244
+ const configUri = this.preferenceService.getConfigUri(scope, resourceUri);
245
+ if (!configUri) {
246
+ return undefined;
247
+ }
248
+ if (configUri && !await this.fileService.exists(configUri)) {
249
+ await this.fileService.create(configUri);
250
+ }
251
+ return configUri;
252
+ }
253
+
254
+ /**
255
+ * Determine if the current widget is the PreferencesWidget.
256
+ */
257
+ protected withWidget<T>(widget: Widget | undefined = this.tryGetWidget(), fn: (widget: PreferencesWidget) => T): T | false {
258
+ if (widget instanceof PreferencesWidget && widget.id === PreferencesWidget.ID) {
259
+ return fn(widget);
260
+ }
261
+ return false;
262
+ }
263
+ }