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

debt - use Promise and CancellationToken instead of TPromise

上级 bca795fc
......@@ -12,8 +12,9 @@ import { ITextModel } from 'vs/editor/common/model';
import { registerDefaultLanguageCommand, registerLanguageCommand } from 'vs/editor/browser/editorExtensions';
import { DocumentFormattingEditProviderRegistry, DocumentRangeFormattingEditProviderRegistry, OnTypeFormattingEditProviderRegistry, FormattingOptions, TextEdit } from 'vs/editor/common/modes';
import { IModelService } from 'vs/editor/common/services/modelService';
import { asWinJsPromise, first } from 'vs/base/common/async';
import { asWinJsPromise, first2 } from 'vs/base/common/async';
import { Position } from 'vs/editor/common/core/position';
import { CancellationToken } from 'vs/base/common/cancellation';
export class NoProviderError extends Error {
......@@ -26,30 +27,30 @@ export class NoProviderError extends Error {
}
}
export function getDocumentRangeFormattingEdits(model: ITextModel, range: Range, options: FormattingOptions): TPromise<TextEdit[], NoProviderError> {
export function getDocumentRangeFormattingEdits(model: ITextModel, range: Range, options: FormattingOptions, token: CancellationToken): Promise<TextEdit[]> {
const providers = DocumentRangeFormattingEditProviderRegistry.ordered(model);
if (providers.length === 0) {
return TPromise.wrapError(new NoProviderError());
return Promise.reject(new NoProviderError());
}
return first(providers.map(provider => () => {
return asWinJsPromise(token => provider.provideDocumentRangeFormattingEdits(model, range, options, token))
return first2(providers.map(provider => () => {
return Promise.resolve(provider.provideDocumentRangeFormattingEdits(model, range, options, token))
.then(undefined, onUnexpectedExternalError);
}), result => !isFalsyOrEmpty(result));
}
export function getDocumentFormattingEdits(model: ITextModel, options: FormattingOptions): TPromise<TextEdit[]> {
export function getDocumentFormattingEdits(model: ITextModel, options: FormattingOptions, token: CancellationToken): Promise<TextEdit[]> {
const providers = DocumentFormattingEditProviderRegistry.ordered(model);
// try range formatters when no document formatter is registered
if (providers.length === 0) {
return getDocumentRangeFormattingEdits(model, model.getFullModelRange(), options);
return getDocumentRangeFormattingEdits(model, model.getFullModelRange(), options, token);
}
return first(providers.map(provider => () => {
return asWinJsPromise(token => provider.provideDocumentFormattingEdits(model, options, token))
return first2(providers.map(provider => () => {
return Promise.resolve(provider.provideDocumentFormattingEdits(model, options, token))
.then(undefined, onUnexpectedExternalError);
}), result => !isFalsyOrEmpty(result));
}
......@@ -77,7 +78,7 @@ registerLanguageCommand('_executeFormatRangeProvider', function (accessor, args)
if (!model) {
throw illegalArgument('resource');
}
return getDocumentRangeFormattingEdits(model, Range.lift(range), options);
return getDocumentRangeFormattingEdits(model, Range.lift(range), options, CancellationToken.None);
});
registerLanguageCommand('_executeFormatDocumentProvider', function (accessor, args) {
......@@ -90,7 +91,7 @@ registerLanguageCommand('_executeFormatDocumentProvider', function (accessor, ar
throw illegalArgument('resource');
}
return getDocumentFormattingEdits(model, options);
return getDocumentFormattingEdits(model, options, CancellationToken.None);
});
registerDefaultLanguageCommand('_executeFormatOnTypeProvider', function (model, position, args) {
......
......@@ -27,6 +27,7 @@ import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { ISingleEditOperation } from 'vs/editor/common/model';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { CancellationToken } from 'vs/base/common/cancellation';
function alertFormattingEdits(edits: ISingleEditOperation[]): void {
......@@ -239,7 +240,7 @@ class FormatOnPaste implements editorCommon.IEditorContribution {
const { tabSize, insertSpaces } = model.getOptions();
const state = new EditorState(this.editor, CodeEditorStateFlag.Value | CodeEditorStateFlag.Position);
getDocumentRangeFormattingEdits(model, range, { tabSize, insertSpaces }).then(edits => {
getDocumentRangeFormattingEdits(model, range, { tabSize, insertSpaces }, CancellationToken.None).then(edits => {
return this.workerService.computeMoreMinimalEdits(model.uri, edits);
}).then(edits => {
if (!state.validate(this.editor) || isFalsyOrEmpty(edits)) {
......@@ -267,7 +268,7 @@ export abstract class AbstractFormatAction extends EditorAction {
const workerService = accessor.get(IEditorWorkerService);
const notificationService = accessor.get(INotificationService);
const formattingPromise = this._getFormattingEdits(editor);
const formattingPromise = this._getFormattingEdits(editor, CancellationToken.None);
if (!formattingPromise) {
return TPromise.as(void 0);
}
......@@ -276,7 +277,7 @@ export abstract class AbstractFormatAction extends EditorAction {
const state = new EditorState(editor, CodeEditorStateFlag.Value | CodeEditorStateFlag.Position);
// Receive formatted value from worker
return formattingPromise.then(edits => workerService.computeMoreMinimalEdits(editor.getModel().uri, edits)).then(edits => {
return TPromise.wrap(formattingPromise).then(edits => workerService.computeMoreMinimalEdits(editor.getModel().uri, edits)).then(edits => {
if (!state.validate(editor) || isFalsyOrEmpty(edits)) {
return;
}
......@@ -293,7 +294,8 @@ export abstract class AbstractFormatAction extends EditorAction {
});
}
protected abstract _getFormattingEdits(editor: ICodeEditor): TPromise<ISingleEditOperation[]>;
protected abstract _getFormattingEdits(editor: ICodeEditor, token: CancellationToken): Promise<ISingleEditOperation[]>;
protected _notifyNoProviderError(notificationService: INotificationService, language: string): void {
notificationService.info(nls.localize('no.provider', "There is no formatter for '{0}'-files installed.", language));
}
......@@ -322,10 +324,10 @@ export class FormatDocumentAction extends AbstractFormatAction {
});
}
protected _getFormattingEdits(editor: ICodeEditor): TPromise<ISingleEditOperation[]> {
protected _getFormattingEdits(editor: ICodeEditor, token: CancellationToken): Promise<ISingleEditOperation[]> {
const model = editor.getModel();
const { tabSize, insertSpaces } = model.getOptions();
return getDocumentFormattingEdits(model, { tabSize, insertSpaces });
return getDocumentFormattingEdits(model, { tabSize, insertSpaces }, token);
}
protected _notifyNoProviderError(notificationService: INotificationService, language: string): void {
......@@ -354,10 +356,10 @@ export class FormatSelectionAction extends AbstractFormatAction {
});
}
protected _getFormattingEdits(editor: ICodeEditor): TPromise<ISingleEditOperation[]> {
protected _getFormattingEdits(editor: ICodeEditor, token: CancellationToken): Promise<ISingleEditOperation[]> {
const model = editor.getModel();
const { tabSize, insertSpaces } = model.getOptions();
return getDocumentRangeFormattingEdits(model, editor.getSelection(), { tabSize, insertSpaces });
return getDocumentRangeFormattingEdits(model, editor.getSelection(), { tabSize, insertSpaces }, token);
}
protected _notifyNoProviderError(notificationService: INotificationService, language: string): void {
......@@ -379,14 +381,14 @@ CommandsRegistry.registerCommand('editor.action.format', accessor => {
constructor() {
super({} as IActionOptions);
}
_getFormattingEdits(editor: ICodeEditor): TPromise<ISingleEditOperation[]> {
_getFormattingEdits(editor: ICodeEditor, token: CancellationToken): Promise<ISingleEditOperation[]> {
const model = editor.getModel();
const editorSelection = editor.getSelection();
const { tabSize, insertSpaces } = model.getOptions();
return editorSelection.isEmpty()
? getDocumentFormattingEdits(model, { tabSize, insertSpaces })
: getDocumentRangeFormattingEdits(model, editorSelection, { tabSize, insertSpaces });
? getDocumentFormattingEdits(model, { tabSize, insertSpaces }, token)
: getDocumentRangeFormattingEdits(model, editorSelection, { tabSize, insertSpaces }, token);
}
}().run(accessor, editor);
}
......
......@@ -37,6 +37,7 @@ import { applyCodeAction } from 'vs/editor/contrib/codeAction/codeActionCommands
import { getCodeActions } from 'vs/editor/contrib/codeAction/codeAction';
import { ICodeActionsOnSaveOptions } from 'vs/editor/common/config/editorOptions';
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
export interface ISaveParticipantParticipant extends ISaveParticipant {
// progressMessage: string;
......@@ -215,11 +216,12 @@ class FormatOnSaveParticipant implements ISaveParticipantParticipant {
const timeout = this._configurationService.getValue('editor.formatOnSaveTimeout', { overrideIdentifier: model.getLanguageIdentifier().language, resource: editorModel.getResource() });
return new Promise<ISingleEditOperation[]>((resolve, reject) => {
let request = getDocumentFormattingEdits(model, { tabSize, insertSpaces });
let source = new CancellationTokenSource();
let request = getDocumentFormattingEdits(model, { tabSize, insertSpaces }, source.token);
setTimeout(() => {
reject(localize('timeout.formatOnSave', "Aborted format on save after {0}ms", timeout));
request.cancel();
source.cancel();
}, timeout);
request.then(edits => this._editorWorkerService.computeMoreMinimalEdits(model.uri, edits)).then(resolve, err => {
......
......@@ -1011,7 +1011,7 @@ suite('ExtHostLanguageFeatures', function () {
}));
return rpcProtocol.sync().then(() => {
return getDocumentFormattingEdits(model, { insertSpaces: true, tabSize: 4 }).then(value => {
return getDocumentFormattingEdits(model, { insertSpaces: true, tabSize: 4 }, CancellationToken.None).then(value => {
assert.equal(value.length, 2);
let [first, second] = value;
assert.equal(first.text, 'testing');
......@@ -1032,7 +1032,7 @@ suite('ExtHostLanguageFeatures', function () {
}));
return rpcProtocol.sync().then(() => {
return getDocumentFormattingEdits(model, { insertSpaces: true, tabSize: 4 });
return getDocumentFormattingEdits(model, { insertSpaces: true, tabSize: 4 }, CancellationToken.None);
});
});
......@@ -1057,7 +1057,7 @@ suite('ExtHostLanguageFeatures', function () {
}));
return rpcProtocol.sync().then(() => {
return getDocumentFormattingEdits(model, { insertSpaces: true, tabSize: 4 }).then(value => {
return getDocumentFormattingEdits(model, { insertSpaces: true, tabSize: 4 }, CancellationToken.None).then(value => {
assert.equal(value.length, 1);
let [first] = value;
assert.equal(first.text, 'testing');
......@@ -1074,7 +1074,7 @@ suite('ExtHostLanguageFeatures', function () {
}));
return rpcProtocol.sync().then(() => {
return getDocumentRangeFormattingEdits(model, new EditorRange(1, 1, 1, 1), { insertSpaces: true, tabSize: 4 }).then(value => {
return getDocumentRangeFormattingEdits(model, new EditorRange(1, 1, 1, 1), { insertSpaces: true, tabSize: 4 }, CancellationToken.None).then(value => {
assert.equal(value.length, 1);
let [first] = value;
assert.equal(first.text, 'testing');
......@@ -1100,7 +1100,7 @@ suite('ExtHostLanguageFeatures', function () {
}
}));
return rpcProtocol.sync().then(() => {
return getDocumentRangeFormattingEdits(model, new EditorRange(1, 1, 1, 1), { insertSpaces: true, tabSize: 4 }).then(value => {
return getDocumentRangeFormattingEdits(model, new EditorRange(1, 1, 1, 1), { insertSpaces: true, tabSize: 4 }, CancellationToken.None).then(value => {
assert.equal(value.length, 1);
let [first] = value;
assert.equal(first.text, 'range2');
......@@ -1120,7 +1120,7 @@ suite('ExtHostLanguageFeatures', function () {
}));
return rpcProtocol.sync().then(() => {
return getDocumentRangeFormattingEdits(model, new EditorRange(1, 1, 1, 1), { insertSpaces: true, tabSize: 4 });
return getDocumentRangeFormattingEdits(model, new EditorRange(1, 1, 1, 1), { insertSpaces: true, tabSize: 4 }, CancellationToken.None);
});
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册