From e283c32b9db5faedfee01e30731edc6f21054bdf Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Thu, 5 Jul 2018 15:55:28 +0200 Subject: [PATCH] fix #52447 --- .../src/singlefolder-tests/workspace.test.ts | 3 +-- .../bulkEdit/electron-browser/bulkEditService.ts | 2 +- .../services/textfile/common/textFileService.ts | 14 ++++++++++++++ .../services/textfile/common/textfiles.ts | 6 ++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/extensions/vscode-api-tests/src/singlefolder-tests/workspace.test.ts b/extensions/vscode-api-tests/src/singlefolder-tests/workspace.test.ts index 9d50cafbb17..ccdfbd8829d 100644 --- a/extensions/vscode-api-tests/src/singlefolder-tests/workspace.test.ts +++ b/extensions/vscode-api-tests/src/singlefolder-tests/workspace.test.ts @@ -689,8 +689,7 @@ suite('workspace-namespace', () => { we = new vscode.WorkspaceEdit(); we.createFile(docUri, { overwrite: true }); assert.ok(await vscode.workspace.applyEdit(we)); - // todo@ben - // assert.equal((await vscode.workspace.openTextDocument(docUri)).getText(), ''); + assert.equal((await vscode.workspace.openTextDocument(docUri)).getText(), ''); }); test('WorkspaceEdit: create & ignoreIfExists', async function () { diff --git a/src/vs/workbench/services/bulkEdit/electron-browser/bulkEditService.ts b/src/vs/workbench/services/bulkEdit/electron-browser/bulkEditService.ts index 7b1ca552400..a23fcba984b 100644 --- a/src/vs/workbench/services/bulkEdit/electron-browser/bulkEditService.ts +++ b/src/vs/workbench/services/bulkEdit/electron-browser/bulkEditService.ts @@ -343,7 +343,7 @@ export class BulkEdit { } else if (edit.newUri && !edit.oldUri) { let ignoreIfExists = edit.options && edit.options.ignoreIfExists; if (!ignoreIfExists || !await this._fileService.existsFile(edit.newUri)) { - await this._fileService.createFile(edit.newUri, undefined, { overwrite }); + await this._textFileService.create(edit.newUri, undefined, { overwrite }); } } } diff --git a/src/vs/workbench/services/textfile/common/textFileService.ts b/src/vs/workbench/services/textfile/common/textFileService.ts index 6a6b4648038..5e1e00594c6 100644 --- a/src/vs/workbench/services/textfile/common/textFileService.ts +++ b/src/vs/workbench/services/textfile/common/textFileService.ts @@ -692,6 +692,20 @@ export abstract class TextFileService extends Disposable implements ITextFileSer }); } + create(resource: URI, contents?: string, options?: { overwrite?: boolean }): TPromise { + return this.fileService.createFile(resource, contents, options).then(() => { + + // If we had an existing model for the given resource, load + // it again to make sure it is up to date with the contents + // we just wrote into the underlying resource. + if (!!this.models.get(resource)) { + return this.models.loadOrCreate(resource, { reload: { async: false } }).then(() => void 0); + } + + return void 0; + }); + } + delete(resource: URI, options?: { useTrash?: boolean, recursive?: boolean }): TPromise { const dirtyFiles = this.getDirty().filter(dirty => isEqualOrParent(dirty, resource, !platform.isLinux /* ignorecase */)); diff --git a/src/vs/workbench/services/textfile/common/textfiles.ts b/src/vs/workbench/services/textfile/common/textfiles.ts index 716b6c1eb36..737fbaed9c4 100644 --- a/src/vs/workbench/services/textfile/common/textfiles.ts +++ b/src/vs/workbench/services/textfile/common/textfiles.ts @@ -328,6 +328,12 @@ export interface ITextFileService extends IDisposable { */ revertAll(resources?: URI[], options?: IRevertOptions): TPromise; + /** + * Create a file. If the file exists it will be overwritten with the contents if + * the options enable to overwrite. + */ + create(resource: URI, contents?: string, options?: { overwrite?: boolean }): TPromise; + /** * Delete a file. If the file is dirty, it will get reverted and then deleted from disk. */