diff --git a/src/vs/workbench/api/node/extHostDocumentSaveParticipant.ts b/src/vs/workbench/api/node/extHostDocumentSaveParticipant.ts index 50f1aa656dc620b9e116004c5729c570a51cb848..fcda092983ad012e1c2ecd0e0f4165dd3016743f 100644 --- a/src/vs/workbench/api/node/extHostDocumentSaveParticipant.ts +++ b/src/vs/workbench/api/node/extHostDocumentSaveParticipant.ts @@ -11,14 +11,14 @@ import {sequence} from 'vs/base/common/async'; import {illegalState} from 'vs/base/common/errors'; import {TPromise} from 'vs/base/common/winjs.base'; import {MainThreadWorkspaceShape} from 'vs/workbench/api/node/extHost.protocol'; +import {TextEdit} from 'vs/workbench/api/node/extHostTypes'; import {fromRange} from 'vs/workbench/api/node/extHostTypeConverters'; import {IResourceEdit} from 'vs/editor/common/services/bulkEdit'; import {ExtHostDocuments} from 'vs/workbench/api/node/extHostDocuments'; export interface TextDocumentWillSaveEvent { document: vscode.TextDocument; - pushEdits(edits: vscode.TextEdit[]): void; - waitUntil(t: Thenable): void; + waitUntil(t: Thenable): void; } export class ExtHostDocumentSaveParticipant { @@ -60,28 +60,15 @@ export class ExtHostDocumentSaveParticipant { private _deliverEventAsync(listener: Function, thisArg: any, document: vscode.TextDocument): TPromise { - const promises: TPromise[] = []; - const resourceEdits: IResourceEdit[] = []; + const promises: TPromise[] = []; const {version} = document; const event = Object.freeze( { document, - pushEdits(edits) { - if (Object.isFrozen(resourceEdits)) { - throw illegalState('pushEdits can not be called anymore'); - } - for (const {newText, range} of edits) { - resourceEdits.push({ - newText, - range: fromRange(range), - resource: document.uri, - }); - } - }, - waitUntil(p: Thenable) { + waitUntil(p: Thenable) { if (Object.isFrozen(promises)) { - throw illegalState('waitUntil can not be called anymore'); + throw illegalState('waitUntil can not be called async'); } promises.push(TPromise.wrap(p)); } @@ -94,21 +81,33 @@ export class ExtHostDocumentSaveParticipant { // freeze promises after event call Object.freeze(promises); - return TPromise.join(promises).then(() => { - // freeze edits after async/sync is done - Object.freeze(resourceEdits); + return TPromise.join(promises).then(values => { + + const edits: IResourceEdit[] = []; + for (const value of values) { + if (Array.isArray(value) && ( value).every(e => e instanceof TextEdit)) { + for (const {newText, range} of value) { + edits.push({ + resource: document.uri, + range: fromRange(range), + newText + }); + } + } + } - if (resourceEdits.length === 0) { + // apply edits iff any and iff document + // didn't change somehow in the meantime + if (edits.length === 0) { return; } - if (version !== document.version) { - // TODO@joh - fail? - return; + if (version === document.version) { + return this._workspace.$applyWorkspaceEdit(edits); } - // apply edits iff any - return this._workspace.$applyWorkspaceEdit(resourceEdits); + // TODO@joh - fail? + console.warn('IGNORING changes because document has changed while computing changes'); }, err => { // ignore error diff --git a/src/vs/workbench/test/node/api/extHostDocumentSaveParticipant.test.ts b/src/vs/workbench/test/node/api/extHostDocumentSaveParticipant.test.ts index bd4285b2ebd41a99ab83f1e907e30a9320c548bc..a0ec404c9f60054e146e33b0e080c112a5560857 100644 --- a/src/vs/workbench/test/node/api/extHostDocumentSaveParticipant.test.ts +++ b/src/vs/workbench/test/node/api/extHostDocumentSaveParticipant.test.ts @@ -168,7 +168,7 @@ suite('ExtHostDocumentSaveParticipant', () => { }); let sub = participant.onWillSaveTextDocumentEvent(function (e) { - e.pushEdits([TextEdit.insert(new Position(0, 0), 'bar')]); + e.waitUntil(TPromise.as([TextEdit.insert(new Position(0, 0), 'bar')])); }); return participant.$participateInSave(resource).then(() => {