提交 bb655625 编写于 作者: J Johannes Rieken

merge waitUntil and pushEdits

上级 ab64f825
...@@ -11,14 +11,14 @@ import {sequence} from 'vs/base/common/async'; ...@@ -11,14 +11,14 @@ import {sequence} from 'vs/base/common/async';
import {illegalState} from 'vs/base/common/errors'; import {illegalState} from 'vs/base/common/errors';
import {TPromise} from 'vs/base/common/winjs.base'; import {TPromise} from 'vs/base/common/winjs.base';
import {MainThreadWorkspaceShape} from 'vs/workbench/api/node/extHost.protocol'; 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 {fromRange} from 'vs/workbench/api/node/extHostTypeConverters';
import {IResourceEdit} from 'vs/editor/common/services/bulkEdit'; import {IResourceEdit} from 'vs/editor/common/services/bulkEdit';
import {ExtHostDocuments} from 'vs/workbench/api/node/extHostDocuments'; import {ExtHostDocuments} from 'vs/workbench/api/node/extHostDocuments';
export interface TextDocumentWillSaveEvent { export interface TextDocumentWillSaveEvent {
document: vscode.TextDocument; document: vscode.TextDocument;
pushEdits(edits: vscode.TextEdit[]): void; waitUntil(t: Thenable<any | vscode.TextEdit[]>): void;
waitUntil(t: Thenable<any>): void;
} }
export class ExtHostDocumentSaveParticipant { export class ExtHostDocumentSaveParticipant {
...@@ -60,28 +60,15 @@ export class ExtHostDocumentSaveParticipant { ...@@ -60,28 +60,15 @@ export class ExtHostDocumentSaveParticipant {
private _deliverEventAsync(listener: Function, thisArg: any, document: vscode.TextDocument): TPromise<any> { private _deliverEventAsync(listener: Function, thisArg: any, document: vscode.TextDocument): TPromise<any> {
const promises: TPromise<any>[] = []; const promises: TPromise<any | vscode.TextEdit[]>[] = [];
const resourceEdits: IResourceEdit[] = [];
const {version} = document; const {version} = document;
const event = Object.freeze(<TextDocumentWillSaveEvent> { const event = Object.freeze(<TextDocumentWillSaveEvent> {
document, document,
pushEdits(edits) { waitUntil(p: Thenable<any | vscode.TextEdit[]>) {
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: <URI> document.uri,
});
}
},
waitUntil(p: Thenable<any>) {
if (Object.isFrozen(promises)) { 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)); promises.push(TPromise.wrap(p));
} }
...@@ -94,21 +81,33 @@ export class ExtHostDocumentSaveParticipant { ...@@ -94,21 +81,33 @@ export class ExtHostDocumentSaveParticipant {
// freeze promises after event call // freeze promises after event call
Object.freeze(promises); Object.freeze(promises);
return TPromise.join(promises).then(() => { return TPromise.join(promises).then(values => {
// freeze edits after async/sync is done
Object.freeze(resourceEdits); const edits: IResourceEdit[] = [];
for (const value of values) {
if (Array.isArray(value) && (<vscode.TextEdit[]> value).every(e => e instanceof TextEdit)) {
for (const {newText, range} of value) {
edits.push({
resource: <URI>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; return;
} }
if (version !== document.version) { if (version === document.version) {
// TODO@joh - fail? return this._workspace.$applyWorkspaceEdit(edits);
return;
} }
// apply edits iff any // TODO@joh - fail?
return this._workspace.$applyWorkspaceEdit(resourceEdits); console.warn('IGNORING changes because document has changed while computing changes');
}, err => { }, err => {
// ignore error // ignore error
......
...@@ -168,7 +168,7 @@ suite('ExtHostDocumentSaveParticipant', () => { ...@@ -168,7 +168,7 @@ suite('ExtHostDocumentSaveParticipant', () => {
}); });
let sub = participant.onWillSaveTextDocumentEvent(function (e) { 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(() => { return participant.$participateInSave(resource).then(() => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册