@theia/workspace 1.77.0-next.0 → 1.77.0-next.101

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 (202) hide show
  1. package/lib/browser/canonical-uri-service.js +12 -16
  2. package/lib/browser/canonical-uri-service.js.map +1 -1
  3. package/lib/browser/diff-service.js +23 -29
  4. package/lib/browser/diff-service.js.map +1 -1
  5. package/lib/browser/index.d.ts +8 -8
  6. package/lib/browser/index.d.ts.map +1 -1
  7. package/lib/browser/index.js +8 -11
  8. package/lib/browser/index.js.map +1 -1
  9. package/lib/browser/metadata-storage/index.d.ts +2 -2
  10. package/lib/browser/metadata-storage/index.d.ts.map +1 -1
  11. package/lib/browser/metadata-storage/index.js +2 -9
  12. package/lib/browser/metadata-storage/index.js.map +1 -1
  13. package/lib/browser/metadata-storage/workspace-metadata-storage-service.d.ts +6 -2
  14. package/lib/browser/metadata-storage/workspace-metadata-storage-service.d.ts.map +1 -1
  15. package/lib/browser/metadata-storage/workspace-metadata-storage-service.js +34 -36
  16. package/lib/browser/metadata-storage/workspace-metadata-storage-service.js.map +1 -1
  17. package/lib/browser/metadata-storage/workspace-metadata-storage-service.spec.js +70 -73
  18. package/lib/browser/metadata-storage/workspace-metadata-storage-service.spec.js.map +1 -1
  19. package/lib/browser/metadata-storage/workspace-metadata-store.d.ts +1 -1
  20. package/lib/browser/metadata-storage/workspace-metadata-store.d.ts.map +1 -1
  21. package/lib/browser/metadata-storage/workspace-metadata-store.js +19 -28
  22. package/lib/browser/metadata-storage/workspace-metadata-store.js.map +1 -1
  23. package/lib/browser/quick-open-workspace.d.ts +2 -2
  24. package/lib/browser/quick-open-workspace.d.ts.map +1 -1
  25. package/lib/browser/quick-open-workspace.js +31 -40
  26. package/lib/browser/quick-open-workspace.js.map +1 -1
  27. package/lib/browser/test/with-workspace-service-defaults.d.ts +8 -0
  28. package/lib/browser/test/with-workspace-service-defaults.d.ts.map +1 -0
  29. package/lib/browser/test/with-workspace-service-defaults.js +25 -0
  30. package/lib/browser/test/with-workspace-service-defaults.js.map +1 -0
  31. package/lib/browser/untitled-workspace-exit-dialog.js +13 -17
  32. package/lib/browser/untitled-workspace-exit-dialog.js.map +1 -1
  33. package/lib/browser/workspace-breadcrumbs-contribution.d.ts +3 -1
  34. package/lib/browser/workspace-breadcrumbs-contribution.d.ts.map +1 -1
  35. package/lib/browser/workspace-breadcrumbs-contribution.js +23 -14
  36. package/lib/browser/workspace-breadcrumbs-contribution.js.map +1 -1
  37. package/lib/browser/workspace-commands.d.ts +5 -5
  38. package/lib/browser/workspace-commands.d.ts.map +1 -1
  39. package/lib/browser/workspace-commands.js +129 -146
  40. package/lib/browser/workspace-commands.js.map +1 -1
  41. package/lib/browser/workspace-commands.spec.js +50 -52
  42. package/lib/browser/workspace-commands.spec.js.map +1 -1
  43. package/lib/browser/workspace-compare-handler.d.ts +1 -1
  44. package/lib/browser/workspace-compare-handler.d.ts.map +1 -1
  45. package/lib/browser/workspace-compare-handler.js +8 -12
  46. package/lib/browser/workspace-compare-handler.js.map +1 -1
  47. package/lib/browser/workspace-delete-handler.d.ts +2 -2
  48. package/lib/browser/workspace-delete-handler.d.ts.map +1 -1
  49. package/lib/browser/workspace-delete-handler.js +42 -51
  50. package/lib/browser/workspace-delete-handler.js.map +1 -1
  51. package/lib/browser/workspace-duplicate-handler.d.ts +2 -2
  52. package/lib/browser/workspace-duplicate-handler.d.ts.map +1 -1
  53. package/lib/browser/workspace-duplicate-handler.js +20 -27
  54. package/lib/browser/workspace-duplicate-handler.js.map +1 -1
  55. package/lib/browser/workspace-files-exclude-filter-provider.d.ts +1 -1
  56. package/lib/browser/workspace-files-exclude-filter-provider.d.ts.map +1 -1
  57. package/lib/browser/workspace-files-exclude-filter-provider.js +12 -19
  58. package/lib/browser/workspace-files-exclude-filter-provider.js.map +1 -1
  59. package/lib/browser/workspace-frontend-contribution.d.ts +4 -4
  60. package/lib/browser/workspace-frontend-contribution.d.ts.map +1 -1
  61. package/lib/browser/workspace-frontend-contribution.js +129 -143
  62. package/lib/browser/workspace-frontend-contribution.js.map +1 -1
  63. package/lib/browser/workspace-frontend-module.js +85 -87
  64. package/lib/browser/workspace-frontend-module.js.map +1 -1
  65. package/lib/browser/workspace-input-dialog.js +13 -18
  66. package/lib/browser/workspace-input-dialog.js.map +1 -1
  67. package/lib/browser/workspace-schema-updater.d.ts +1 -1
  68. package/lib/browser/workspace-schema-updater.d.ts.map +1 -1
  69. package/lib/browser/workspace-schema-updater.js +36 -40
  70. package/lib/browser/workspace-schema-updater.js.map +1 -1
  71. package/lib/browser/workspace-search-filter-service.js +13 -20
  72. package/lib/browser/workspace-search-filter-service.js.map +1 -1
  73. package/lib/browser/workspace-search-filter-service.spec.js +18 -20
  74. package/lib/browser/workspace-search-filter-service.spec.js.map +1 -1
  75. package/lib/browser/workspace-service.d.ts +134 -5
  76. package/lib/browser/workspace-service.d.ts.map +1 -1
  77. package/lib/browser/workspace-service.js +409 -157
  78. package/lib/browser/workspace-service.js.map +1 -1
  79. package/lib/browser/workspace-service.spec.d.ts +2 -0
  80. package/lib/browser/workspace-service.spec.d.ts.map +1 -0
  81. package/lib/browser/workspace-service.spec.js +425 -0
  82. package/lib/browser/workspace-service.spec.js.map +1 -0
  83. package/lib/browser/workspace-storage-service.d.ts +1 -1
  84. package/lib/browser/workspace-storage-service.d.ts.map +1 -1
  85. package/lib/browser/workspace-storage-service.js +13 -21
  86. package/lib/browser/workspace-storage-service.js.map +1 -1
  87. package/lib/browser/workspace-trust-dialog.js +9 -13
  88. package/lib/browser/workspace-trust-dialog.js.map +1 -1
  89. package/lib/browser/workspace-trust-service.d.ts +3 -3
  90. package/lib/browser/workspace-trust-service.d.ts.map +1 -1
  91. package/lib/browser/workspace-trust-service.js +106 -127
  92. package/lib/browser/workspace-trust-service.js.map +1 -1
  93. package/lib/browser/workspace-trust-service.spec.js +146 -148
  94. package/lib/browser/workspace-trust-service.spec.js.map +1 -1
  95. package/lib/browser/workspace-uri-contribution.d.ts +27 -2
  96. package/lib/browser/workspace-uri-contribution.d.ts.map +1 -1
  97. package/lib/browser/workspace-uri-contribution.js +79 -32
  98. package/lib/browser/workspace-uri-contribution.js.map +1 -1
  99. package/lib/browser/workspace-uri-contribution.spec.js +130 -70
  100. package/lib/browser/workspace-uri-contribution.spec.js.map +1 -1
  101. package/lib/browser/workspace-user-working-directory-provider.d.ts +1 -1
  102. package/lib/browser/workspace-user-working-directory-provider.d.ts.map +1 -1
  103. package/lib/browser/workspace-user-working-directory-provider.js +13 -18
  104. package/lib/browser/workspace-user-working-directory-provider.js.map +1 -1
  105. package/lib/browser/workspace-utils.d.ts +1 -1
  106. package/lib/browser/workspace-utils.d.ts.map +1 -1
  107. package/lib/browser/workspace-utils.js +8 -12
  108. package/lib/browser/workspace-utils.js.map +1 -1
  109. package/lib/browser/workspace-variable-contribution.d.ts +1 -1
  110. package/lib/browser/workspace-variable-contribution.d.ts.map +1 -1
  111. package/lib/browser/workspace-variable-contribution.js +37 -47
  112. package/lib/browser/workspace-variable-contribution.js.map +1 -1
  113. package/lib/browser/workspace-window-title-updater.d.ts +1 -1
  114. package/lib/browser/workspace-window-title-updater.d.ts.map +1 -1
  115. package/lib/browser/workspace-window-title-updater.js +12 -16
  116. package/lib/browser/workspace-window-title-updater.js.map +1 -1
  117. package/lib/browser-only/browser-only-workspace-server.d.ts +1 -1
  118. package/lib/browser-only/browser-only-workspace-server.d.ts.map +1 -1
  119. package/lib/browser-only/browser-only-workspace-server.js +17 -22
  120. package/lib/browser-only/browser-only-workspace-server.js.map +1 -1
  121. package/lib/browser-only/workspace-frontend-only-module.js +8 -10
  122. package/lib/browser-only/workspace-frontend-only-module.js.map +1 -1
  123. package/lib/common/index.d.ts +4 -4
  124. package/lib/common/index.d.ts.map +1 -1
  125. package/lib/common/index.js +4 -7
  126. package/lib/common/index.js.map +1 -1
  127. package/lib/common/test/mock-workspace-server.d.ts +1 -1
  128. package/lib/common/test/mock-workspace-server.d.ts.map +1 -1
  129. package/lib/common/test/mock-workspace-server.js +5 -8
  130. package/lib/common/test/mock-workspace-server.js.map +1 -1
  131. package/lib/common/untitled-workspace-service.d.ts +1 -1
  132. package/lib/common/untitled-workspace-service.d.ts.map +1 -1
  133. package/lib/common/untitled-workspace-service.js +8 -12
  134. package/lib/common/untitled-workspace-service.js.map +1 -1
  135. package/lib/common/workspace-file-service.js +11 -14
  136. package/lib/common/workspace-file-service.js.map +1 -1
  137. package/lib/common/workspace-preferences.js +14 -19
  138. package/lib/common/workspace-preferences.js.map +1 -1
  139. package/lib/common/workspace-protocol.js +2 -5
  140. package/lib/common/workspace-protocol.js.map +1 -1
  141. package/lib/common/workspace-trust-preferences.js +29 -34
  142. package/lib/common/workspace-trust-preferences.js.map +1 -1
  143. package/lib/electron-browser/electron-workspace-contribution.d.ts +1 -1
  144. package/lib/electron-browser/electron-workspace-contribution.d.ts.map +1 -1
  145. package/lib/electron-browser/electron-workspace-contribution.js +11 -15
  146. package/lib/electron-browser/electron-workspace-contribution.js.map +1 -1
  147. package/lib/electron-browser/electron-workspace-module.js +6 -8
  148. package/lib/electron-browser/electron-workspace-module.js.map +1 -1
  149. package/lib/node/default-workspace-server.d.ts +1 -1
  150. package/lib/node/default-workspace-server.d.ts.map +1 -1
  151. package/lib/node/default-workspace-server.js +51 -63
  152. package/lib/node/default-workspace-server.js.map +1 -1
  153. package/lib/node/default-workspace-server.spec.js +29 -31
  154. package/lib/node/default-workspace-server.spec.js.map +1 -1
  155. package/lib/node/index.d.ts +2 -2
  156. package/lib/node/index.d.ts.map +1 -1
  157. package/lib/node/index.js +2 -5
  158. package/lib/node/index.js.map +1 -1
  159. package/lib/node/workspace-backend-module.js +21 -23
  160. package/lib/node/workspace-backend-module.js.map +1 -1
  161. package/package.json +17 -5
  162. package/src/browser/index.ts +8 -8
  163. package/src/browser/metadata-storage/index.ts +2 -2
  164. package/src/browser/metadata-storage/workspace-metadata-storage-service.spec.ts +4 -5
  165. package/src/browser/metadata-storage/workspace-metadata-storage-service.ts +10 -3
  166. package/src/browser/metadata-storage/workspace-metadata-store.ts +1 -1
  167. package/src/browser/quick-open-workspace.ts +2 -2
  168. package/src/browser/test/with-workspace-service-defaults.ts +26 -0
  169. package/src/browser/workspace-breadcrumbs-contribution.ts +16 -1
  170. package/src/browser/workspace-commands.spec.ts +6 -6
  171. package/src/browser/workspace-commands.ts +7 -8
  172. package/src/browser/workspace-compare-handler.ts +1 -1
  173. package/src/browser/workspace-delete-handler.ts +2 -2
  174. package/src/browser/workspace-duplicate-handler.ts +2 -2
  175. package/src/browser/workspace-files-exclude-filter-provider.ts +1 -1
  176. package/src/browser/workspace-frontend-contribution.ts +10 -8
  177. package/src/browser/workspace-frontend-module.ts +25 -25
  178. package/src/browser/workspace-schema-updater.ts +5 -1
  179. package/src/browser/workspace-search-filter-service.spec.ts +1 -1
  180. package/src/browser/workspace-service.spec.ts +508 -0
  181. package/src/browser/workspace-service.ts +353 -44
  182. package/src/browser/workspace-storage-service.ts +1 -1
  183. package/src/browser/workspace-trust-service.spec.ts +2 -2
  184. package/src/browser/workspace-trust-service.ts +5 -5
  185. package/src/browser/workspace-uri-contribution.spec.ts +76 -6
  186. package/src/browser/workspace-uri-contribution.ts +61 -4
  187. package/src/browser/workspace-user-working-directory-provider.ts +1 -1
  188. package/src/browser/workspace-utils.ts +1 -1
  189. package/src/browser/workspace-variable-contribution.ts +1 -1
  190. package/src/browser/workspace-window-title-updater.ts +1 -1
  191. package/src/browser-only/browser-only-workspace-server.ts +1 -1
  192. package/src/browser-only/workspace-frontend-only-module.ts +2 -2
  193. package/src/common/index.ts +4 -4
  194. package/src/common/test/mock-workspace-server.ts +1 -1
  195. package/src/common/untitled-workspace-service.ts +1 -1
  196. package/src/electron-browser/electron-workspace-contribution.ts +1 -1
  197. package/src/electron-browser/electron-workspace-module.ts +1 -1
  198. package/src/node/default-workspace-server.spec.ts +2 -2
  199. package/src/node/default-workspace-server.ts +1 -1
  200. package/src/node/index.ts +2 -2
  201. package/src/node/workspace-backend-module.ts +3 -3
  202. package/src/typings/valid-filename.d.ts +20 -0
@@ -0,0 +1,508 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2026 STMicroelectronics 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 { enableJSDOM } from '@theia/core/lib/browser/test/jsdom';
18
+ let disableJSDOM = enableJSDOM();
19
+
20
+ import { FrontendApplicationConfigProvider } from '@theia/core/lib/browser/frontend-application-config-provider';
21
+ try { FrontendApplicationConfigProvider.set({}); } catch { /* already set by a sibling spec */ }
22
+
23
+ import { expect } from 'chai';
24
+ import URI from '@theia/core/lib/common/uri';
25
+ import { OS } from '@theia/core/lib/common/os';
26
+ import { FileStat } from '@theia/filesystem/lib/common/files';
27
+ import { WorkspaceData, WorkspaceService } from './workspace-service.js';
28
+
29
+ disableJSDOM();
30
+
31
+ describe('WorkspaceData', () => {
32
+
33
+ before(() => {
34
+ disableJSDOM = enableJSDOM();
35
+ });
36
+ after(() => {
37
+ disableJSDOM();
38
+ });
39
+
40
+ const workspaceFile = FileStat.file('file:///home/user/project/project.theia-workspace');
41
+
42
+ describe('is()', () => {
43
+
44
+ it('should accept a workspace file with a folder name that is not a string', () => {
45
+ // Such a file loads as in VS Code, with the folder falling back to its folder name,
46
+ // instead of opening without roots and dropping the other contents of the file on the next write.
47
+ expect(WorkspaceData.is({ folders: [{ path: 'backend', name: 42 }], settings: { 'editor.tabSize': 2 } })).to.be.true;
48
+ expect(WorkspaceData.is({ folders: [{ path: 'backend', name: {} }] })).to.be.true;
49
+ });
50
+
51
+ it('should still reject a workspace file without valid folders', () => {
52
+ expect(WorkspaceData.is({ folders: [{ name: 'API Server' }] })).to.be.false;
53
+ expect(WorkspaceData.is({ settings: {} })).to.be.false;
54
+ });
55
+ });
56
+
57
+ describe('buildWorkspaceData()', () => {
58
+
59
+ it('should keep the names of the folders that are still present', () => {
60
+ const data = WorkspaceData.buildWorkspaceData(
61
+ [FileStat.dir('file:///home/user/project/backend'), FileStat.dir('file:///home/user/project/frontend')],
62
+ { folders: [{ path: 'file:///home/user/project/backend/', name: 'API Server' }, { path: 'file:///home/user/project/removed', name: 'Removed' }] }
63
+ );
64
+ expect(data.folders).to.deep.equal([
65
+ { path: 'file:///home/user/project/backend', name: 'API Server' },
66
+ { path: 'file:///home/user/project/frontend' }
67
+ ]);
68
+ });
69
+
70
+ it('should use the name of a folder entry and fall back to the current name', () => {
71
+ const data = WorkspaceData.buildWorkspaceData(
72
+ [{ path: 'file:///home/user/project/backend' }, { path: 'file:///home/user/project/frontend', name: 'Web Client' }],
73
+ { folders: [{ path: 'file:///home/user/project/backend', name: 'API Server' }, { path: 'file:///home/user/project/frontend', name: 'Old Name' }] }
74
+ );
75
+ expect(data.folders).to.deep.equal([
76
+ { path: 'file:///home/user/project/backend', name: 'API Server' },
77
+ { path: 'file:///home/user/project/frontend', name: 'Web Client' }
78
+ ]);
79
+ });
80
+ });
81
+
82
+ describe('transformToRelative()', () => {
83
+
84
+ it('should keep the names of the folders', () => {
85
+ const data = WorkspaceData.transformToRelative({
86
+ folders: [{ path: 'file:///home/user/project/backend', name: 'API Server' }, { path: 'file:///home/user/project/frontend' }]
87
+ }, workspaceFile);
88
+ expect(data.folders).to.deep.equal([{ path: 'backend', name: 'API Server' }, { path: 'frontend' }]);
89
+ });
90
+ });
91
+
92
+ describe('transformToAbsolute()', () => {
93
+
94
+ it('should keep the names of the folders', () => {
95
+ const data = WorkspaceData.transformToAbsolute({
96
+ folders: [{ path: 'backend', name: 'API Server' }, { path: 'frontend' }]
97
+ }, workspaceFile);
98
+ expect(data.folders).to.deep.equal([
99
+ { path: 'file:///home/user/project/backend', name: 'API Server' },
100
+ { path: 'file:///home/user/project/frontend' }
101
+ ]);
102
+ });
103
+
104
+ it('should drop folder names that are not strings and keep the rest of the file', () => {
105
+ const data = WorkspaceData.transformToAbsolute({
106
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
107
+ folders: [{ path: 'backend', name: 42 as any }, { path: 'frontend', name: {} as any }],
108
+ settings: { 'editor.tabSize': 2 }
109
+ }, workspaceFile);
110
+ expect(data.folders).to.deep.equal([
111
+ { path: 'file:///home/user/project/backend' },
112
+ { path: 'file:///home/user/project/frontend' }
113
+ ]);
114
+ expect(data.settings).to.deep.equal({ 'editor.tabSize': 2 });
115
+ });
116
+ });
117
+ });
118
+
119
+ describe('WorkspaceService', () => {
120
+
121
+ before(() => {
122
+ disableJSDOM = enableJSDOM();
123
+ });
124
+ after(() => {
125
+ disableJSDOM();
126
+ });
127
+
128
+ const backend = 'file:///home/user/project/backend';
129
+ const frontend = 'file:///home/user/project/frontend';
130
+ const shared = 'file:///home/user/shared';
131
+
132
+ class TestWorkspaceService extends WorkspaceService {
133
+ computedRoots: FileStat[] = [];
134
+ names = new Map<string, string>();
135
+ fileData: WorkspaceData | undefined;
136
+ writtenData: WorkspaceData | undefined;
137
+ protected override async computeRoots(): Promise<FileStat[]> {
138
+ // Like reading the workspace file, set the names only after an `await`
139
+ await Promise.resolve();
140
+ this.computedRootNames = this.names;
141
+ return this.computedRoots;
142
+ }
143
+ protected override async getWorkspaceDataFromFile(): Promise<WorkspaceData | undefined> {
144
+ return this.fileData;
145
+ }
146
+ protected override async writeWorkspaceFile(workspaceFile: FileStat | undefined, workspaceData: WorkspaceData): Promise<FileStat | undefined> {
147
+ this.writtenData = workspaceData;
148
+ return workspaceFile;
149
+ }
150
+ protected override async updateWorkspace(): Promise<void> {
151
+ }
152
+ override updateRoots(): Promise<void> {
153
+ return super.updateRoots();
154
+ }
155
+ setWorkspaceFile(workspaceFile: FileStat, roots: FileStat[]): void {
156
+ this._workspace = workspaceFile;
157
+ this._roots = roots;
158
+ }
159
+ }
160
+
161
+ let service: TestWorkspaceService;
162
+ let changeEvents: number;
163
+
164
+ beforeEach(() => {
165
+ service = new TestWorkspaceService();
166
+ service.computedRoots = [FileStat.dir(backend)];
167
+ changeEvents = 0;
168
+ service.onWorkspaceChanged(() => changeEvents++);
169
+ });
170
+
171
+ describe('getConfiguredRootName()', () => {
172
+
173
+ it('should return the name of a root', async () => {
174
+ service.names = new Map([[backend, 'API Server']]);
175
+ await service.updateRoots();
176
+ expect(service.getConfiguredRootName(new URI(backend))).to.equal('API Server');
177
+ expect(service.getConfiguredRootName(new URI(`${backend}/`))).to.equal('API Server');
178
+ });
179
+
180
+ it('should return undefined for a root without a name', async () => {
181
+ await service.updateRoots();
182
+ expect(service.getConfiguredRootName(new URI(backend))).to.be.undefined;
183
+ });
184
+
185
+ it('should return the current name after the names changed', async () => {
186
+ service.names = new Map([[backend, 'API Server'], [frontend, 'Web Client']]);
187
+ await service.updateRoots();
188
+ expect(service.getConfiguredRootName(new URI(backend))).to.equal('API Server');
189
+ service.names = new Map([[backend, 'Backend'], [frontend, 'Web Client']]);
190
+ await service.updateRoots();
191
+ expect(service.getConfiguredRootName(new URI(backend))).to.equal('Backend');
192
+ });
193
+ });
194
+
195
+ describe('updateRoots()', () => {
196
+
197
+ it('should fire onWorkspaceChanged when only the name of a root changes', async () => {
198
+ await service.updateRoots();
199
+ expect(changeEvents).to.equal(1);
200
+
201
+ service.names = new Map([[backend, 'API Server']]);
202
+ await service.updateRoots();
203
+ expect(changeEvents).to.equal(2);
204
+ expect(service.getConfiguredRootName(new URI(backend))).to.equal('API Server');
205
+ });
206
+
207
+ it('should not fire onWorkspaceChanged when neither the roots nor their names change', async () => {
208
+ service.names = new Map([[backend, 'API Server']]);
209
+ await service.updateRoots();
210
+ service.names = new Map([[backend, 'API Server']]);
211
+ await service.updateRoots();
212
+ expect(changeEvents).to.equal(1);
213
+ });
214
+
215
+ it('should fire onWorkspaceChanged once when overlapping calls see the same rename', async () => {
216
+ await service.updateRoots();
217
+ service.names = new Map([[backend, 'API Server']]);
218
+ await Promise.all([service.updateRoots(), service.updateRoots()]);
219
+ expect(changeEvents).to.equal(2);
220
+ });
221
+ });
222
+
223
+ describe('writing the workspace file', () => {
224
+
225
+ beforeEach(() => {
226
+ service.setWorkspaceFile(FileStat.file('file:///home/user/project/project.theia-workspace'), [FileStat.dir(backend), FileStat.dir(frontend)]);
227
+ service.fileData = { folders: [{ path: backend, name: 'API Server' }, { path: frontend }] };
228
+ });
229
+
230
+ it('should keep the names of the roots and write the name of an added root', async () => {
231
+ await service.spliceRoots(2, 0, { uri: new URI(shared), name: 'Shared' }, new URI('file:///home/user/other'));
232
+ expect(service.writtenData?.folders).to.deep.equal([
233
+ { path: backend, name: 'API Server' },
234
+ { path: frontend },
235
+ { path: shared, name: 'Shared' },
236
+ { path: 'file:///home/user/other' }
237
+ ]);
238
+ });
239
+
240
+ it('should keep the names of the remaining roots when removing a root', async () => {
241
+ await service.removeRoots([new URI(frontend)]);
242
+ expect(service.writtenData?.folders).to.deep.equal([{ path: backend, name: 'API Server' }]);
243
+ });
244
+
245
+ it('should rename a root that is removed and added again in its place', async () => {
246
+ await service.spliceRoots(1, 1, { uri: new URI(frontend), name: 'Web Client' });
247
+ expect(service.writtenData?.folders).to.deep.equal([
248
+ { path: backend, name: 'API Server' },
249
+ { path: frontend, name: 'Web Client' }
250
+ ]);
251
+ });
252
+
253
+ it('should reset the name of a root that is removed and added again without a name', async () => {
254
+ await service.spliceRoots(0, 1, new URI(backend));
255
+ expect(service.writtenData?.folders).to.deep.equal([{ path: backend }, { path: frontend }]);
256
+ });
257
+
258
+ it('should not add a root that is not removed by the same call again', async () => {
259
+ await service.spliceRoots(1, 1, { uri: new URI(backend), name: 'Other' });
260
+ expect(service.writtenData?.folders).to.deep.equal([{ path: backend, name: 'API Server' }]);
261
+ });
262
+ });
263
+ });
264
+
265
+ describe('WorkspaceService root names', () => {
266
+
267
+ let originalIsWindows: boolean;
268
+ before(() => {
269
+ disableJSDOM = enableJSDOM();
270
+ });
271
+ after(() => {
272
+ disableJSDOM();
273
+ });
274
+ beforeEach(() => {
275
+ // Paths outside the workspace are rendered with `Path.fsPath()`, which branches on the backend OS.
276
+ originalIsWindows = OS.backend.isWindows;
277
+ OS.backend.isWindows = false;
278
+ });
279
+ afterEach(() => {
280
+ OS.backend.isWindows = originalIsWindows;
281
+ });
282
+
283
+ const workspaceWith = (...roots: string[]): WorkspaceService => Object.create(WorkspaceService.prototype, {
284
+ tryGetRoots: { value: () => roots.map(root => ({ resource: new URI(root), isDirectory: true })) }
285
+ });
286
+
287
+ const namesOf = (service: WorkspaceService): Record<string, string> =>
288
+ Object.fromEntries(Array.from(service.getRootNames(), ([name, uri]) => [name, uri.toString()]));
289
+
290
+ describe('getRootNames', () => {
291
+
292
+ it('names each root after its basename', () => {
293
+ expect(namesOf(workspaceWith('file:///home/user/frontend', 'file:///home/user/backend'))).to.deep.equal({
294
+ frontend: 'file:///home/user/frontend',
295
+ backend: 'file:///home/user/backend'
296
+ });
297
+ });
298
+
299
+ it('names all roots sharing a basename by the shortest tail that tells them apart', () => {
300
+ expect(namesOf(workspaceWith('file:///bob/app', 'file:///alice/app', 'file:///home/other'))).to.deep.equal({
301
+ 'bob/app': 'file:///bob/app',
302
+ 'alice/app': 'file:///alice/app',
303
+ other: 'file:///home/other'
304
+ });
305
+ });
306
+
307
+ it('uses as many parent segments as needed', () => {
308
+ expect(namesOf(workspaceWith('file:///a/src/app', 'file:///b/src/app', 'file:///b/lib/app'))).to.deep.equal({
309
+ 'a/src/app': 'file:///a/src/app',
310
+ 'b/src/app': 'file:///b/src/app',
311
+ 'lib/app': 'file:///b/lib/app'
312
+ });
313
+ });
314
+
315
+ it('names a root with a shorter path than its peer by its whole path', () => {
316
+ expect(namesOf(workspaceWith('file:///app', 'file:///x/app'))).to.deep.equal({
317
+ app: 'file:///app',
318
+ 'x/app': 'file:///x/app'
319
+ });
320
+ });
321
+
322
+ it('qualifies a name that another root name starts with', () => {
323
+ expect(namesOf(workspaceWith('file:///work/alice', 'file:///home/alice/app', 'file:///home/bob/app'))).to.deep.equal({
324
+ 'work/alice': 'file:///work/alice',
325
+ 'alice/app': 'file:///home/alice/app',
326
+ 'bob/app': 'file:///home/bob/app'
327
+ });
328
+ });
329
+
330
+ it('qualifies the longer name if the shorter one is a whole path', () => {
331
+ expect(namesOf(workspaceWith('file:///alice', 'file:///home/alice/app', 'file:///home/bob/app'))).to.deep.equal({
332
+ alice: 'file:///alice',
333
+ 'home/alice/app': 'file:///home/alice/app',
334
+ 'bob/app': 'file:///home/bob/app'
335
+ });
336
+ });
337
+
338
+ it('does not qualify a name that the name of a root nested in it starts with', () => {
339
+ expect(namesOf(workspaceWith('file:///ws/x', 'file:///ws/x/app', 'file:///other/app'))).to.deep.equal({
340
+ x: 'file:///ws/x',
341
+ 'x/app': 'file:///ws/x/app',
342
+ 'other/app': 'file:///other/app'
343
+ });
344
+ });
345
+
346
+ it('does not depend on the order of the roots', () => {
347
+ expect(namesOf(workspaceWith('file:///alice/app', 'file:///bob/app')))
348
+ .to.deep.equal(namesOf(workspaceWith('file:///bob/app', 'file:///alice/app')));
349
+ });
350
+
351
+ it('names a root listed twice once, after its basename', () => {
352
+ expect(namesOf(workspaceWith('file:///home/user/app', 'file:///home/user/app'))).to.deep.equal({
353
+ app: 'file:///home/user/app'
354
+ });
355
+ });
356
+
357
+ it('returns the same names until the roots change', async () => {
358
+ let roots = [FileStat.dir('file:///alice/app')];
359
+ const service = new WorkspaceService();
360
+ service['computeRoots'] = async () => roots;
361
+ await service['updateRoots']();
362
+ const names = service.getRootNames();
363
+ expect(service.getRootNames()).to.equal(names);
364
+ roots = [...roots, FileStat.dir('file:///bob/app')];
365
+ await service['updateRoots']();
366
+ expect(Array.from(service.getRootNames().keys())).to.deep.equal(['alice/app', 'bob/app']);
367
+ expect(service.getRootName(new URI('file:///bob/app'))).to.equal('bob/app');
368
+ });
369
+
370
+ it('falls back to the URI for roots with the same path', () => {
371
+ expect(namesOf(workspaceWith('file:///x/app', 'memfs:/x/app'))).to.deep.equal({
372
+ 'file:///x/app': 'file:///x/app',
373
+ 'memfs:/x/app': 'memfs:/x/app'
374
+ });
375
+ });
376
+ });
377
+
378
+ describe('getRootPrefixedPath', () => {
379
+
380
+ it('prefixes the path with the name of the root', () => {
381
+ const service = workspaceWith('file:///alice/app', 'file:///bob/app');
382
+ expect(service.getRootPrefixedPath(new URI('file:///bob/app/src/index.ts'))).to.equal('bob/app/src/index.ts');
383
+ expect(service.getRootPrefixedPath(new URI('file:///bob/app'))).to.equal('bob/app');
384
+ });
385
+
386
+ it('uses the deepest root for nested roots', () => {
387
+ const service = workspaceWith('file:///ws/theia', 'file:///ws/theia/packages/core');
388
+ expect(service.getRootPrefixedPath(new URI('file:///ws/theia/packages/core/src/a.ts'))).to.equal('core/src/a.ts');
389
+ });
390
+
391
+ it('returns the absolute path for files outside the workspace', () => {
392
+ const service = workspaceWith('file:///ws/theia');
393
+ expect(service.getRootPrefixedPath(new URI('file:///tmp/a.ts'))).to.equal('/tmp/a.ts');
394
+ });
395
+ });
396
+
397
+ describe('resolveSupraRelativePath', () => {
398
+
399
+ const resolve = (service: WorkspaceService, path: string): string[] => service.resolveSupraRelativePath(path).map(uri => uri.toString());
400
+
401
+ it('resolves a path that starts with the basename of a root', () => {
402
+ const service = workspaceWith('file:///home/user/frontend', 'file:///home/user/backend');
403
+ expect(resolve(service, 'backend/src/index.ts')).to.deep.equal(['file:///home/user/backend/src/index.ts']);
404
+ expect(resolve(service, 'backend')).to.deep.equal(['file:///home/user/backend']);
405
+ });
406
+
407
+ it('resolves a path prefixed with more of the root path than its name', () => {
408
+ const service = workspaceWith('file:///alice/app');
409
+ expect(resolve(service, 'alice/app/src/index.ts')).to.deep.equal(['file:///alice/app/src/index.ts']);
410
+ expect(resolve(service, 'bob/app/src/index.ts')).to.deep.equal([]);
411
+ });
412
+
413
+ it('resolves a basename shared by several roots in each of them', () => {
414
+ const service = workspaceWith('file:///alice/app', 'file:///bob/app');
415
+ expect(resolve(service, 'app/src/index.ts')).to.deep.equal(['file:///alice/app/src/index.ts', 'file:///bob/app/src/index.ts']);
416
+ expect(resolve(service, 'bob/app/src/index.ts')).to.deep.equal(['file:///bob/app/src/index.ts']);
417
+ });
418
+
419
+ it('does not resolve absolute paths, paths leaving the root or paths without a root basename', () => {
420
+ const service = workspaceWith('file:///home/user/backend');
421
+ expect(resolve(service, '/home/user/backend/a.ts')).to.deep.equal([]);
422
+ expect(resolve(service, 'C:\\backend\\a.ts')).to.deep.equal([]);
423
+ expect(resolve(service, '../backend/a.ts')).to.deep.equal([]);
424
+ expect(resolve(service, 'src/index.ts')).to.deep.equal([]);
425
+ });
426
+ });
427
+
428
+ describe('splitRootPrefixedPath', () => {
429
+
430
+ const split = (service: WorkspaceService, path: string): { rootName: string; rootUri: string; rest: string } | undefined => {
431
+ const result = service.splitRootPrefixedPath(path);
432
+ return result && { ...result, rootUri: result.rootUri.toString() };
433
+ };
434
+
435
+ it('splits off the longest matching root name and keeps the rest as it is', () => {
436
+ const service = workspaceWith('file:///ws/x', 'file:///ws/x/app', 'file:///other/app');
437
+ expect(split(service, 'x/app/src/**/\\*.ts')).to.deep.equal({ rootName: 'x/app', rootUri: 'file:///ws/x/app', rest: 'src/**/\\*.ts' });
438
+ expect(split(service, 'x/lib/{a,b}.ts')).to.deep.equal({ rootName: 'x', rootUri: 'file:///ws/x', rest: 'lib/{a,b}.ts' });
439
+ expect(split(service, 'other/app')).to.deep.equal({ rootName: 'other/app', rootUri: 'file:///other/app', rest: '' });
440
+ });
441
+
442
+ it('does not split a path without a root name', () => {
443
+ const service = workspaceWith('file:///alice/app', 'file:///bob/app');
444
+ expect(split(service, 'app/src/index.ts')).to.be.undefined;
445
+ });
446
+ });
447
+
448
+ describe('resolveRootPrefixedPath', () => {
449
+
450
+ const resolve = (service: WorkspaceService, path: string): string | undefined => service.resolveRootPrefixedPath(path)?.toString();
451
+
452
+ it('resolves a path relative to the named root', () => {
453
+ const service = workspaceWith('file:///home/user/frontend', 'file:///home/user/backend');
454
+ expect(resolve(service, 'backend/src/index.ts')).to.equal('file:///home/user/backend/src/index.ts');
455
+ expect(resolve(service, 'backend')).to.equal('file:///home/user/backend');
456
+ });
457
+
458
+ it('resolves parent-qualified root names', () => {
459
+ const service = workspaceWith('file:///alice/app', 'file:///bob/app');
460
+ expect(resolve(service, 'bob/app/src/index.ts')).to.equal('file:///bob/app/src/index.ts');
461
+ expect(resolve(service, 'alice/app')).to.equal('file:///alice/app');
462
+ });
463
+
464
+ it('does not resolve a basename shared by several roots', () => {
465
+ const service = workspaceWith('file:///alice/app', 'file:///bob/app');
466
+ expect(resolve(service, 'app/src/index.ts')).to.be.undefined;
467
+ });
468
+
469
+ it('prefers the longest matching root name', () => {
470
+ const service = workspaceWith('file:///ws/x', 'file:///ws/x/app', 'file:///other/app');
471
+ expect(resolve(service, 'x/app/src/index.ts')).to.equal('file:///ws/x/app/src/index.ts');
472
+ expect(resolve(service, 'x/lib/index.ts')).to.equal('file:///ws/x/lib/index.ts');
473
+ });
474
+
475
+ it('accepts backslashes and redundant segments', () => {
476
+ const service = workspaceWith('file:///home/user/backend');
477
+ expect(resolve(service, 'backend\\src\\index.ts')).to.equal('file:///home/user/backend/src/index.ts');
478
+ expect(resolve(service, './backend/src/../lib/a.ts')).to.equal('file:///home/user/backend/lib/a.ts');
479
+ });
480
+
481
+ it('does not resolve absolute paths, URIs or paths leaving the root', () => {
482
+ const service = workspaceWith('file:///home/user/backend');
483
+ expect(resolve(service, '/home/user/backend/a.ts')).to.be.undefined;
484
+ expect(resolve(service, 'C:\\backend\\a.ts')).to.be.undefined;
485
+ expect(resolve(service, 'file:///home/user/backend/a.ts')).to.be.undefined;
486
+ expect(resolve(service, 'backend/../../etc/passwd')).to.be.undefined;
487
+ });
488
+
489
+ it('does not resolve paths without a root name', () => {
490
+ const service = workspaceWith('file:///home/user/backend');
491
+ expect(resolve(service, 'src/index.ts')).to.be.undefined;
492
+ });
493
+
494
+ it('is the inverse of getRootPrefixedPath', () => {
495
+ const service = workspaceWith('file:///alice/app', 'file:///bob/app', 'file:///ws/theia', 'file:///ws/theia/packages/core');
496
+ for (const file of ['file:///alice/app/a.ts', 'file:///bob/app/a.ts', 'file:///ws/theia/b.ts', 'file:///ws/theia/packages/core/c.ts']) {
497
+ expect(resolve(service, service.getRootPrefixedPath(new URI(file)))).to.equal(file);
498
+ }
499
+ });
500
+
501
+ it('is the inverse of getRootPrefixedPath for a root with a folder named like another root', () => {
502
+ const service = workspaceWith('file:///work/alice', 'file:///home/alice/app', 'file:///home/bob/app');
503
+ for (const file of ['file:///work/alice/app/x.ts', 'file:///home/alice/app/x.ts']) {
504
+ expect(resolve(service, service.getRootPrefixedPath(new URI(file)))).to.equal(file);
505
+ }
506
+ });
507
+ });
508
+ });