Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize showing recent workspaces #14260

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 15 additions & 33 deletions packages/workspace/src/browser/quick-open-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -31,14 +28,11 @@ interface RecentlyOpenedPick extends QuickPickItem {

@injectable()
export class QuickOpenWorkspace {
protected items: Array<RecentlyOpenedPick | QuickPickSeparator>;
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;

Expand All @@ -48,45 +42,34 @@ export class QuickOpenWorkspace {
};

async open(workspaces: string[]): Promise<void> {
this.items = [];
const [homeDirUri] = await Promise.all([
this.envServer.getHomeDirUri(),
this.workspaceService.getUntitledWorkspace()
]);
const home = new URI(homeDirUri).path.toString();
await this.preferences.ready;
sdirix marked this conversation as resolved.
Show resolved Hide resolved
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'),
Expand All @@ -101,7 +84,6 @@ export class QuickOpenWorkspace {
}

select(): void {
this.items = [];
this.opened = this.workspaceService.opened;
this.workspaceService.recentWorkspaces().then(workspaceRoots => {
if (workspaceRoots) {
Expand Down
7 changes: 6 additions & 1 deletion packages/workspace/src/node/default-workspace-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ export class DefaultWorkspaceServer implements WorkspaceServer, BackendApplicati
}

protected async workspaceStillExist(workspaceRootUri: string): Promise<boolean> {
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<string | undefined> {
Expand Down
Loading