From 97f48d7bba360d151e5ad5e861f72196d26bdfcb Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Wed, 20 Jun 2018 12:25:09 +0200 Subject: [PATCH] rename/create should be able to define override behaviour #10659 --- src/vs/editor/common/modes.ts | 1 + src/vs/monaco.d.ts | 3 ++ src/vs/vscode.proposed.d.ts | 4 +-- src/vs/workbench/api/node/extHost.protocol.ts | 1 + .../workbench/api/node/extHostApiCommands.ts | 4 +-- .../api/node/extHostLanguageFeatures.ts | 6 ++-- .../api/node/extHostTypeConverters.ts | 9 +++--- src/vs/workbench/api/node/extHostTypes.ts | 32 +++++++++++++------ .../electron-browser/bulkEditService.ts | 4 +-- .../electron-browser/api/extHostTypes.test.ts | 4 +-- .../api/mainThreadEditors.test.ts | 6 ++-- 11 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/vs/editor/common/modes.ts b/src/vs/editor/common/modes.ts index 0f14cfc72265c..cc2d937768b63 100644 --- a/src/vs/editor/common/modes.ts +++ b/src/vs/editor/common/modes.ts @@ -899,6 +899,7 @@ export function isResourceTextEdit(thing: any): thing is ResourceTextEdit { export interface ResourceFileEdit { oldUri: URI; newUri: URI; + options: { override: boolean }; } export interface ResourceTextEdit { diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index 05aad76b9ef9a..a97334410aadd 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -5100,6 +5100,9 @@ declare namespace monaco.languages { export interface ResourceFileEdit { oldUri: Uri; newUri: Uri; + options: { + override: boolean; + }; } export interface ResourceTextEdit { diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index acd0beaabe07b..8b93e1b86f478 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -621,9 +621,9 @@ declare module 'vscode' { //#region joh: https://github.com/Microsoft/vscode/issues/10659 export interface WorkspaceEdit { - createFile(uri: Uri): void; + createFile(uri: Uri, options?: { override?: boolean }): void; deleteFile(uri: Uri): void; - renameFile(oldUri: Uri, newUri: Uri): void; + renameFile(oldUri: Uri, newUri: Uri, options?: { override?: boolean }): void; // replaceText(uri: Uri, range: Range, newText: string): void; // insertText(uri: Uri, position: Position, newText: string): void; diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index b037ad5bc3436..be70e46cee364 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -766,6 +766,7 @@ export interface WorkspaceSymbolsDto extends IdObject { export interface ResourceFileEditDto { oldUri: UriComponents; newUri: UriComponents; + options: { override?: boolean }; } export interface ResourceTextEditDto { diff --git a/src/vs/workbench/api/node/extHostApiCommands.ts b/src/vs/workbench/api/node/extHostApiCommands.ts index 3ff0a6186c004..d3467d37632d9 100644 --- a/src/vs/workbench/api/node/extHostApiCommands.ts +++ b/src/vs/workbench/api/node/extHostApiCommands.ts @@ -10,7 +10,7 @@ import { IDisposable } from 'vs/base/common/lifecycle'; import * as vscode from 'vscode'; import * as typeConverters from 'vs/workbench/api/node/extHostTypeConverters'; import * as types from 'vs/workbench/api/node/extHostTypes'; -import { IRawColorInfo } from 'vs/workbench/api/node/extHost.protocol'; +import { IRawColorInfo, WorkspaceEditDto } from 'vs/workbench/api/node/extHost.protocol'; import { ISingleEditOperation } from 'vs/editor/common/model'; import * as modes from 'vs/editor/common/modes'; import * as search from 'vs/workbench/parts/search/common/search'; @@ -344,7 +344,7 @@ export class ExtHostApiCommands { position: position && typeConverters.Position.from(position), newName }; - return this._commands.executeCommand('_executeDocumentRenameProvider', args).then(value => { + return this._commands.executeCommand('_executeDocumentRenameProvider', args).then(value => { if (!value) { return undefined; } diff --git a/src/vs/workbench/api/node/extHostLanguageFeatures.ts b/src/vs/workbench/api/node/extHostLanguageFeatures.ts index 49120b1c2911c..83f75db9b2557 100644 --- a/src/vs/workbench/api/node/extHostLanguageFeatures.ts +++ b/src/vs/workbench/api/node/extHostLanguageFeatures.ts @@ -495,18 +495,18 @@ class RenameAdapter { return typeConvert.WorkspaceEdit.from(value); }, err => { if (typeof err === 'string') { - return { + return { edits: undefined, rejectReason: err }; } else if (err instanceof Error && typeof err.message === 'string') { - return { + return { edits: undefined, rejectReason: err.message }; } else { // generic error - return TPromise.wrapError(err); + return TPromise.wrapError(err); } }); } diff --git a/src/vs/workbench/api/node/extHostTypeConverters.ts b/src/vs/workbench/api/node/extHostTypeConverters.ts index 9aed1474620fb..2c5a3cc0babbe 100644 --- a/src/vs/workbench/api/node/extHostTypeConverters.ts +++ b/src/vs/workbench/api/node/extHostTypeConverters.ts @@ -271,7 +271,7 @@ export const TextEdit = { export namespace WorkspaceEdit { export function from(value: vscode.WorkspaceEdit, documents?: ExtHostDocumentsAndEditors): WorkspaceEditDto { - const result: modes.WorkspaceEdit = { + const result: WorkspaceEditDto = { edits: [] }; for (const entry of (value as types.WorkspaceEdit).allEntries()) { @@ -279,10 +279,10 @@ export namespace WorkspaceEdit { if (Array.isArray(uriOrEdits)) { // text edits let doc = documents ? documents.getDocument(uri.toString()) : undefined; - result.edits.push({ resource: uri, modelVersionId: doc && doc.version, edits: uriOrEdits.map(TextEdit.from) }); + result.edits.push({ resource: uri, modelVersionId: doc && doc.version, edits: uriOrEdits.map(TextEdit.from) }); } else { // resource edits - result.edits.push({ oldUri: uri, newUri: uriOrEdits }); + result.edits.push({ oldUri: uri, newUri: uriOrEdits, options: entry[2] }); } } return result; @@ -299,7 +299,8 @@ export namespace WorkspaceEdit { } else { result.renameFile( URI.revive((edit).oldUri), - URI.revive((edit).newUri) + URI.revive((edit).newUri), + (edit).options ); } } diff --git a/src/vs/workbench/api/node/extHostTypes.ts b/src/vs/workbench/api/node/extHostTypes.ts index 39658d10c72ae..66681d42b5a7a 100644 --- a/src/vs/workbench/api/node/extHostTypes.ts +++ b/src/vs/workbench/api/node/extHostTypes.ts @@ -495,21 +495,33 @@ export class TextEdit { } +export interface IFileOperation { + _type: 1; + from: URI; + to: URI; + options?: { override?: boolean }; +} + +export interface IFileTextEdit { + _type: 2; + uri: URI; + edit: TextEdit; +} export class WorkspaceEdit implements vscode.WorkspaceEdit { - private _edits = new Array<{ _type: 1, from: URI, to: URI } | { _type: 2, uri: URI, edit: TextEdit }>(); + private _edits = new Array(); - createFile(uri: vscode.Uri): void { - this.renameFile(undefined, uri); + renameFile(from: vscode.Uri, to: vscode.Uri, options?: { override?: boolean }): void { + this._edits.push({ _type: 1, from, to, options }); } - deleteFile(uri: vscode.Uri): void { - this.renameFile(uri, undefined); + createFile(uri: vscode.Uri, options?: { override?: boolean }): void { + this.renameFile(undefined, uri, options); } - renameFile(from: vscode.Uri, to: vscode.Uri): void { - this._edits.push({ _type: 1, from, to }); + deleteFile(uri: vscode.Uri): void { + this.renameFile(uri, undefined); } replace(uri: URI, range: Range, newText: string): void { @@ -581,11 +593,11 @@ export class WorkspaceEdit implements vscode.WorkspaceEdit { return values(textEdits); } - allEntries(): ([URI, TextEdit[]] | [URI, URI])[] { - let res: ([URI, TextEdit[]] | [URI, URI])[] = []; + allEntries(): ([URI, TextEdit[]] | [URI, URI, { override?: boolean }])[] { + let res: ([URI, TextEdit[]] | [URI, URI, { override?: boolean }])[] = []; for (let edit of this._edits) { if (edit._type === 1) { - res.push([edit.from, edit.to]); + res.push([edit.from, edit.to, edit.options]); } else { res.push([edit.uri, [edit.edit]]); } diff --git a/src/vs/workbench/services/bulkEdit/electron-browser/bulkEditService.ts b/src/vs/workbench/services/bulkEdit/electron-browser/bulkEditService.ts index 1d989c07002ce..3b661348e19a8 100644 --- a/src/vs/workbench/services/bulkEdit/electron-browser/bulkEditService.ts +++ b/src/vs/workbench/services/bulkEdit/electron-browser/bulkEditService.ts @@ -336,11 +336,11 @@ export class BulkEdit { progress.report(undefined); if (edit.newUri && edit.oldUri) { - await this._textFileService.move(edit.oldUri, edit.newUri, false); + await this._textFileService.move(edit.oldUri, edit.newUri, edit.options && edit.options.override); } else if (!edit.newUri && edit.oldUri) { await this._textFileService.delete(edit.oldUri, true); } else if (edit.newUri && !edit.oldUri) { - await this._fileService.createFile(edit.newUri, undefined, { overwrite: false }); + await this._fileService.createFile(edit.newUri, undefined, { overwrite: edit.options && edit.options.override }); } } } diff --git a/src/vs/workbench/test/electron-browser/api/extHostTypes.test.ts b/src/vs/workbench/test/electron-browser/api/extHostTypes.test.ts index 11e5187b6e6f1..add4566a3f62e 100644 --- a/src/vs/workbench/test/electron-browser/api/extHostTypes.test.ts +++ b/src/vs/workbench/test/electron-browser/api/extHostTypes.test.ts @@ -386,12 +386,12 @@ suite('ExtHostTypes', function () { const all = edit.allEntries(); assert.equal(all.length, 4); - function isFileChange(thing: [URI, types.TextEdit[]] | [URI, URI]): thing is [URI, URI] { + function isFileChange(thing: [URI, types.TextEdit[]] | [URI, URI, { override?: boolean }]): thing is [URI, URI, { override?: boolean }] { const [f, s] = thing; return URI.isUri(f) && URI.isUri(s); } - function isTextChange(thing: [URI, types.TextEdit[]] | [URI, URI]): thing is [URI, types.TextEdit[]] { + function isTextChange(thing: [URI, types.TextEdit[]] | [URI, URI, { override?: boolean }]): thing is [URI, types.TextEdit[]] { const [f, s] = thing; return URI.isUri(f) && Array.isArray(s); } diff --git a/src/vs/workbench/test/electron-browser/api/mainThreadEditors.test.ts b/src/vs/workbench/test/electron-browser/api/mainThreadEditors.test.ts index 7277d63ccc5b2..9e79075d377e7 100644 --- a/src/vs/workbench/test/electron-browser/api/mainThreadEditors.test.ts +++ b/src/vs/workbench/test/electron-browser/api/mainThreadEditors.test.ts @@ -135,9 +135,9 @@ suite('MainThreadEditors', () => { test(`applyWorkspaceEdit with only resource edit`, () => { return editors.$tryApplyWorkspaceEdit({ edits: [ - { oldUri: resource, newUri: resource }, - { oldUri: undefined, newUri: resource }, - { oldUri: resource, newUri: undefined } + { oldUri: resource, newUri: resource, options: undefined }, + { oldUri: undefined, newUri: resource, options: undefined }, + { oldUri: resource, newUri: undefined, options: undefined } ] }).then((result) => { assert.equal(result, true);