@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
@@ -16,21 +16,22 @@
16
16
 
17
17
  import { injectable, inject, postConstruct, named } from '@theia/core/shared/inversify';
18
18
  import URI from '@theia/core/lib/common/uri';
19
- import { WorkspaceServer, UntitledWorkspaceService, WorkspaceFileService } from '../common';
19
+ import { Path } from '@theia/core/lib/common/path';
20
+ import { WorkspaceServer, UntitledWorkspaceService, WorkspaceFileService } from '../common/index.js';
20
21
  import { WindowService } from '@theia/core/lib/browser/window/window-service';
21
22
  import { DEFAULT_WINDOW_HASH } from '@theia/core/lib/common/window';
22
23
  import { FrontendApplicationContribution, LabelProvider, OnWillStopAction } from '@theia/core/lib/browser';
23
24
  import { Deferred } from '@theia/core/lib/common/promise-util';
24
25
  import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
25
26
  import { ILogger, Disposable, DisposableCollection, Emitter, environment, Event, MaybePromise, MessageService, nls, ContributionProvider } from '@theia/core';
26
- import { WorkspacePreferences } from '../common/workspace-preferences';
27
+ import { WorkspacePreferences } from '../common/workspace-preferences.js';
27
28
  import * as jsoncparser from 'jsonc-parser';
28
- import * as Ajv from '@theia/core/shared/ajv';
29
+ import Ajv from '@theia/core/shared/ajv';
29
30
  import { FileStat, BaseStat } from '@theia/filesystem/lib/common/files';
30
31
  import { FileService } from '@theia/filesystem/lib/browser/file-service';
31
32
  import { WindowTitleService } from '@theia/core/lib/browser/window/window-title-service';
32
33
  import { FileSystemPreferences } from '@theia/filesystem/lib/common';
33
- import { workspaceSchema, WorkspaceSchemaUpdater } from './workspace-schema-updater';
34
+ import { workspaceSchema, WorkspaceSchemaUpdater } from './workspace-schema-updater.js';
34
35
  import { IJSONSchema } from '@theia/core/lib/common/json-schema';
35
36
  import { StopReason } from '@theia/core/lib/common/frontend-application-state';
36
37
  import { PreferenceSchemaService, PreferenceScope, PreferenceService } from '@theia/core/lib/common/preferences';
@@ -57,6 +58,10 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
57
58
  protected _workspace: FileStat | undefined;
58
59
 
59
60
  protected _roots: FileStat[] = [];
61
+ /** The names of the roots, published together with `_roots` when `onWorkspaceChanged` fires. */
62
+ protected configuredRootNames = new Map<string, string>();
63
+ /** The names of the roots read by the last `computeRoots`, not yet published. */
64
+ protected computedRootNames = new Map<string, string>();
60
65
  protected deferredRoots = new Deferred<FileStat[]>();
61
66
 
62
67
  @inject(FileService)
@@ -299,7 +304,9 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
299
304
 
300
305
  protected async updateRoots(): Promise<void> {
301
306
  const newRoots = await this.computeRoots();
302
- let rootsChanged = false;
307
+ const newRootNames = this.computedRootNames;
308
+ // Compare against the published names, so that overlapping calls fire only once for a rename
309
+ let rootsChanged = !this.areConfiguredRootNamesEqual(this.configuredRootNames, newRootNames);
303
310
  if (newRoots.length !== this._roots.length || newRoots.length === 0) {
304
311
  rootsChanged = true;
305
312
  } else {
@@ -312,6 +319,8 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
312
319
  }
313
320
  if (rootsChanged) {
314
321
  this._roots = newRoots;
322
+ this.configuredRootNames = newRootNames;
323
+ this.rootNamesCache = undefined;
315
324
  this.deferredRoots.resolve(this._roots); // in order to resolve first
316
325
  this.deferredRoots = new Deferred<FileStat[]>();
317
326
  this.deferredRoots.resolve(this._roots);
@@ -319,28 +328,44 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
319
328
  }
320
329
  }
321
330
 
331
+ /**
332
+ * Computes the workspace roots. Also sets `computedRootNames` to the names given to the roots in the workspace file,
333
+ * which `updateRoots` publishes with the roots, see `getConfiguredRootName`.
334
+ * An override that does not call `super` has to set `computedRootNames` itself, otherwise the names of the previous roots remain.
335
+ */
322
336
  protected async computeRoots(): Promise<FileStat[]> {
323
337
  const roots: FileStat[] = [];
338
+ const rootNames = new Map<string, string>();
324
339
  if (this._workspace) {
325
340
  if (this._workspace.isDirectory) {
341
+ this.computedRootNames = rootNames;
326
342
  return [this._workspace];
327
343
  }
328
344
 
329
345
  const workspaceData = await this.getWorkspaceDataFromFile();
330
346
  if (workspaceData) {
331
- for (const { path } of workspaceData.folders) {
347
+ for (const { path, name } of workspaceData.folders) {
332
348
  const valid = await this.toValidRoot(path);
333
349
  if (valid) {
334
350
  roots.push(valid);
335
351
  } else {
336
352
  roots.push(FileStat.dir(path));
337
353
  }
354
+ // `getWorkspaceDataFromFile` already drops invalid names, but an override of it may not
355
+ if (typeof name === 'string' && name) {
356
+ rootNames.set(WorkspaceData.normalizeFolderUri(path), name);
357
+ }
338
358
  }
339
359
  }
340
360
  }
361
+ this.computedRootNames = rootNames;
341
362
  return roots;
342
363
  }
343
364
 
365
+ protected areConfiguredRootNamesEqual(names: Map<string, string>, otherNames: Map<string, string>): boolean {
366
+ return names.size === otherNames.size && Array.from(names).every(([root, name]) => otherNames.get(root) === name);
367
+ }
368
+
344
369
  protected async getWorkspaceDataFromFile(): Promise<WorkspaceData | undefined> {
345
370
  if (this._workspace && await this.fileService.exists(this._workspace.resource)) {
346
371
  if (this._workspace.isDirectory) {
@@ -535,7 +560,12 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
535
560
  }
536
561
  }
537
562
 
538
- async spliceRoots(start: number, deleteCount?: number, ...rootsToAdd: URI[]): Promise<URI[]> {
563
+ /**
564
+ * Removes `deleteCount` roots starting at `start` and inserts the given roots there, and writes the result to the workspace file.
565
+ * A root to add can be given a name, which is written to the workspace file as the `name` of its folder entry.
566
+ * @returns the URIs of the removed roots
567
+ */
568
+ async spliceRoots(start: number, deleteCount?: number, ...rootsToAdd: (URI | WorkspaceRootToAdd)[]): Promise<URI[]> {
539
569
  if (!this._workspace || this._workspace.isDirectory) {
540
570
  const untitledWorkspace = await this.getUntitledWorkspace();
541
571
  await this.save(untitledWorkspace);
@@ -543,23 +573,35 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
543
573
  throw new Error('Could not create new untitled workspace');
544
574
  }
545
575
  }
546
- const dedup = new Set<string>();
547
- const roots = this._roots.map(root => (dedup.add(root.resource.toString()), root.resource.toString()));
576
+ const roots = this._roots.map(root => root.resource.toString());
577
+ const toRemove = roots.splice(start, deleteCount || 0);
578
+ // Deduplicate against the remaining roots only, so that a root removed by this call can be added again, e.g. to rename it.
579
+ const dedup = new Set<string>(roots);
548
580
  const toAdd: string[] = [];
581
+ const namesToAdd = new Map<string, string>();
549
582
  for (const root of rootsToAdd) {
550
- const uri = root.toString();
551
- if (!dedup.has(uri)) {
552
- dedup.add(uri);
553
- toAdd.push(uri);
583
+ const { uri, name } = root instanceof URI ? { uri: root, name: undefined } : root;
584
+ const uriString = uri.toString();
585
+ if (!dedup.has(uriString)) {
586
+ dedup.add(uriString);
587
+ toAdd.push(uriString);
588
+ if (name) {
589
+ namesToAdd.set(uriString, name);
590
+ }
554
591
  }
555
592
  }
556
- const toRemove = roots.splice(start, deleteCount || 0, ...toAdd);
593
+ roots.splice(start, 0, ...toAdd);
557
594
  if (!toRemove.length && !toAdd.length) {
558
595
  return [];
559
596
  }
560
597
 
561
598
  const currentData = await this.getWorkspaceDataFromFile();
562
- const newData = WorkspaceData.buildWorkspaceData(roots, currentData);
599
+ if (currentData) {
600
+ // A root added by this call only gets the name it is added with, so that adding a root again without a name resets its name
601
+ const added = new Set(toAdd.map(root => WorkspaceData.normalizeFolderUri(root)));
602
+ currentData.folders = currentData.folders.filter(folder => !added.has(WorkspaceData.normalizeFolderUri(folder.path)));
603
+ }
604
+ const newData = WorkspaceData.buildWorkspaceData(roots.map(path => ({ path, name: namesToAdd.get(path) })), currentData);
563
605
  await this.writeWorkspaceFile(this._workspace, newData);
564
606
  await this.updateWorkspace();
565
607
  return toRemove.map(root => new URI(root));
@@ -596,6 +638,7 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
596
638
  this.windowService.setSafeToShutDown();
597
639
  this._workspace = undefined;
598
640
  this._roots.length = 0;
641
+ this.rootNamesCache = undefined;
599
642
 
600
643
  if (!this.isRemoteSession()) {
601
644
  await this.server.setMostRecentlyUsedWorkspace('');
@@ -622,12 +665,8 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
622
665
  if (!uri) {
623
666
  return undefined;
624
667
  }
625
- let uriStr = uri.toString();
626
668
  try {
627
- if (uriStr.endsWith('/')) {
628
- uriStr = uriStr.slice(0, -1);
629
- }
630
- const normalizedUri = new URI(uriStr).normalizePath();
669
+ const normalizedUri = new URI(WorkspaceData.normalizeFolderUri(uri.toString()));
631
670
  return await this.fileService.resolve(normalizedUri);
632
671
  } catch (error) {
633
672
  return undefined;
@@ -800,6 +839,35 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
800
839
  return rootUris.sort((r1, r2) => r2.toString().length - r1.toString().length)[0];
801
840
  }
802
841
 
842
+ /**
843
+ * Returns the name given to the workspace root by the `name` property of its folder entry in the workspace file.
844
+ * This is the name shown for the root, as `WorkspaceFolder.name` in VS Code. Unlike the unique names of {@link getRootNames},
845
+ * it is chosen by the user and not used to address files.
846
+ * @param rootUri URI of the workspace root
847
+ * @returns the name of the root, or `undefined` if the root has no name or is not a workspace root
848
+ */
849
+ getConfiguredRootName(rootUri: URI): string | undefined {
850
+ if (this.configuredRootNames.size === 0) {
851
+ return undefined;
852
+ }
853
+ // Called for every file label, e.g. by `getDetails`, so normalize each root URI only once per set of names
854
+ if (this.configuredRootNamesByUri?.names !== this.configuredRootNames) {
855
+ this.configuredRootNamesByUri = { names: this.configuredRootNames, byUri: new Map() };
856
+ }
857
+ const key = rootUri.toString();
858
+ const { byUri } = this.configuredRootNamesByUri;
859
+ if (!byUri.has(key)) {
860
+ byUri.set(key, this.configuredRootNames.get(WorkspaceData.normalizeFolderUri(key)));
861
+ }
862
+ return byUri.get(key);
863
+ }
864
+
865
+ /**
866
+ * The configured root names by the string of a root URI as passed to `getConfiguredRootName`,
867
+ * for the `configuredRootNames` they were looked up in.
868
+ */
869
+ protected configuredRootNamesByUri: { names: Map<string, string>; byUri: Map<string, string | undefined> } | undefined;
870
+
803
871
  /**
804
872
  * Returns the relative path of the given file to the workspace root.
805
873
  * @param uri URI of the file
@@ -833,7 +901,7 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
833
901
  if (!rootUri) {
834
902
  return uri.path.fsPath();
835
903
  }
836
- const rootName = rootUri.path.base;
904
+ const rootName = this.getRootName(rootUri) ?? rootUri.path.base;
837
905
  const relative = rootUri.relative(uri);
838
906
  if (!relative || relative.toString() === '') {
839
907
  return rootName;
@@ -841,6 +909,217 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
841
909
  return `${rootName}/${relative.toString()}`;
842
910
  }
843
911
 
912
+ /**
913
+ * Returns a unique name for each workspace root, in workspace order.
914
+ *
915
+ * A root is named after its directory basename, e.g. `backend`. Roots that share a basename are
916
+ * treated as peers: each of them is named after the shortest tail of its path that tells them
917
+ * apart, e.g. `alice/app` and `bob/app`. A name that another name starts with, e.g. `alice` next to
918
+ * `alice/app`, is qualified in the same way, e.g. `work/alice`, so that every path resolves to the root
919
+ * it came from. The names are thus always the ending of the real path, so they match what appears in
920
+ * terminal output, compiler errors and the like.
921
+ *
922
+ * These are the root names used by {@link getRootPrefixedPath} and resolved by {@link resolveRootPrefixedPath}.
923
+ */
924
+ getRootNames(): ReadonlyMap<string, URI> {
925
+ return this.getRootNamesCache().names;
926
+ }
927
+
928
+ /**
929
+ * The root names, and the names by root URI, which depend on each other. Reset whenever the roots change.
930
+ */
931
+ protected rootNamesCache: { names: ReadonlyMap<string, URI>; namesByRoot: ReadonlyMap<string, string> } | undefined;
932
+
933
+ protected getRootNamesCache(): { names: ReadonlyMap<string, URI>; namesByRoot: ReadonlyMap<string, string> } {
934
+ if (!this.rootNamesCache) {
935
+ const names = this.computeRootNames(this.tryGetRoots());
936
+ const namesByRoot = new Map(Array.from(names, ([name, uri]) => [uri.toString(), name]));
937
+ this.rootNamesCache = { names, namesByRoot };
938
+ }
939
+ return this.rootNamesCache;
940
+ }
941
+
942
+ protected computeRootNames(workspaceRoots: FileStat[]): Map<string, URI> {
943
+ const roots: URI[] = [];
944
+ for (const root of workspaceRoots) {
945
+ if (!roots.some(other => other.isEqual(root.resource))) {
946
+ roots.push(root.resource);
947
+ }
948
+ }
949
+ const byBasename = new Map<string, URI[]>();
950
+ for (const root of roots) {
951
+ const peers = byBasename.get(root.path.base) ?? [];
952
+ peers.push(root);
953
+ byBasename.set(root.path.base, peers);
954
+ }
955
+ const names = new Map<string, URI>();
956
+ for (const root of roots) {
957
+ names.set(this.computeRootName(root, byBasename.get(root.path.base)!), root);
958
+ }
959
+ return this.qualifyRootNames(names);
960
+ }
961
+
962
+ /**
963
+ * Returns the name of the given workspace root, as assigned by {@link getRootNames}, or
964
+ * `undefined` if the URI is not a workspace root.
965
+ */
966
+ getRootName(rootUri: URI): string | undefined {
967
+ return this.getRootNamesCache().namesByRoot.get(rootUri.toString());
968
+ }
969
+
970
+ /**
971
+ * Resolves a path in the format of {@link getRootPrefixedPath}, i.e. `<rootName>/<relativePath>`
972
+ * or a bare `<rootName>`, to a URI. If more than one root name is a prefix of the path, which is
973
+ * the case when a root named `a` has a subfolder `b` and another root is named `a/b`, the longest
974
+ * one wins.
975
+ *
976
+ * @returns `undefined` if the path is absolute, does not start with a root name or leaves its root.
977
+ */
978
+ resolveRootPrefixedPath(rootPrefixedPath: string): URI | undefined {
979
+ const path = new Path(Path.normalizePathSeparator(rootPrefixedPath)).normalize();
980
+ if (path.isAbsolute || Path.isDrive(path.toString().split('/')[0])) {
981
+ return undefined;
982
+ }
983
+ const segments = path.toString().split('/').filter(segment => segment.length > 0);
984
+ if (segments.includes('..')) {
985
+ return undefined;
986
+ }
987
+ const match = this.splitRootPrefixedPath(segments.join('/'));
988
+ if (!match) {
989
+ return undefined;
990
+ }
991
+ return match.rest ? match.rootUri.resolve(match.rest) : match.rootUri;
992
+ }
993
+
994
+ /**
995
+ * Resolves a path that contains the basename of a workspace root, preceded by nothing or by the
996
+ * trailing segments of the root's parent path, e.g. `bob/app/src` for the root `/home/bob/app`.
997
+ * Unlike {@link resolveRootPrefixedPath}, this does not depend on the current root names, so it
998
+ * also resolves paths that were prefixed with a root name before the workspace roots changed.
999
+ *
1000
+ * @returns the distinct candidates in workspace order, which are several if the path is ambiguous,
1001
+ * or none if the path is absolute, leaves its root or does not contain the basename of a root.
1002
+ */
1003
+ resolveSupraRelativePath(supraRelativePath: string): URI[] {
1004
+ const path = new Path(Path.normalizePathSeparator(supraRelativePath)).normalize();
1005
+ const segments = path.toString().split('/').filter(segment => segment.length > 0);
1006
+ if (path.isAbsolute || Path.isDrive(segments[0] ?? '') || segments.includes('..')) {
1007
+ return [];
1008
+ }
1009
+ const candidates: URI[] = [];
1010
+ for (const root of this.tryGetRoots()) {
1011
+ const candidate = this.resolveSupraRelativeSegments(root.resource, segments);
1012
+ if (candidate && !candidates.some(other => other.isEqual(candidate))) {
1013
+ candidates.push(candidate);
1014
+ }
1015
+ }
1016
+ return candidates;
1017
+ }
1018
+
1019
+ /**
1020
+ * Resolves `segments` against the given root as described in {@link resolveSupraRelativePath}.
1021
+ */
1022
+ protected resolveSupraRelativeSegments(rootUri: URI, segments: string[]): URI | undefined {
1023
+ const rootBasename = rootUri.path.base;
1024
+ const rootPathSegments = rootUri.path.toString().split('/').filter(segment => segment.length > 0);
1025
+ const rootPrecedingSegments = rootPathSegments.slice(0, rootPathSegments.length - 1);
1026
+ for (let i = 0; i < segments.length; i++) {
1027
+ if (segments[i] !== rootBasename) {
1028
+ continue;
1029
+ }
1030
+ const pathPrecedingSegments = segments.slice(0, i);
1031
+ if (pathPrecedingSegments.length > rootPrecedingSegments.length) {
1032
+ continue;
1033
+ }
1034
+ const rootTail = rootPrecedingSegments.slice(rootPrecedingSegments.length - pathPrecedingSegments.length);
1035
+ if (pathPrecedingSegments.every((segment, j) => segment === rootTail[j])) {
1036
+ const rest = segments.slice(i + 1).join('/');
1037
+ return rest ? rootUri.resolve(rest) : rootUri;
1038
+ }
1039
+ }
1040
+ return undefined;
1041
+ }
1042
+
1043
+ /**
1044
+ * Splits a `/`-separated path that starts with a root name, as in {@link getRootPrefixedPath}, into
1045
+ * that root and the rest of the path, which is empty for a bare root name. The rest is not normalized,
1046
+ * so it may as well be a glob. If more than one root name is a prefix of the path, the longest one wins.
1047
+ *
1048
+ * @returns `undefined` if the path does not start with a root name.
1049
+ */
1050
+ splitRootPrefixedPath(rootPrefixedPath: string): { rootName: string; rootUri: URI; rest: string } | undefined {
1051
+ const segments = rootPrefixedPath.split('/');
1052
+ let match: { rootName: string; rootUri: URI; length: number } | undefined;
1053
+ for (const [rootName, rootUri] of this.getRootNames()) {
1054
+ const nameSegments = rootName.split('/');
1055
+ const matches = nameSegments.length <= segments.length && nameSegments.every((segment, i) => segment === segments[i]);
1056
+ if (matches && (!match || nameSegments.length > match.length)) {
1057
+ match = { rootName, rootUri, length: nameSegments.length };
1058
+ }
1059
+ }
1060
+ return match && { rootName: match.rootName, rootUri: match.rootUri, rest: segments.slice(match.length).join('/') };
1061
+ }
1062
+
1063
+ /**
1064
+ * Computes the name of a root from the roots that share its basename (including itself).
1065
+ */
1066
+ protected computeRootName(root: URI, peers: URI[]): string {
1067
+ if (peers.length === 1) {
1068
+ return root.path.base || root.path.toString();
1069
+ }
1070
+ const segmentsOf = (uri: URI): string[] => uri.path.toString().split('/').filter(segment => segment.length > 0);
1071
+ const rootSegments = segmentsOf(root);
1072
+ const peerSegments = peers.filter(peer => !peer.isEqual(root)).map(segmentsOf);
1073
+ // A root with a shorter path than a peer, e.g. `/app` next to `/x/app`, is named by its whole path, `app`.
1074
+ const maxLength = Math.max(rootSegments.length, ...peerSegments.map(segments => segments.length));
1075
+ for (let length = 2; length <= maxLength; length++) {
1076
+ const tail = rootSegments.slice(-length).join('/');
1077
+ if (peerSegments.every(segments => segments.slice(-length).join('/') !== tail)) {
1078
+ return tail;
1079
+ }
1080
+ }
1081
+ // The paths are the same, e.g. in different URI schemes, so only the full URI tells them apart.
1082
+ return root.toString();
1083
+ }
1084
+
1085
+ /**
1086
+ * Qualifies a root name that another root name starts with, e.g. `alice` next to `alice/app`, by more
1087
+ * segments of its path, as a path in the one root would otherwise resolve to the other. If the root
1088
+ * named `alice/app` is the `app` folder of the root named `alice`, the names are left as they are:
1089
+ * the nested root is then also the one that {@link getRootPrefixedPath} names its files after.
1090
+ */
1091
+ protected qualifyRootNames(names: Map<string, URI>): Map<string, URI> {
1092
+ const entries = Array.from(names, ([name, root]) => ({ name, root }));
1093
+ let qualified = true;
1094
+ while (qualified) {
1095
+ qualified = false;
1096
+ for (const shorter of entries) {
1097
+ for (const longer of entries) {
1098
+ if (longer.name.startsWith(shorter.name + '/')
1099
+ && !shorter.root.resolve(longer.name.substring(shorter.name.length + 1)).isEqual(longer.root)) {
1100
+ qualified = this.qualifyRootName(shorter) || this.qualifyRootName(longer) || qualified;
1101
+ }
1102
+ }
1103
+ }
1104
+ }
1105
+ return new Map(entries.map(({ name, root }) => [name, root]));
1106
+ }
1107
+
1108
+ /**
1109
+ * Adds the next parent segment to the name of a root, unless the name is its whole path or its URI already.
1110
+ *
1111
+ * @returns whether the name was qualified
1112
+ */
1113
+ protected qualifyRootName(entry: { name: string; root: URI }): boolean {
1114
+ const segments = entry.root.path.toString().split('/').filter(segment => segment.length > 0);
1115
+ const length = entry.name.split('/').length;
1116
+ if (entry.name === entry.root.toString() || length >= segments.length) {
1117
+ return false;
1118
+ }
1119
+ entry.name = segments.slice(-(length + 1)).join('/');
1120
+ return true;
1121
+ }
1122
+
844
1123
  areWorkspaceRoots(uris: URI[]): boolean {
845
1124
  if (!uris.length) {
846
1125
  return false;
@@ -885,6 +1164,12 @@ export interface WorkspaceInput {
885
1164
 
886
1165
  }
887
1166
 
1167
+ export interface WorkspaceRootToAdd {
1168
+ uri: URI;
1169
+ /** The name of the root, written to the workspace file as the `name` of its folder entry. */
1170
+ name?: string;
1171
+ }
1172
+
888
1173
  export interface WorkspaceData {
889
1174
  folders: Array<{ path: string, name?: string }>;
890
1175
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -892,23 +1177,52 @@ export interface WorkspaceData {
892
1177
  }
893
1178
 
894
1179
  export namespace WorkspaceData {
895
- const validateSchema = new Ajv().compile(workspaceSchema);
1180
+ // The `name` of a folder is not validated when loading, so that a workspace file with an invalid name still loads,
1181
+ // and the folder falls back to its folder name, as in VS Code. The editor still validates it against `workspaceSchema`.
1182
+ const validateSchema = new Ajv().compile(withoutFolderName(workspaceSchema));
1183
+
1184
+ function withoutFolderName(schema: IJSONSchema): IJSONSchema {
1185
+ const copy: IJSONSchema = structuredClone(schema);
1186
+ delete (copy.properties?.folders.items as IJSONSchema | undefined)?.properties?.name;
1187
+ return copy;
1188
+ }
1189
+
1190
+ function isValidFolderName(name: unknown): name is string {
1191
+ return typeof name === 'string' && name.length > 0;
1192
+ }
896
1193
 
897
1194
  export function is(data: unknown): data is WorkspaceData {
898
1195
  return !!validateSchema(data);
899
1196
  }
900
1197
 
901
- export function buildWorkspaceData(folders: string[] | FileStat[], additionalFields?: Partial<WorkspaceData>): WorkspaceData {
902
- const roots = new Set<string>();
903
- if (folders.length > 0) {
904
- if (typeof folders[0] !== 'string') {
905
- (<FileStat[]>folders).forEach(folder => roots.add(folder.resource.toString()));
906
- } else {
907
- (<string[]>folders).forEach(folder => roots.add(folder));
1198
+ /**
1199
+ * Normalizes the given folder URI the same way as the URIs of the workspace roots, so that they can be compared.
1200
+ */
1201
+ export function normalizeFolderUri(folderUri: string): string {
1202
+ return new URI(folderUri.endsWith('/') ? folderUri.slice(0, -1) : folderUri).normalizePath().toString();
1203
+ }
1204
+
1205
+ /**
1206
+ * Builds the workspace data for the given folders. A folder given as an entry with a `name` keeps that name,
1207
+ * any other folder keeps the `name` of its entry in `additionalFields.folders`. Names that are not strings are dropped.
1208
+ */
1209
+ export function buildWorkspaceData(folders: string[] | FileStat[] | WorkspaceData['folders'], additionalFields?: Partial<WorkspaceData>): WorkspaceData {
1210
+ const currentNames = new Map<string, string>();
1211
+ for (const { path, name } of additionalFields?.folders ?? []) {
1212
+ if (isValidFolderName(name)) {
1213
+ currentNames.set(normalizeFolderUri(path), name);
1214
+ }
1215
+ }
1216
+ const roots = new Map<string, string | undefined>();
1217
+ for (const folder of folders) {
1218
+ const path = typeof folder === 'string' ? folder : FileStat.is(folder) ? folder.resource.toString() : folder.path;
1219
+ if (!roots.has(path)) {
1220
+ const name = typeof folder === 'string' || FileStat.is(folder) ? undefined : folder.name;
1221
+ roots.set(path, isValidFolderName(name) ? name : currentNames.get(normalizeFolderUri(path)));
908
1222
  }
909
1223
  }
910
1224
  const data: WorkspaceData = {
911
- folders: Array.from(roots, folder => ({ path: folder }))
1225
+ folders: Array.from(roots, ([path, name]) => name ? { path, name } : { path })
912
1226
  };
913
1227
  if (additionalFields) {
914
1228
  delete additionalFields.folders;
@@ -918,36 +1232,31 @@ export namespace WorkspaceData {
918
1232
  }
919
1233
 
920
1234
  export function transformToRelative(data: WorkspaceData, workspaceFile?: FileStat): WorkspaceData {
921
- const folderUris: string[] = [];
1235
+ const folders: WorkspaceData['folders'] = [];
922
1236
  const workspaceFileUri = new URI(workspaceFile ? workspaceFile.resource.toString() : '').withScheme('file');
923
- for (const { path } of data.folders) {
1237
+ for (const { path, name } of data.folders) {
924
1238
  const folderUri = new URI(path).withScheme('file');
925
1239
  const rel = workspaceFileUri.parent.relative(folderUri);
926
- if (rel) {
927
- folderUris.push(rel.toString());
928
- } else {
929
- folderUris.push(folderUri.toString());
930
- }
1240
+ folders.push({ path: rel ? rel.toString() : folderUri.toString(), name });
931
1241
  }
932
- return buildWorkspaceData(folderUris, data);
1242
+ return buildWorkspaceData(folders, { ...data, folders: [] });
933
1243
  }
934
1244
 
935
1245
  export function transformToAbsolute(data: WorkspaceData, workspaceFile?: BaseStat): WorkspaceData {
936
1246
  if (workspaceFile) {
937
- const folders: string[] = [];
938
- for (const folder of data.folders) {
939
- const path = folder.path;
1247
+ const folders: WorkspaceData['folders'] = [];
1248
+ for (const { path, name } of data.folders) {
940
1249
  if (path.startsWith('file:///')) {
941
- folders.push(path);
1250
+ folders.push({ path, name });
942
1251
  } else {
943
1252
  const absolutePath = workspaceFile.resource.withScheme('file').parent.resolveToAbsolute(path)?.toString();
944
1253
  if (absolutePath) {
945
- folders.push(absolutePath.toString());
1254
+ folders.push({ path: absolutePath.toString(), name });
946
1255
  }
947
1256
  }
948
1257
 
949
1258
  }
950
- return Object.assign(data, buildWorkspaceData(folders, data));
1259
+ return Object.assign(data, buildWorkspaceData(folders, { ...data, folders: [] }));
951
1260
  }
952
1261
  return data;
953
1262
  }
@@ -16,7 +16,7 @@
16
16
 
17
17
  import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
18
18
  import { LocalStorageService, StorageService } from '@theia/core/lib/browser/storage-service';
19
- import { WorkspaceService } from './workspace-service';
19
+ import { WorkspaceService } from './workspace-service.js';
20
20
  import { FileStat } from '@theia/filesystem/lib/common/files';
21
21
 
22
22
  /*
@@ -17,14 +17,14 @@
17
17
  import { expect } from 'chai';
18
18
  import * as sinon from 'sinon';
19
19
  import { PreferenceChange, PreferenceScope } from '@theia/core/lib/common/preferences';
20
- import { WorkspaceTrustService } from './workspace-trust-service';
20
+ import { WorkspaceTrustService } from './workspace-trust-service.js';
21
21
  import {
22
22
  WORKSPACE_TRUST_EMPTY_WINDOW,
23
23
  WORKSPACE_TRUST_ENABLED,
24
24
  WORKSPACE_TRUST_STARTUP_PROMPT,
25
25
  WORKSPACE_TRUST_TRUSTED_FOLDERS,
26
26
  WorkspaceTrustPrompt
27
- } from '../common/workspace-trust-preferences';
27
+ } from '../common/workspace-trust-preferences.js';
28
28
  import URI from '@theia/core/lib/common/uri';
29
29
 
30
30
  class TestableWorkspaceTrustService extends WorkspaceTrustService {
@@ -28,13 +28,13 @@ import { inject, injectable, named, postConstruct, preDestroy } from '@theia/cor
28
28
  import { WindowService } from '@theia/core/lib/browser/window/window-service';
29
29
  import {
30
30
  WorkspaceTrustPreferences, WORKSPACE_TRUST_EMPTY_WINDOW, WORKSPACE_TRUST_ENABLED, WORKSPACE_TRUST_STARTUP_PROMPT, WORKSPACE_TRUST_TRUSTED_FOLDERS, WorkspaceTrustPrompt
31
- } from '../common/workspace-trust-preferences';
31
+ } from '../common/workspace-trust-preferences.js';
32
32
  import { FrontendApplicationConfigProvider } from '@theia/core/lib/browser/frontend-application-config-provider';
33
- import { WorkspaceService } from './workspace-service';
34
- import { WorkspaceCommands } from './workspace-commands';
33
+ import { WorkspaceService } from './workspace-service.js';
34
+ import { WorkspaceCommands } from './workspace-commands.js';
35
35
  import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
36
- import { WorkspaceTrustDialog } from './workspace-trust-dialog';
37
- import { UntitledWorkspaceService } from '../common/untitled-workspace-service';
36
+ import { WorkspaceTrustDialog } from './workspace-trust-dialog.js';
37
+ import { UntitledWorkspaceService } from '../common/untitled-workspace-service.js';
38
38
  import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
39
39
 
40
40
  const STORAGE_TRUSTED = 'trusted';