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

Copy and paste file/folder in the same place will duplicate the file/folder #8940

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 8 additions & 1 deletion packages/filesystem/src/browser/file-tree/file-tree-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { FileService } from '../file-service';
import { FileOperationError, FileOperationResult, FileChangesEvent, FileChangeType, FileChange, FileOperation, FileOperationEvent } from '../../common/files';
import { MessageService } from '@theia/core/lib/common/message-service';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { FileSystemUtils } from '../../common/filesystem-utils';

@injectable()
export class FileTreeModel extends TreeModelImpl implements LocationService {
Expand Down Expand Up @@ -148,7 +149,13 @@ export class FileTreeModel extends TreeModelImpl implements LocationService {
}

async copy(source: URI, target: Readonly<FileStatNode>): Promise<URI> {
const targetUri = target.uri.resolve(source.path.base);
let targetUri = target.uri.resolve(source.path.base);
// duplicate file or folder
if (source.path.toString() === target.uri.path.toString()) {
const parent = await this.fileService.resolve(source.parent);
const name = source.path.name + '_copy';
targetUri = FileSystemUtils.generateUniqueResourceURI(source.parent, parent, name, source.path.ext);
}
try {
await this.fileService.copy(source, targetUri);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should move the logic to the actual implementation of fileService.copy, so all clients (callers) who are requesting a similar behavior will not need to duplicate the logic. For example, the doMoveCopy is handling the _copy like you've done:

// if target exists get valid target
if (exists && !overwrite) {
const parent = await this.resolve(target.parent);
const name = target.path.name + '_copy';
target = FileSystemUtils.generateUniqueResourceURI(target.parent, parent, name, target.path.ext);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should move the logic to the actual implementation of fileService.copy

I do not think so. If you debug into the VS Code's FileService, that we copied, you will see the target URI is the expected target URI (file:///path/to/file copy or file:///path/to/file copy 2). Code does not do any more magic just copies/moves the file. We should do the same since we copied the code and we do not want to diverge from it.

By the way, I proposed something similar here

} catch (e) {
Expand Down