Skip to content

Commit

Permalink
feat(DuplicateFile): add typeahead support
Browse files Browse the repository at this point in the history
  • Loading branch information
sleistner committed Jan 23, 2023
1 parent 0e3e0ca commit 44ac603
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@
"markdownDeprecationMessage": "**Deprecated**: Please use `#fileutils.newFile.typeahead.enabled#` or `#fileutils.newFolder.typeahead.enabled#` instead.",
"deprecationMessage": "Deprecated: Please use fileutils.newFile.typeahead.enabled or fileutils.newFolder.typeahead.enabled instead."
},
"fileutils.duplicateFile.typeahead.enabled": {
"type": "boolean",
"default": false,
"description": "Controls wheather to show a directory selector for duplicate file command."
},
"fileutils.moveFile.typeahead.enabled": {
"type": "boolean",
"default": false,
Expand Down
4 changes: 3 additions & 1 deletion src/command/DuplicateFileCommand.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Uri } from "vscode";
import { MoveFileController } from "../controller/MoveFileController";
import { getConfiguration } from "../lib/config";
import { BaseCommand } from "./BaseCommand";

export class DuplicateFileCommand extends BaseCommand<MoveFileController> {
public async execute(uri?: Uri): Promise<void> {
const dialogOptions = { prompt: "Duplicate As", showFullPath: true, uri };
const typeahead = getConfiguration("duplicateFile.typeahead.enabled") === true;
const dialogOptions = { prompt: "Duplicate As", showFullPath: true, uri, typeahead };
const fileItem = await this.controller.showDialog(dialogOptions);
await this.executeController(fileItem, { openFileInEditor: !fileItem?.isDir });
}
Expand Down
52 changes: 48 additions & 4 deletions test/command/DuplicateFileCommand.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from "chai";
import * as path from "path";
import * as fs from "fs";
import * as path from "path";
import sinon from "sinon";
import { Uri, window, workspace } from "vscode";
import { DuplicateFileCommand } from "../../src/command/DuplicateFileCommand";
import { DuplicateFileController } from "../../src/controller";
Expand All @@ -10,7 +11,10 @@ import { tmpDir } from "../helper";
describe(DuplicateFileCommand.name, () => {
const subject = new DuplicateFileCommand(new DuplicateFileController(helper.createExtensionContext()));

beforeEach(helper.beforeEach);
beforeEach(async () => {
await helper.beforeEach();
helper.createGetConfigurationStub({ "duplicateFile.typeahead.enabled": false });
});

afterEach(helper.afterEach);

Expand All @@ -19,18 +23,59 @@ describe(DuplicateFileCommand.name, () => {
beforeEach(async () => {
await helper.openDocument(helper.editorFile1);
helper.createShowInputBoxStub().resolves(helper.targetFile.path);
helper.createShowQuickPickStub().resolves({ label: "/", description: "" });
});

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

helper.protocol.it("should prompt for file destination", subject, "Duplicate As");
helper.protocol.it("should duplicate current file to destination", subject);
helper.protocol.describe("with target file in non-existent nested directory", subject);
helper.protocol.describe("when target destination exists", subject);
helper.protocol.it("should open target file as active editor", subject);

describe("configuration", () => {
describe('when "newFile.typeahead.enabled" is "true"', () => {
beforeEach(async () => {
await workspace.fs.createDirectory(Uri.file(path.resolve(helper.tmpDir.fsPath, "dir-1")));
await workspace.fs.createDirectory(Uri.file(path.resolve(helper.tmpDir.fsPath, "dir-2")));

helper.createGetConfigurationStub({ "duplicateFile.typeahead.enabled": true });
helper
.createStubObject(workspace, "workspaceFolders")
.get(() => [{ uri: Uri.file(helper.tmpDir.fsPath), name: "a", index: 0 }]);
});

it("should show the quick pick dialog", async () => {
await subject.execute();
expect(window.showQuickPick).to.have.been.calledOnceWith(
sinon.match([
{ description: "- workspace root", label: "/" },
{ description: undefined, label: "/dir-1" },
{ description: undefined, label: "/dir-2" },
]),
sinon.match({
placeHolder: helper.quickPick.typeahead.placeHolder,
})
);
});
});

describe('when "newFile.typeahead.enabled" is "false"', () => {
beforeEach(async () => {
helper.createGetConfigurationStub({ "duplicateFile.typeahead.enabled": false });
});

it("should not show the quick pick dialog", async () => {
await subject.execute();
expect(window.showQuickPick).to.have.not.been.called;
});
});
});
});

helper.protocol.describe("without an open text document", subject);
Expand Down Expand Up @@ -65,8 +110,7 @@ describe(DuplicateFileCommand.name, () => {
it("should prompt for file destination", async () => {
await subject.execute(sourceDirectory);
const value = sourceDirectory.path;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const valueSelection = [value.length - value.split(path.sep).pop()!.length, value.length];
const valueSelection = [value.length - (value.split(path.sep).pop() as string).length, value.length];
const prompt = "Duplicate As";
expect(window.showInputBox).to.have.been.calledWithExactly({ prompt, value, valueSelection });
});
Expand Down

0 comments on commit 44ac603

Please sign in to comment.