Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
- remove checks for files against workspace URI
- update variable name to a more representative one
- Simplify algorithm for single selected URI case
- update signatures to indicate an array of files or folders can be opened rather than a single element
  • Loading branch information
rschnekenbu committed Sep 26, 2023
1 parent 7e8f500 commit a116c9e
Showing 1 changed file with 36 additions and 45 deletions.
81 changes: 36 additions & 45 deletions packages/workspace/src/browser/workspace-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,85 +262,76 @@ export class WorkspaceFrontendContribution implements CommandContribution, Keybi
* This is the generic `Open` method. Opens files and directories too. Resolves to the opened URI.
* Except when you are on either Windows or Linux `AND` running in electron. If so, it opens a file.
*/
protected async doOpen(): Promise<MaybeArray<URI> | undefined> {
protected async doOpen(): Promise<URI[] | undefined> {
if (!isOSX && this.isElectron()) {
return this.doOpenFile();
}
const [rootStat] = await this.workspaceService.roots;
const destinationUri = await this.fileDialogService.showOpenDialog({
let selectedURIs = await this.fileDialogService.showOpenDialog({
title: WorkspaceCommands.OPEN.dialogLabel,
canSelectFolders: true,
canSelectFiles: true,
canSelectMany: true
}, rootStat);
if (destinationUri) {
if (Array.isArray(destinationUri)) {
// only open files then open all folders in a new workspace, as done with Electron see doOpenFolder
const folders: MaybeArray<URI> = [];
for (const uri of destinationUri) {
if (selectedURIs) {
if (!Array.isArray(selectedURIs)) {
selectedURIs = [selectedURIs];
}
const folders: URI[] = [];
// Only open files then open all folders in a new workspace, as done with Electron see doOpenFolder.
for (const uri of selectedURIs) {
const destination = await this.fileService.resolve(uri);
if (destination.isDirectory) {
if (this.getCurrentWorkspaceUri()?.toString() !== uri.toString()) {
const destination = await this.fileService.resolve(uri);
if (destination.isDirectory) {
folders.push(uri);
} else {
await open(this.openerService, uri);
}
}
}
if (folders.length > 0) {
const openableURI = await this.getOpenableWorkspaceUri(folders);
if (openableURI && (!this.workspaceService.workspace || !openableURI.isEqual(this.workspaceService.workspace.resource))) {
this.workspaceService.open(openableURI);
folders.push(uri);
}
} else {
await open(this.openerService, uri);
}

return destinationUri;
} else {
return this.doOpenFileOrFolder(destinationUri, true);
}
}
return undefined;
}

protected async doOpenFileOrFolder(uri: URI, openFolders: boolean): Promise<URI> {
if (this.getCurrentWorkspaceUri()?.toString() !== uri.toString()) {
const destination = await this.fileService.resolve(uri);
if (destination.isDirectory) {
if (openFolders) {
this.workspaceService.open(uri);
if (folders.length > 0) {
const openableURI = await this.getOpenableWorkspaceUri(folders);
if (openableURI && (!this.workspaceService.workspace || !openableURI.isEqual(this.workspaceService.workspace.resource))) {
this.workspaceService.open(openableURI);
}
} else {
await open(this.openerService, uri);
}

return selectedURIs;
}
return uri;
return undefined;
}

/**
* Opens a file after prompting the `Open File` dialog. Resolves to `undefined`, if
* Opens a set of files after prompting the `Open File` dialog. Resolves to `undefined`, if
* - the workspace root is not set,
* - the file to open does not exist, or
* - it was not a file, but a directory.
*
* Otherwise, resolves to the URI of the file.
* Otherwise, resolves to the set of URIs of the files.
*/
protected async doOpenFile(): Promise<MaybeArray<URI> | undefined> {
protected async doOpenFile(): Promise<URI[] | undefined> {
const props: OpenFileDialogProps = {
title: WorkspaceCommands.OPEN_FILE.dialogLabel,
canSelectFolders: false,
canSelectFiles: true,
canSelectMany: true
};
const [rootStat] = await this.workspaceService.roots;
const destinationFileUri: MaybeArray<URI> | undefined = await this.fileDialogService.showOpenDialog(props, rootStat);
if (Array.isArray(destinationFileUri)) {
let selectedFilesURIs: MaybeArray<URI> | undefined = await this.fileDialogService.showOpenDialog(props, rootStat);
if (selectedFilesURIs) {
if (!Array.isArray(selectedFilesURIs)) {
selectedFilesURIs = [selectedFilesURIs];
}

const result = [];
for (const uri of destinationFileUri) {
result.push(await this.doOpenFileOrFolder(uri, false));
for (const uri of selectedFilesURIs) {
const destination = await this.fileService.resolve(uri);
if (destination.isFile) {
await open(this.openerService, uri);
result.push(uri);
}
}
return result;
} else if (destinationFileUri) {
return this.doOpenFileOrFolder(destinationFileUri, false);
}
return undefined;
}
Expand Down

0 comments on commit a116c9e

Please sign in to comment.