From 0d48b2275a2a0b3538f2e9352414eb4e917e1447 Mon Sep 17 00:00:00 2001 From: Mark Sujew Date: Thu, 10 Oct 2024 09:53:18 +0200 Subject: [PATCH] Optimize showing recent workspaces (#14260) --- .../src/browser/quick-open-workspace.ts | 48 ++++++------------- .../src/node/default-workspace-server.ts | 7 ++- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/packages/workspace/src/browser/quick-open-workspace.ts b/packages/workspace/src/browser/quick-open-workspace.ts index 473c27400d17a..91890ec2bf89f 100644 --- a/packages/workspace/src/browser/quick-open-workspace.ts +++ b/packages/workspace/src/browser/quick-open-workspace.ts @@ -18,10 +18,7 @@ import { injectable, inject, optional } from '@theia/core/shared/inversify'; import { QuickPickItem, LabelProvider, QuickInputService, QuickInputButton, QuickPickSeparator } from '@theia/core/lib/browser'; import { EnvVariablesServer } from '@theia/core/lib/common/env-variables'; import { WorkspaceService } from './workspace-service'; -import { WorkspacePreferences } from './workspace-preferences'; import URI from '@theia/core/lib/common/uri'; -import { FileService } from '@theia/filesystem/lib/browser/file-service'; -import { FileStat } from '@theia/filesystem/lib/common/files'; import { nls, Path } from '@theia/core/lib/common'; import { UntitledWorkspaceService } from '../common/untitled-workspace-service'; @@ -31,14 +28,11 @@ interface RecentlyOpenedPick extends QuickPickItem { @injectable() export class QuickOpenWorkspace { - protected items: Array; protected opened: boolean; @inject(QuickInputService) @optional() protected readonly quickInputService: QuickInputService; @inject(WorkspaceService) protected readonly workspaceService: WorkspaceService; - @inject(FileService) protected readonly fileService: FileService; @inject(LabelProvider) protected readonly labelProvider: LabelProvider; - @inject(WorkspacePreferences) protected preferences: WorkspacePreferences; @inject(EnvVariablesServer) protected readonly envServer: EnvVariablesServer; @inject(UntitledWorkspaceService) protected untitledWorkspaceService: UntitledWorkspaceService; @@ -48,45 +42,34 @@ export class QuickOpenWorkspace { }; async open(workspaces: string[]): Promise { - this.items = []; - const [homeDirUri] = await Promise.all([ - this.envServer.getHomeDirUri(), - this.workspaceService.getUntitledWorkspace() - ]); - const home = new URI(homeDirUri).path.toString(); - await this.preferences.ready; - this.items.push({ + const homeDirUri = await this.envServer.getHomeDirUri(); + const home = new URI(homeDirUri).path.fsPath(); + const items: (RecentlyOpenedPick | QuickPickSeparator)[] = [{ type: 'separator', label: nls.localizeByDefault('folders & workspaces') - }); + }]; + for (const workspace of workspaces) { const uri = new URI(workspace); - let stat: FileStat | undefined; - try { - stat = await this.fileService.resolve(uri); - } catch { } - if (this.untitledWorkspaceService.isUntitledWorkspace(uri) || !stat) { - continue; // skip the temporary workspace files or an undefined stat. + const label = uri.path.base; + if (!label || this.untitledWorkspaceService.isUntitledWorkspace(uri)) { + continue; // skip temporary workspace files & empty workspace names } - const icon = this.labelProvider.getIcon(stat); - const iconClasses = icon === '' ? undefined : [icon + ' file-icon']; - - this.items.push({ - label: uri.path.base, - description: Path.tildify(uri.path.toString(), home), - iconClasses, + items.push({ + label: label, + description: Path.tildify(uri.path.fsPath(), home), buttons: [this.removeRecentWorkspaceButton], resource: uri, execute: () => { const current = this.workspaceService.workspace; - const uriToOpen = new URI(workspace); if ((current && current.resource.toString() !== workspace) || !current) { - this.workspaceService.open(uriToOpen); + this.workspaceService.open(uri); } - }, + } }); } - this.quickInputService?.showQuickPick(this.items, { + + this.quickInputService?.showQuickPick(items, { placeholder: nls.localize( 'theia/workspace/openRecentPlaceholder', 'Type the name of the workspace you want to open'), @@ -101,7 +84,6 @@ export class QuickOpenWorkspace { } select(): void { - this.items = []; this.opened = this.workspaceService.opened; this.workspaceService.recentWorkspaces().then(workspaceRoots => { if (workspaceRoots) { diff --git a/packages/workspace/src/node/default-workspace-server.ts b/packages/workspace/src/node/default-workspace-server.ts index 10a7490fc41a5..251d8dc6f54de 100644 --- a/packages/workspace/src/node/default-workspace-server.ts +++ b/packages/workspace/src/node/default-workspace-server.ts @@ -161,7 +161,12 @@ export class DefaultWorkspaceServer implements WorkspaceServer, BackendApplicati } protected async workspaceStillExist(workspaceRootUri: string): Promise { - return fs.pathExists(FileUri.fsPath(workspaceRootUri)); + const uri = new URI(workspaceRootUri); + // Non file system workspaces cannot be checked for existence + if (uri.scheme !== 'file') { + return false; + } + return fs.pathExists(uri.path.fsPath()); } protected async getWorkspaceURIFromCli(): Promise {