Skip to content

Commit

Permalink
textFileService.copy
Browse files Browse the repository at this point in the history
  • Loading branch information
isidorn committed Nov 28, 2019
1 parent 791f193 commit 137bda6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/vs/workbench/services/textfile/browser/textFileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,17 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
}

async move(source: URI, target: URI, overwrite?: boolean): Promise<IFileStatWithMetadata> {
return this.moveOrCopy(source, target, true, overwrite);
}

async copy(source: URI, target: URI, overwrite?: boolean): Promise<IFileStatWithMetadata> {
return this.moveOrCopy(source, target, false, overwrite);
}

private async moveOrCopy(source: URI, target: URI, move: boolean, overwrite?: boolean): Promise<IFileStatWithMetadata> {

// before event
await this._onWillRunOperation.fireAsync({ operation: FileOperation.MOVE, target, source }, CancellationToken.None);
await this._onWillRunOperation.fireAsync({ operation: move ? FileOperation.MOVE : FileOperation.COPY, target, source }, CancellationToken.None);

// find all models that related to either source or target (can be many if resource is a folder)
const sourceModels: ITextFileEditorModel[] = [];
Expand Down Expand Up @@ -433,15 +441,19 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
modelsToRestore.push(modelToRestore);
}

// in order to move, we need to soft revert all dirty models,
// in order to move and copy, we need to soft revert all dirty models,
// both from the source as well as the target if any
const dirtyModels = [...sourceModels, ...conflictingModels].filter(model => model.isDirty());
await this.revertAll(dirtyModels.map(dirtyModel => dirtyModel.resource), { soft: true });

// now we can rename the source to target via file operation
let stat: IFileStatWithMetadata;
try {
stat = await this.fileService.move(source, target, overwrite);
if (move) {
stat = await this.fileService.move(source, target, overwrite);
} else {
stat = await this.fileService.copy(source, target, overwrite);
}
} catch (error) {

// in case of any error, ensure to set dirty flag back
Expand Down Expand Up @@ -469,7 +481,7 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
}));

// after event
this._onDidRunOperation.fire(new FileOperationDidRunEvent(FileOperation.MOVE, target, source));
this._onDidRunOperation.fire(new FileOperationDidRunEvent(move ? FileOperation.MOVE : FileOperation.COPY, target, source));

return stat;
}
Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/services/textfile/common/textfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export interface ITextFileService extends IDisposable {
* Move a file. If the file is dirty, its contents will be preserved and restored.
*/
move(source: URI, target: URI, overwrite?: boolean): Promise<IFileStatWithMetadata>;

/**
* Copy a file. If the file is dirty, its contents will be preserved and restored.
*/
copy(source: URI, target: URI, overwrite?: boolean): Promise<IFileStatWithMetadata>;
}

export interface FileOperationWillRunEvent extends IWaitUntil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ suite('MainThreadEditors', () => {
let editors: MainThreadTextEditors;

const movedResources = new Map<URI, URI>();
const copiedResources = new Map<URI, URI>();
const createdResources = new Set<URI>();
const deletedResources = new Set<URI>();

Expand All @@ -46,6 +47,7 @@ suite('MainThreadEditors', () => {
const codeEditorService = new TestCodeEditorService();

movedResources.clear();
copiedResources.clear();
createdResources.clear();
deletedResources.clear();

Expand All @@ -65,6 +67,10 @@ suite('MainThreadEditors', () => {
movedResources.set(source, target);
return Promise.resolve(Object.create(null));
}
copy(source: URI, target: URI) {
copiedResources.set(source, target);
return Promise.resolve(Object.create(null));
}
models = <any>{
onModelSaved: Event.None,
onModelReverted: Event.None,
Expand Down

0 comments on commit 137bda6

Please sign in to comment.