Skip to content

Commit

Permalink
rename/create should be able to define override behaviour #10659
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Jun 20, 2018
1 parent c570b73 commit 97f48d7
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/vs/editor/common/modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5100,6 +5100,9 @@ declare namespace monaco.languages {
export interface ResourceFileEdit {
oldUri: Uri;
newUri: Uri;
options: {
override: boolean;
};
}

export interface ResourceTextEdit {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/node/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ export interface WorkspaceSymbolsDto extends IdObject {
export interface ResourceFileEditDto {
oldUri: UriComponents;
newUri: UriComponents;
options: { override?: boolean };
}

export interface ResourceTextEditDto {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/api/node/extHostApiCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -344,7 +344,7 @@ export class ExtHostApiCommands {
position: position && typeConverters.Position.from(position),
newName
};
return this._commands.executeCommand<modes.WorkspaceEdit>('_executeDocumentRenameProvider', args).then(value => {
return this._commands.executeCommand<WorkspaceEditDto>('_executeDocumentRenameProvider', args).then(value => {
if (!value) {
return undefined;
}
Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/api/node/extHostLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,18 +495,18 @@ class RenameAdapter {
return typeConvert.WorkspaceEdit.from(value);
}, err => {
if (typeof err === 'string') {
return <modes.WorkspaceEdit>{
return <WorkspaceEditDto>{
edits: undefined,
rejectReason: err
};
} else if (err instanceof Error && typeof err.message === 'string') {
return <modes.WorkspaceEdit>{
return <WorkspaceEditDto>{
edits: undefined,
rejectReason: err.message
};
} else {
// generic error
return TPromise.wrapError<modes.WorkspaceEdit>(err);
return TPromise.wrapError<WorkspaceEditDto>(err);
}
});
}
Expand Down
9 changes: 5 additions & 4 deletions src/vs/workbench/api/node/extHostTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,18 @@ 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()) {
const [uri, uriOrEdits] = entry;
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(<ResourceTextEditDto>{ resource: uri, modelVersionId: doc && doc.version, edits: uriOrEdits.map(TextEdit.from) });
} else {
// resource edits
result.edits.push({ oldUri: uri, newUri: uriOrEdits });
result.edits.push(<ResourceFileEditDto>{ oldUri: uri, newUri: uriOrEdits, options: entry[2] });
}
}
return result;
Expand All @@ -299,7 +299,8 @@ export namespace WorkspaceEdit {
} else {
result.renameFile(
URI.revive((<ResourceFileEditDto>edit).oldUri),
URI.revive((<ResourceFileEditDto>edit).newUri)
URI.revive((<ResourceFileEditDto>edit).newUri),
(<ResourceFileEditDto>edit).options
);
}
}
Expand Down
32 changes: 22 additions & 10 deletions src/vs/workbench/api/node/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IFileOperation | IFileTextEdit>();

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 {
Expand Down Expand Up @@ -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]]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 97f48d7

Please sign in to comment.