Skip to content

Commit

Permalink
fix(Rename, Move): keep file in editor group
Browse files Browse the repository at this point in the history
  • Loading branch information
sleistner committed Oct 23, 2020
1 parent f0fd00d commit 5478345
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 217 deletions.
22 changes: 1 addition & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Build Status](https://travis-ci.org/sleistner/vscode-fileutils.svg?branch=master)](https://travis-ci.org/sleistner/vscode-fileutils)
[![Dependency Status](https://david-dm.org/sleistner/vscode-fileutils.svg)](https://david-dm.org/sleistner/vscode-fileutils)
[![Known Vulnerabilities](https://snyk.io/test/github/sleistner/vscode-fileutils/badge.svg)](https://snyk.io/test/github/sleistner/vscode-fileutils)
[![Known Vulnerabilities](https://snyk.io/test/github/sleistner/vscode-fileutils/badge.svg)](https://snyk.io/test/github/sleistner/vscode-fileutils)
[![Renovate](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

Expand Down Expand Up @@ -111,26 +111,6 @@ Nonexistent folders are created automatically.

```json
{
"fileutils.delete.useTrash": {
"type": "boolean",
"default": false,
"description": "Move file to the recycle bin instead of deleting it permanently."
},
"fileutils.delete.confirm": {
"type": "boolean",
"default": true,
"description": "Controls if it should ask for confirmation when deleting a file."
},
"fileutils.rename.closeOldTab": {
"type": "boolean",
"default": true,
"description": "Controls whether to close the tab of the renamed file (Will work only if 'Close On File Delete' setting is disabled)"
},
"fileutils.move.closeOldTab": {
"type": "boolean",
"default": true,
"description": "Controls whether to close the tab of the moved file (Will work only if 'Close On File Delete' setting is disabled)"
},
"fileutils.typeahead.enabled": {
"type": "boolean",
"default": true,
Expand Down
38 changes: 9 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,47 +48,47 @@
"commands": [
{
"command": "fileutils.renameFile",
"category": "File",
"category": "File Utils",
"title": "Rename"
},
{
"command": "fileutils.moveFile",
"category": "File",
"category": "File Utils",
"title": "Move"
},
{
"command": "fileutils.duplicateFile",
"category": "File",
"category": "File Utils",
"title": "Duplicate"
},
{
"command": "fileutils.removeFile",
"category": "File",
"category": "File Utils",
"title": "Delete"
},
{
"command": "fileutils.newFile",
"category": "File",
"category": "File Utils",
"title": "New File Relative to Current View"
},
{
"command": "fileutils.newFileAtRoot",
"category": "File",
"category": "File Utils",
"title": "New File Relative to Project Root"
},
{
"command": "fileutils.newFolder",
"category": "File",
"category": "File Utils",
"title": "New Folder Relative to Current View"
},
{
"command": "fileutils.newFolderAtRoot",
"category": "File",
"category": "File Utils",
"title": "New Folder Relative to Project Root"
},
{
"command": "fileutils.copyFileName",
"category": "File",
"category": "File Utils",
"title": "Copy Name"
}
],
Expand Down Expand Up @@ -140,26 +140,6 @@
"type": "object",
"title": "Fileutils configuration",
"properties": {
"fileutils.delete.useTrash": {
"type": "boolean",
"default": false,
"description": "Move file to the recycle bin instead of deleting it permanently."
},
"fileutils.delete.confirm": {
"type": "boolean",
"default": true,
"description": "Controls if it should ask for confirmation when deleting a file."
},
"fileutils.rename.closeOldTab": {
"type": "boolean",
"default": true,
"description": "Controls whether to close the tab of the renamed file (Will work only if 'Close On File Delete' setting is disabled)"
},
"fileutils.move.closeOldTab": {
"type": "boolean",
"default": true,
"description": "Controls whether to close the tab of the moved file (Will work only if 'Close On File Delete' setting is disabled)"
},
"fileutils.typeahead.enabled": {
"type": "boolean",
"default": true,
Expand Down
15 changes: 6 additions & 9 deletions src/FileItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,17 @@ export class FileItem {

public async duplicate(): Promise<FileItem> {
this.ensureTargetPath();

const edit = new WorkspaceEdit();
await workspace.fs.copy(this.path, this.targetPath!, { overwrite: true });

return new FileItem(this.targetPath!);
}

public async remove(useTrash = false): Promise<FileItem> {
try {
await workspace.fs.delete(this.path, { recursive: true, useTrash });
} catch (err) {
if (useTrash === true && err instanceof FileSystemError) {
return this.remove(false);
}
throw err;
}
public async remove(): Promise<FileItem> {
const edit = new WorkspaceEdit();
edit.deleteFile(this.path, { recursive: true, ignoreIfNotExists: true });
await workspace.applyEdit(edit);
return this;
}

Expand Down
10 changes: 1 addition & 9 deletions src/command/MoveFileCommand.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { ExtensionContext, Uri } from 'vscode';
import { IFileController, MoveFileController } from '../controller';
import { Uri } from 'vscode';
import { IMoveFileDialogOptions } from '../controller/MoveFileController';
import { getConfiguration } from '../lib/config';
import { BaseCommand } from './BaseCommand';

export class MoveFileCommand extends BaseCommand {
Expand All @@ -11,12 +9,6 @@ export class MoveFileCommand extends BaseCommand {
const fileItem = await this.controller.showDialog(dialogOptions);
if (fileItem) {
await this.controller.execute({ fileItem });

if (getConfiguration('move.closeOldTab')) {
await this.controller.closeCurrentFileEditor();
}

return this.controller.openFileInEditor(fileItem);
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/command/RenameFileCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Uri } from 'vscode';
import { getConfiguration } from '../lib/config';
import { BaseCommand } from './BaseCommand';

export class RenameFileCommand extends BaseCommand {
Expand All @@ -8,13 +7,7 @@ export class RenameFileCommand extends BaseCommand {
const fileItem = await this.controller.showDialog({ prompt: 'New Name' });

if (fileItem) {
const movedFileItem = await this.controller.execute({ fileItem });

if (getConfiguration('rename.closeOldTab')) {
await this.controller.closeCurrentFileEditor();
}

return this.controller.openFileInEditor(movedFileItem);
await this.controller.execute({ fileItem });
}
}

Expand Down
13 changes: 4 additions & 9 deletions src/controller/RemoveFileController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as path from 'path';
import { window } from 'vscode';
import { window, workspace } from 'vscode';
import { FileItem } from '../FileItem';
import { getConfiguration } from '../lib/config';
import { BaseFileController } from './BaseFileController';
import { IExecuteOptions } from './FileController';

Expand All @@ -19,7 +18,7 @@ export class RemoveFileController extends BaseFileController {
}

const message = `Are you sure you want to delete '${path.basename(sourcePath)}'?`;
const action = this.useTrash ? 'Move to Trash' : 'Delete';
const action = 'Move to Trash';
const remove = await window.showInformationMessage(message, { modal: true }, action);
if (remove) {
return new FileItem(sourcePath);
Expand All @@ -29,18 +28,14 @@ export class RemoveFileController extends BaseFileController {
public async execute(options: IExecuteOptions): Promise<FileItem> {
const { fileItem } = options;
try {
await fileItem.remove(this.useTrash);
await fileItem.remove();
} catch (e) {
throw new Error(`Error deleting file '${fileItem.path}'.`);
}
return fileItem;
}

private get useTrash(): boolean {
return getConfiguration('delete.useTrash');
}

private get confirmDelete(): boolean {
return getConfiguration('delete.confirm');
return workspace.getConfiguration('explorer', null).get('confirmDelete') === true;
}
}
2 changes: 1 addition & 1 deletion test/command/CopyFileNameCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('CopyFileNameCommand', () => {
await env.clipboard.writeText(clipboardInitialTestData);
});

it('ignores the command call and does not change the clipboard data', async () => {
it('should ignore the command call and does not change the clipboard data', async () => {
try {
await subject.execute();
expect.fail('must fail');
Expand Down
12 changes: 6 additions & 6 deletions test/command/DuplicateFileCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ describe('DuplicateFileCommand', () => {
helper.restoreShowInputBox();
});

helper.protocol.it('prompts for file destination', subject, 'Duplicate As');
helper.protocol.it('duplicates current file to destination', subject);
helper.protocol.it('should prompt for file destination', subject, 'Duplicate As');
helper.protocol.it('should duplicate current file to destination', subject);
helper.protocol.describe('target file in non existing nested directories', subject);
helper.protocol.describe('when target destination exists', subject);
helper.protocol.it('opens target file as active editor', subject);
helper.protocol.it('should open target file as active editor', subject);
});

helper.protocol.describe('without open text document', subject);
Expand All @@ -37,8 +37,8 @@ describe('DuplicateFileCommand', () => {

afterEach(async () => helper.restoreShowInputBox());

helper.protocol.it('prompts for file destination', subject, 'Duplicate As');
helper.protocol.it('duplicates current file to destination', subject, helper.editorFile1);
helper.protocol.it('opens target file as active editor', subject, helper.editorFile1);
helper.protocol.it('should prompt for file destination', subject, 'Duplicate As');
helper.protocol.it('should duplicate current file to destination', subject, helper.editorFile1);
helper.protocol.it('should open target file as active editor', subject, helper.editorFile1);
});
});
47 changes: 4 additions & 43 deletions test/command/MoveFileCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,9 @@ describe('MoveFileCommand', () => {
helper.restoreShowInputBox();
});

helper.protocol.it('prompts for file destination', subject, 'New Location');
helper.protocol.it('moves current file to destination', subject);
helper.protocol.it('should prompt for file destination', subject, 'New Location');
helper.protocol.it('should move current file to destination', subject);
helper.protocol.describe('target file in non existing nested directories', subject);
helper.protocol.describe('when target destination exists', subject, {
extra() {
describe('configuration', () => {
beforeEach(async () => {
helper.createExecuteCommandStub().withArgs('workbench.action.closeActiveEditor');
});

afterEach(async () => {
helper.restoreGetConfiguration();
helper.restoreExecuteCommand();
});

describe('move.closeOldTab set to true', () => {
beforeEach(async () => {
helper.createGetConfigurationStub({ 'move.closeOldTab': true });
});

it('moves a file and verifies that the tab of the file was closed', async () => {
await subject.execute();
expect(commands.executeCommand).to.have.been.called;
});
});

describe('move.closeOldTab set to false', () => {
beforeEach(async () => {
helper.createGetConfigurationStub({ 'move.closeOldTab': false });
});

it('moves a file and verifies that the tab of the file was not closed', async () => {
await subject.execute();
expect(commands.executeCommand).to.have.not.been.called;
});
});
});
}
});

helper.protocol.it('opens target file as active editor', subject);
});

helper.protocol.describe('without open text document', subject);
Expand All @@ -76,8 +38,7 @@ describe('MoveFileCommand', () => {

afterEach(async () => helper.restoreShowInputBox());

helper.protocol.it('prompts for file destination', subject, 'New Location');
helper.protocol.it('moves current file to destination', subject, helper.editorFile1);
helper.protocol.it('opens target file as active editor', subject, helper.editorFile1);
helper.protocol.it('should prompt for file destination', subject, 'New Location');
helper.protocol.it('should move current file to destination', subject, helper.editorFile1);
});
});
4 changes: 2 additions & 2 deletions test/command/NewFileCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('NewFileCommand', () => {
helper.restoreShowQuickPick();
});

it('prompts for file destination', async () => {
it('should prompt for file destination', async () => {
await subject.execute();
const prompt = 'File Name';
const value = path.join(path.dirname(helper.editorFile1.path), path.sep);
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('NewFileCommand', () => {

helper.protocol.describe('target file in non existing nested directories', subject);
helper.protocol.describe('when target destination exists', subject, { overwriteFileContent: '' });
helper.protocol.it('opens target file as active editor', subject);
helper.protocol.it('should open target file as active editor', subject);
});

describe('with relativeToRoot set "true"', () => {
Expand Down
Loading

0 comments on commit 5478345

Please sign in to comment.