add TextDocumentWillSaveEvent#reason, #239

This commit is contained in:
Johannes Rieken
2016-09-26 17:33:28 +02:00
parent e19b83b5f8
commit e986a2b093
10 changed files with 99 additions and 40 deletions

View File

@@ -84,6 +84,7 @@ export class ExtHostAPIImplementation {
OverviewRulerLane: typeof vscode.OverviewRulerLane;
TextEditorRevealType: typeof vscode.TextEditorRevealType;
EndOfLine: typeof vscode.EndOfLine;
TextDocumentSaveReason: typeof vscode.TextDocumentSaveReason;
TextEditorCursorStyle: typeof vscode.TextEditorCursorStyle;
TextEditorSelectionChangeKind: typeof vscode.TextEditorSelectionChangeKind;
commands: typeof vscode.commands;
@@ -169,6 +170,7 @@ export class ExtHostAPIImplementation {
this.EndOfLine = extHostTypes.EndOfLine;
this.TextEditorCursorStyle = EditorCommon.TextEditorCursorStyle;
this.TextEditorSelectionChangeKind = extHostTypes.TextEditorSelectionChangeKind;
this.TextDocumentSaveReason = extHostTypes.TextDocumentSaveReason;
// env namespace
let telemetryInfo: ITelemetryInfo;

View File

@@ -21,7 +21,7 @@ import {Position as EditorPosition} from 'vs/platform/editor/common/editor';
import {IMessage, IExtensionDescription} from 'vs/platform/extensions/common/extensions';
import {StatusbarAlignment as MainThreadStatusBarAlignment} from 'vs/platform/statusbar/common/statusbar';
import {ITelemetryInfo} from 'vs/platform/telemetry/common/telemetry';
import {ICommandHandlerDescription} from 'vs/platform/commands/common/commands';
import { ICommandHandlerDescription } from 'vs/platform/commands/common/commands';
import * as editorCommon from 'vs/editor/common/editorCommon';
import * as modes from 'vs/editor/common/modes';
@@ -30,6 +30,7 @@ import {IResourceEdit} from 'vs/editor/common/services/bulkEdit';
import {ConfigurationTarget} from 'vs/workbench/services/configuration/common/configurationEditing';
import {IPickOpenEntry, IPickOptions} from 'vs/workbench/services/quickopen/common/quickOpenService';
import { SaveReason } from 'vs/workbench/parts/files/common/files';
import {IWorkspaceSymbol} from 'vs/workbench/parts/search/common/search';
import {IApplyEditsOptions, TextEditorRevealType, ITextEditorConfigurationUpdate, IResolvedTextEditorConfiguration, ISelectionChangeEvent} from './mainThreadEditorsTracker';
@@ -233,7 +234,7 @@ export abstract class ExtHostDocumentsShape {
}
export abstract class ExtHostDocumentSaveParticipantShape {
$participateInSave(resource: URI): TPromise<any[]> { throw ni(); }
$participateInSave(resource: URI, reason: SaveReason): TPromise<any[]> { throw ni(); }
}
export interface ITextEditorAddData {

View File

@@ -12,9 +12,10 @@ import { illegalState } from 'vs/base/common/errors';
import { TPromise } from 'vs/base/common/winjs.base';
import { MainThreadWorkspaceShape, ExtHostDocumentSaveParticipantShape } 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, TextDocumentSaveReason } from 'vs/workbench/api/node/extHostTypeConverters';
import { IResourceEdit } from 'vs/editor/common/services/bulkEdit';
import { ExtHostDocuments } from 'vs/workbench/api/node/extHostDocuments';
import { SaveReason } from 'vs/workbench/parts/files/common/files';
declare class WeakMap<K, V> {
// delete(key: K): boolean;
@@ -57,7 +58,7 @@ export class ExtHostDocumentSaveParticipant extends ExtHostDocumentSaveParticipa
};
}
$participateInSave(resource: URI): TPromise<boolean[]> {
$participateInSave(resource: URI, reason: SaveReason): TPromise<boolean[]> {
const entries = this._callbacks.entries();
let didTimeout = false;
@@ -72,21 +73,21 @@ export class ExtHostDocumentSaveParticipant extends ExtHostDocumentSaveParticipa
}
const document = this._documents.getDocumentData(resource).document;
return this._deliverEventAsyncAndBlameBadListeners(fn, thisArg, document);
return this._deliverEventAsyncAndBlameBadListeners(fn, thisArg, <any> { document, reason: TextDocumentSaveReason.to(reason) });
};
}));
return always(promise, () => clearTimeout(didTimeoutHandle));
}
private _deliverEventAsyncAndBlameBadListeners(listener: Function, thisArg: any, document: vscode.TextDocument): TPromise<any> {
private _deliverEventAsyncAndBlameBadListeners(listener: Function, thisArg: any, stubEvent: vscode.TextDocumentWillSaveEvent): TPromise<any> {
const errors = this._badListeners.get(listener);
if (errors > this._thresholds.errors) {
// bad listener - ignore
return TPromise.wrap(false);
}
return this._deliverEventAsync(listener, thisArg, document).then(() => {
return this._deliverEventAsync(listener, thisArg, stubEvent).then(() => {
// don't send result across the wire
return true;
@@ -104,14 +105,16 @@ export class ExtHostDocumentSaveParticipant extends ExtHostDocumentSaveParticipa
});
}
private _deliverEventAsync(listener: Function, thisArg: any, document: vscode.TextDocument): TPromise<any> {
private _deliverEventAsync(listener: Function, thisArg: any, stubEvent: vscode.TextDocumentWillSaveEvent): TPromise<any> {
const promises: TPromise<any | vscode.TextEdit[]>[] = [];
const {document, reason} = stubEvent;
const {version} = document;
const event = Object.freeze(<vscode.TextDocumentWillSaveEvent>{
document,
reason,
waitUntil(p: Thenable<any | vscode.TextEdit[]>) {
if (Object.isFrozen(promises)) {
throw illegalState('waitUntil can not be called async');

View File

@@ -16,6 +16,7 @@ import {IPosition, ISelection, IRange, IDecorationOptions, ISingleEditOperation}
import {IWorkspaceSymbol} from 'vs/workbench/parts/search/common/search';
import * as vscode from 'vscode';
import URI from 'vs/base/common/uri';
import { SaveReason } from 'vs/workbench/parts/files/common/files';
export interface PositionLike {
line: number;
@@ -436,3 +437,18 @@ export namespace Command {
return result;
}
}
export namespace TextDocumentSaveReason {
export function to(reason: SaveReason): vscode.TextDocumentSaveReason {
switch (reason) {
case SaveReason.AUTO:
return types.TextDocumentSaveReason.Auto;
case SaveReason.EXPLICIT:
return types.TextDocumentSaveReason.Explicit;
case SaveReason.FOCUS_CHANGE:
case SaveReason.WINDOW_CHANGE:
return types.TextDocumentSaveReason.FocusOut;
}
}
}

View File

@@ -825,6 +825,12 @@ export enum EndOfLine {
CRLF = 2
}
export enum TextDocumentSaveReason {
Explicit = 1,
Auto = 2,
FocusOut = 3
}
export enum TextEditorRevealType {
Default = 0,
InCenter = 1,

View File

@@ -20,6 +20,7 @@ import {EditOperationsCommand} from 'vs/editor/contrib/format/common/formatComma
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {TextFileEditorModel} from 'vs/workbench/parts/files/common/editors/textFileEditorModel';
import {ExtHostContext, ExtHostDocumentSaveParticipantShape} from './extHost.protocol';
import { SaveReason } from 'vs/workbench/parts/files/common/files';
class TrimWhitespaceParticipant implements ISaveParticipant {
@@ -30,9 +31,9 @@ class TrimWhitespaceParticipant implements ISaveParticipant {
// Nothing
}
public participate(model: ITextFileEditorModel, env: { isAutoSaved: boolean }): any {
public participate(model: ITextFileEditorModel, env: { reason: SaveReason }): any {
if (this.configurationService.lookup('files.trimTrailingWhitespace').value) {
this.doTrimTrailingWhitespace(model.textEditorModel, env.isAutoSaved);
this.doTrimTrailingWhitespace(model.textEditorModel, env.reason === SaveReason.AUTO);
}
}
@@ -84,9 +85,11 @@ class FormatOnSaveParticipant implements ISaveParticipant {
// Nothing
}
participate(editorModel: ITextFileEditorModel, env: { isAutoSaved: boolean }): TPromise<any> {
participate(editorModel: ITextFileEditorModel, env: { reason: SaveReason }): TPromise<any> {
if (env.reason === SaveReason.AUTO
|| !this._configurationService.lookup('editor.formatOnSave').value) {
if (env.isAutoSaved || !this._configurationService.lookup('editor.formatOnSave').value) {
return;
}
@@ -145,8 +148,8 @@ class ExtHostSaveParticipant implements ISaveParticipant {
this._proxy = threadService.get(ExtHostContext.ExtHostDocumentSaveParticipant);
}
participate(editorModel: ITextFileEditorModel, env: { isAutoSaved: boolean }): TPromise<any> {
return this._proxy.$participateInSave(editorModel.getResource());
participate(editorModel: ITextFileEditorModel, env: { reason: SaveReason }): TPromise<any> {
return this._proxy.$participateInSave(editorModel.getResource(), env.reason);
}
}
@@ -169,7 +172,7 @@ export class SaveParticipant implements ISaveParticipant {
// Hook into model
TextFileEditorModel.setSaveParticipant(this);
}
participate(model: ITextFileEditorModel, env: { isAutoSaved: boolean }): TPromise<any> {
participate(model: ITextFileEditorModel, env: { reason: SaveReason }): TPromise<any> {
const promiseFactory = this._saveParticipants.map(p => () => {
return TPromise.as(p.participate(model, env)).then(undefined, err => {
// console.error(err);