Skip to content

Commit

Permalink
fix(NewFileController): show workspace selector when relative to root
Browse files Browse the repository at this point in the history
  • Loading branch information
sleistner committed Jan 30, 2023
1 parent 1b784a3 commit e3fcf96
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/controller/BaseFileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export abstract class BaseFileController implements FileController {
return postAPICallClipboardData;
}

protected async getWorkspaceFolderPath(relativeToRoot?: boolean): Promise<string | undefined>;
protected async getWorkspaceFolderPath(): Promise<string | undefined> {
const workspaceFolder = await this.selectWorkspaceFolder();
return workspaceFolder?.uri.fsPath;
Expand All @@ -209,6 +210,10 @@ export abstract class BaseFileController implements FileController {
return workspace.getWorkspaceFolder(uri) || window.showWorkspaceFolderPick();
}

protected get isMultiRootWorkspace(): boolean {
return workspace.workspaceFolders !== undefined && workspace.workspaceFolders.length > 1;
}

protected async getFileSourcePathAtRoot(rootPath: string, options: SourcePathOptions): Promise<string> {
const { relativeToRoot = false, typeahead } = options;
let sourcePath = rootPath;
Expand Down
29 changes: 21 additions & 8 deletions src/controller/NewFileController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import expand from "brace-expansion";
import * as path from "path";
import { window } from "vscode";
import { FileItem } from "../FileItem";
import { BaseFileController, TargetPathInputBoxValueOptions } from "./BaseFileController";
import { DialogOptions, ExecuteOptions, SourcePathOptions } from "./FileController";
Expand All @@ -18,21 +19,23 @@ export class NewFileController extends BaseFileController {
const sourcePath = await this.getNewFileSourcePath({ relativeToRoot, typeahead });
const targetPath = await this.getTargetPath(sourcePath, options);

if (targetPath) {
return expand(targetPath.replace(/\\/g, "/")).map((filePath) => {
const realPath = path.resolve(sourcePath, filePath);
const isDir = filePath.endsWith("/");
return new FileItem(sourcePath, realPath, isDir);
});
if (!targetPath) {
return;
}

return expand(targetPath.replace(/\\/g, "/")).map((filePath) => {
const realPath = path.resolve(sourcePath, filePath);
const isDir = filePath.endsWith("/");
return new FileItem(sourcePath, realPath, isDir);
});
}

public async execute(options: NewFileExecuteOptions): Promise<FileItem> {
const { fileItem, isDir = false } = options;
await this.ensureWritableFile(fileItem);
try {
return fileItem.create(isDir);
} catch (e) {
} catch {
throw new Error(`Error creating file '${fileItem.path}'.`);
}
}
Expand All @@ -57,8 +60,18 @@ export class NewFileController extends BaseFileController {

private async getRootPath(relativeToRoot: boolean): Promise<string | undefined> {
if (relativeToRoot) {
return this.getWorkspaceFolderPath();
return this.getWorkspaceFolderPath(relativeToRoot);
}
return path.dirname(await this.getSourcePath());
}

protected async getWorkspaceFolderPath(relativeToRoot: boolean): Promise<string | undefined> {
const requiresWorkspaceFolderPick = relativeToRoot && this.isMultiRootWorkspace;
if (requiresWorkspaceFolderPick) {
const workspaceFolder = await window.showWorkspaceFolderPick();
return workspaceFolder?.uri.fsPath;
}

return super.getWorkspaceFolderPath();
}
}
9 changes: 6 additions & 3 deletions test/command/NewFileCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe(NewFileCommand.name, () => {

it("should show workspace selector", async () => {
await subject.execute();
expect(window.showWorkspaceFolderPick).to.have.been.calledWith();
expect(window.showWorkspaceFolderPick).to.have.been.called;

const prompt = "File Name";
const value = path.join(helper.workspaceFolderB.uri.fsPath, path.sep);
Expand All @@ -157,16 +157,19 @@ describe(NewFileCommand.name, () => {
beforeEach(async () => {
helper.createGetWorkspaceFolderStub().returns(helper.workspaceFolderB);
await helper.openDocument(helper.editorFile1);
await subject.execute();
});

afterEach(async () => {
await helper.closeAllEditors();
});

it("should show workspace selector", async () => {
expect(window.showWorkspaceFolderPick).to.have.been.called;
});

it("should select workspace for open file", async () => {
await subject.execute();
expect(workspace.getWorkspaceFolder).to.have.been.calledWith(Uri.file(helper.editorFile1.fsPath));
expect(window.showWorkspaceFolderPick).to.have.not.been.called;
});
});

Expand Down

0 comments on commit e3fcf96

Please sign in to comment.