Skip to content

Commit

Permalink
add proposed api for workspace edit #10659
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Jun 18, 2018
1 parent e037cdb commit 40df36b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as vscode from 'vscode';
import { createRandomFile, deleteFile, closeAllEditors, pathEquals } from '../utils';
import { join, basename } from 'path';
import * as fs from 'fs';
import { networkInterfaces } from 'os';

suite('workspace-namespace', () => {

Expand Down Expand Up @@ -520,42 +521,51 @@ suite('workspace-namespace', () => {
});


// test('applyEdit should fail when editing deleted resource', async () => {
// const resource = await createRandomFile();
test('applyEdit should fail when editing deleted resource', async () => {
const resource = await createRandomFile();

// const edit = new vscode.WorkspaceEdit();
// edit.deleteResource(resource);
// try {
// edit.insert(resource, new vscode.Position(0, 0), '');
// assert.fail(false, 'Should disallow edit of deleted resource');
// } catch {
// // noop
// }
// });
const edit = new vscode.WorkspaceEdit();
edit.deleteFile(resource);
edit.insert(resource, new vscode.Position(0, 0), '');

// test('applyEdit should fail when renaming deleted resource', async () => {
// const resource = await createRandomFile();
let success = await vscode.workspace.applyEdit(edit);
assert.equal(success, false);
});

// const edit = new vscode.WorkspaceEdit();
// edit.deleteResource(resource);
// try {
// edit.renameResource(resource, resource);
// assert.fail(false, 'Should disallow rename of deleted resource');
// } catch {
// // noop
// }
// });
test('applyEdit should fail when renaming deleted resource', async () => {
const resource = await createRandomFile();

const edit = new vscode.WorkspaceEdit();
edit.deleteFile(resource);
edit.renameFile(resource, resource);

// test('applyEdit should fail when editing renamed from resource', async () => {
// const resource = await createRandomFile();
// const newResource = vscode.Uri.parse(resource.fsPath + '.1');
let success = await vscode.workspace.applyEdit(edit);
assert.equal(success, false);
});

test('applyEdit should fail when editing renamed from resource', async () => {
const resource = await createRandomFile();
const newResource = vscode.Uri.parse(resource.fsPath + '.1');
const edit = new vscode.WorkspaceEdit();
edit.renameFile(resource, newResource);
edit.insert(resource, new vscode.Position(0, 0), '');

let success = await vscode.workspace.applyEdit(edit);
assert.equal(success, false);
});

// test('applyEdit "edit A -> rename A to B -> edit B"', async () => {
// const oldUri = await createRandomFile();
// const newUri = oldUri.with({ path: oldUri.path + 'NEW' });
// const edit = new vscode.WorkspaceEdit();
// edit.renameResource(resource, newResource);
// try {
// edit.insert(resource, new vscode.Position(0, 0), '');
// assert.fail(false, 'Should disallow editing renamed file');
// } catch {
// // noop
// }
// edit.insert(oldUri, new vscode.Position(0, 0), 'BEFORE');
// edit.renameFile(oldUri, newUri);
// edit.insert(newUri, new vscode.Position(0, 0), 'AFTER');

// let success = await vscode.workspace.applyEdit(edit);
// assert.equal(success, true);

// let doc = await vscode.workspace.openTextDocument(newUri);
// assert.equal(doc.getText(), 'AFTERBEFORE');
// });
});
20 changes: 2 additions & 18 deletions src/vs/workbench/api/node/extHostTextEditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { TextEditorSelectionChangeKind } from './extHostTypes';
import * as TypeConverters from './extHostTypeConverters';
import { TextEditorDecorationType, ExtHostTextEditor } from './extHostTextEditor';
import { ExtHostDocumentsAndEditors } from './extHostDocumentsAndEditors';
import { MainContext, MainThreadTextEditorsShape, ExtHostEditorsShape, ITextDocumentShowOptions, ITextEditorPositionData, IMainContext, WorkspaceEditDto, IEditorPropertiesChangeData } from './extHost.protocol';
import { MainContext, MainThreadTextEditorsShape, ExtHostEditorsShape, ITextDocumentShowOptions, ITextEditorPositionData, IMainContext, IEditorPropertiesChangeData } from './extHost.protocol';
import * as vscode from 'vscode';

export class ExtHostEditors implements ExtHostEditorsShape {
Expand Down Expand Up @@ -91,23 +91,7 @@ export class ExtHostEditors implements ExtHostEditorsShape {
}

applyWorkspaceEdit(edit: vscode.WorkspaceEdit): TPromise<boolean> {

const dto: WorkspaceEditDto = { edits: [] };

for (let entry of edit.entries()) {
let [uri, uriOrEdits] = entry;
if (Array.isArray(uriOrEdits)) {
let doc = this._extHostDocumentsAndEditors.getDocument(uri.toString());
dto.edits.push({
resource: uri,
modelVersionId: doc && doc.version,
edits: uriOrEdits.map(TypeConverters.TextEdit.from)
});
// } else {
// dto.edits.push({ oldUri: uri, newUri: uriOrEdits });
}
}

const dto = TypeConverters.WorkspaceEdit.from(edit, this._extHostDocumentsAndEditors);
return this._proxy.$tryApplyWorkspaceEdit(dto);
}

Expand Down
6 changes: 4 additions & 2 deletions src/vs/workbench/api/node/extHostTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import * as languageSelector from 'vs/editor/common/modes/languageSelector';
import { WorkspaceEditDto, ResourceTextEditDto, ResourceFileEditDto } from 'vs/workbench/api/node/extHost.protocol';
import { MarkerSeverity, IRelatedInformation, IMarkerData, MarkerTag } from 'vs/platform/markers/common/markers';
import { ACTIVE_GROUP, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/node/extHostDocumentsAndEditors';

export interface PositionLike {
line: number;
Expand Down Expand Up @@ -269,15 +270,16 @@ export const TextEdit = {
};

export namespace WorkspaceEdit {
export function from(value: vscode.WorkspaceEdit): modes.WorkspaceEdit {
export function from(value: vscode.WorkspaceEdit, documents?: ExtHostDocumentsAndEditors): WorkspaceEditDto {
const result: modes.WorkspaceEdit = {
edits: []
};
for (const entry of (value as types.WorkspaceEdit).allEntries()) {
const [uri, uriOrEdits] = entry;
if (Array.isArray(uriOrEdits)) {
// text edits
result.edits.push({ resource: uri, edits: uriOrEdits.map(TextEdit.from) });
let doc = documents ? documents.getDocument(uri.toString()) : undefined;
result.edits.push({ resource: uri, modelVersionId: doc && doc.version, edits: uriOrEdits.map(TextEdit.from) });
} else {
// resource edits
result.edits.push({ oldUri: uri, newUri: uriOrEdits });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { IEditorService } from 'vs/workbench/services/editor/common/editorServic
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { ILogService } from 'vs/platform/log/common/log';

abstract class Recording {

Expand Down Expand Up @@ -385,6 +386,7 @@ export class BulkEditService implements IBulkEditService {
_serviceBrand: any;

constructor(
@ILogService private readonly _logService: ILogService,
@IModelService private readonly _modelService: IModelService,
@IEditorService private readonly _editorService: IEditorService,
@ITextModelService private readonly _textModelService: ITextModelService,
Expand Down Expand Up @@ -424,11 +426,12 @@ export class BulkEditService implements IBulkEditService {

const bulkEdit = new BulkEdit(options.editor, options.progress, this._textModelService, this._fileService, this._textFileService, this._environmentService, this._contextService);
bulkEdit.add(edits);

return bulkEdit.perform().then(selection => {
return {
selection,
ariaSummary: bulkEdit.ariaMessage()
};
return { selection, ariaSummary: bulkEdit.ariaMessage() };
}, err => {
this._logService.error(err);
return TPromise.wrapError(err);
});
}
}
Expand Down

0 comments on commit 40df36b

Please sign in to comment.