From bedec88327174e5d755bc1510dfa3e5aa2dd3f9f Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Fri, 27 Nov 2015 09:26:00 +0100 Subject: [PATCH 1/2] Make executeCommand return a promise --- .../keybinding/browser/keybindingServiceImpl.ts | 12 +++++------- .../platform/keybinding/common/keybindingService.ts | 4 +++- src/vs/workbench/api/common/pluginHostCommands.ts | 2 +- src/vs/workbench/test/browser/servicesTestUtils.ts | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/vs/platform/keybinding/browser/keybindingServiceImpl.ts b/src/vs/platform/keybinding/browser/keybindingServiceImpl.ts index af0ccb3dd2463..048a81e0334da 100644 --- a/src/vs/platform/keybinding/browser/keybindingServiceImpl.ts +++ b/src/vs/platform/keybinding/browser/keybindingServiceImpl.ts @@ -142,7 +142,7 @@ export class AbstractKeybindingService { throw new Error('Not implemented'); } - public executeCommand(commandId: string, args:any): void { + public executeCommand(commandId: string, args:any): TPromise { throw new Error('Not implemented'); } } @@ -300,7 +300,7 @@ export class KeybindingService extends AbstractKeybindingService implements IKey delete this._contexts[String(contextId)]; } - public executeCommand(commandId: string, args:any = {}): any { + public executeCommand(commandId: string, args:any = {}): TPromise { if (!args.context) { var contextId = this._findContextAttr(document.activeElement); var context = this.getContext(contextId); @@ -309,9 +309,7 @@ export class KeybindingService extends AbstractKeybindingService implements IKey args.context = contextValue; } - return this._invokeHandler(commandId, args).done(undefined, err => { - this._messageService.show(Severity.Warning, err); - }); + return this._invokeHandler(commandId, args); } } @@ -356,8 +354,8 @@ class ScopedKeybindingService extends AbstractKeybindingService { this._parent.disposeContext(contextId); } - public executeCommand(commandId: string, args:any): void { - this._parent.executeCommand(commandId, args); + public executeCommand(commandId: string, args:any): TPromise { + return this._parent.executeCommand(commandId, args); } } diff --git a/src/vs/platform/keybinding/common/keybindingService.ts b/src/vs/platform/keybinding/common/keybindingService.ts index 634383082b1ab..5d33814597c2d 100644 --- a/src/vs/platform/keybinding/common/keybindingService.ts +++ b/src/vs/platform/keybinding/common/keybindingService.ts @@ -4,6 +4,7 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; +import {TPromise} from 'vs/base/common/winjs.base'; import {createDecorator, IInstantiationService, ServiceIdentifier, ServicesAccessor} from 'vs/platform/instantiation/common/instantiation'; import {Keybinding} from 'vs/base/common/keyCodes'; @@ -82,5 +83,6 @@ export interface IKeybindingService { lookupKeybindings(commandId: string): Keybinding[]; customKeybindingsCount(): number; - executeCommand(commandId: string, args?: any): any; + executeCommand(commandId: string, args?: any): TPromise; + executeCommand(commandId: string, args?: any): TPromise; } diff --git a/src/vs/workbench/api/common/pluginHostCommands.ts b/src/vs/workbench/api/common/pluginHostCommands.ts index 20d9ca171a5f0..43e01007d31af 100644 --- a/src/vs/workbench/api/common/pluginHostCommands.ts +++ b/src/vs/workbench/api/common/pluginHostCommands.ts @@ -148,7 +148,7 @@ export class MainThreadCommands { } _executeCommand(id: string, args: any[]): Thenable { - return TPromise.as(this._keybindingService.executeCommand(id, args)); + return this._keybindingService.executeCommand(id, args); } _getCommands(): Thenable { diff --git a/src/vs/workbench/test/browser/servicesTestUtils.ts b/src/vs/workbench/test/browser/servicesTestUtils.ts index 44c226bc3c21c..fc87c3db39850 100644 --- a/src/vs/workbench/test/browser/servicesTestUtils.ts +++ b/src/vs/workbench/test/browser/servicesTestUtils.ts @@ -153,7 +153,7 @@ export class TestKeybindingService implements IKeybindingService { public setInstantiationService(instantiationService: IInstantiationService): void { } public setContext(key: string, value: any): void { } public removeContext(key: string): void { } - public executeCommand(commandId: string, args: any): void { } + public executeCommand(commandId: string, args: any): TPromise { return; } public createKey(key: string, defaultValue: T): IKeybindingContextKey { return null; From e33335236ce11c772d591eb4f6712d6fd91ff26f Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Fri, 27 Nov 2015 09:37:05 +0100 Subject: [PATCH 2/2] adopt executeCommand returning a promise --- .../contrib/codelens/browser/codelens.ts | 21 ++++++++++++------- .../platform/actions/common/actionsService.ts | 2 +- .../browser/parts/statusbar/statusbarPart.ts | 2 +- .../workbench/electron-browser/integration.ts | 7 +++++-- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/vs/editor/contrib/codelens/browser/codelens.ts b/src/vs/editor/contrib/codelens/browser/codelens.ts index b65a862004ef7..a4497c8de36c2 100644 --- a/src/vs/editor/contrib/codelens/browser/codelens.ts +++ b/src/vs/editor/contrib/codelens/browser/codelens.ts @@ -11,6 +11,7 @@ import nls = require('vs/nls'); import {format} from 'vs/base/common/strings'; import lifecycle = require('vs/base/common/lifecycle'); import schedulers = require('vs/base/common/async'); +import Severity from 'vs/base/common/severity'; import dom = require('vs/base/browser/dom'); import errors = require('vs/base/common/errors'); import EditorBrowser = require('vs/editor/browser/editorBrowser'); @@ -21,6 +22,7 @@ import referenceSearch = require('vs/editor/contrib/referenceSearch/browser/refe import {IModelService} from 'vs/editor/common/services/modelService'; import {IConfigurationService} from 'vs/platform/configuration/common/configuration'; import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService'; +import {IMessageService} from 'vs/platform/message/common/message'; import {Range} from 'vs/editor/common/core/range'; import {CodeLensRegistry, ICodeLensData, getCodeLensData} from '../common/codelens'; @@ -61,7 +63,7 @@ class CodeLensContentWidget implements EditorBrowser.IContentWidget { private _commands: { [id: string]: Modes.ICommand } = Object.create(null); public constructor(editor: EditorBrowser.ICodeEditor, symbolRange: EditorCommon.IEditorRange, - keybindingService: IKeybindingService) { + keybindingService: IKeybindingService, messageService: IMessageService) { this._id = 'codeLensWidget' + (++CodeLensContentWidget.ID); this._editor = editor; @@ -79,7 +81,9 @@ class CodeLensContentWidget implements EditorBrowser.IContentWidget { let command = this._commands[element.id]; if (command) { editor.focus(); - keybindingService.executeCommand(command.id, command.arguments); + keybindingService.executeCommand(command.id, command.arguments).done(undefined, err => { + messageService.show(Severity.Error, err); + }); } } }); @@ -226,7 +230,7 @@ class CodeLens { public constructor(data: ICodeLensData[], editor: EditorBrowser.ICodeEditor, helper: CodeLensHelper, viewZoneChangeAccessor: EditorBrowser.IViewZoneChangeAccessor, - keybindingService: IKeybindingService) { + keybindingService: IKeybindingService, messageService: IMessageService) { this._editor = editor; this._data = data; @@ -251,7 +255,7 @@ class CodeLens { }); this._viewZone = new CodeLensViewZone(range.startLineNumber - 1); - this._contentWidget = new CodeLensContentWidget(editor, Range.lift(range), keybindingService); + this._contentWidget = new CodeLensContentWidget(editor, Range.lift(range), keybindingService, messageService); this._viewZoneId = viewZoneChangeAccessor.addZone(this._viewZone); this._editor.addContentWidget(this._contentWidget); @@ -343,19 +347,22 @@ export class CodeLensContribution implements EditorCommon.IEditorContribution { private _modelChangeCounter: number; private _configurationService: IConfigurationService; private _keybindingService: IKeybindingService; + private _messageService: IMessageService; private _codeLenseDisabledByMode: boolean; private _currentFindOccPromise:TPromise; constructor(editor: EditorBrowser.ICodeEditor, @IModelService modelService: IModelService, @IConfigurationService configurationService: IConfigurationService, - @IKeybindingService keybindingService: IKeybindingService) { + @IKeybindingService keybindingService: IKeybindingService, + @IMessageService messageService: IMessageService) { this._instanceCount = (++CodeLensContribution.INSTANCE_COUNT); this._editor = editor; this._modelService = modelService; this._configurationService = configurationService; this._keybindingService = keybindingService; + this._messageService = messageService; this._globalToDispose = []; this._localToDispose = []; @@ -553,7 +560,7 @@ export class CodeLensContribution implements EditorCommon.IEditorContribution { groupsIndex++; codeLensIndex++; } else { - this._lenses.splice(codeLensIndex, 0, new CodeLens(groups[groupsIndex], this._editor, helper, accessor, this._keybindingService)); + this._lenses.splice(codeLensIndex, 0, new CodeLens(groups[groupsIndex], this._editor, helper, accessor, this._keybindingService, this._messageService)); codeLensIndex++; groupsIndex++; } @@ -567,7 +574,7 @@ export class CodeLensContribution implements EditorCommon.IEditorContribution { // Create extra symbols while (groupsIndex < groups.length) { - this._lenses.push(new CodeLens(groups[groupsIndex], this._editor, helper, accessor, this._keybindingService)); + this._lenses.push(new CodeLens(groups[groupsIndex], this._editor, helper, accessor, this._keybindingService, this._messageService)); groupsIndex++; } diff --git a/src/vs/platform/actions/common/actionsService.ts b/src/vs/platform/actions/common/actionsService.ts index f36f506668cf3..06d63b3b8bbbd 100644 --- a/src/vs/platform/actions/common/actionsService.ts +++ b/src/vs/platform/actions/common/actionsService.ts @@ -116,7 +116,7 @@ export default class ActionsService implements IActionsService { let label = command.category ? localize('category.label', "{0}: {1}", command.category, command.title) : command.title; let action = new Action(command.command, label, undefined, true, () => { return this._pluginService.activateByEvent(activationEvent).then(() => { - this._keybindingsService.executeCommand(command.command); + return this._keybindingsService.executeCommand(command.command); }); }); this._extensionsActions.push(action); diff --git a/src/vs/workbench/browser/parts/statusbar/statusbarPart.ts b/src/vs/workbench/browser/parts/statusbar/statusbarPart.ts index 7a6bb6a791bd0..aae2b2dd78f11 100644 --- a/src/vs/workbench/browser/parts/statusbar/statusbarPart.ts +++ b/src/vs/workbench/browser/parts/statusbar/statusbarPart.ts @@ -285,7 +285,7 @@ class StatusBarEntryItem implements IStatusbarItem { // Fallback to the keybinding service for any other case else { - this.keybindingService.executeCommand(id); + this.keybindingService.executeCommand(id).done(undefined, err => this.messageService.show(Severity.Error, toErrorMessage(err))); } } } \ No newline at end of file diff --git a/src/vs/workbench/electron-browser/integration.ts b/src/vs/workbench/electron-browser/integration.ts index cd4829ebbdf8b..d1e8699007eb6 100644 --- a/src/vs/workbench/electron-browser/integration.ts +++ b/src/vs/workbench/electron-browser/integration.ts @@ -8,8 +8,10 @@ import {TPromise} from 'vs/base/common/winjs.base'; import errors = require('vs/base/common/errors'); import arrays = require('vs/base/common/arrays'); +import Severity from 'vs/base/common/severity'; import {IPartService} from 'vs/workbench/services/part/common/partService'; import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage'; +import {IMessageService} from 'vs/platform/message/common/message'; import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry'; import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService'; @@ -30,7 +32,8 @@ export class ElectronIntegration { @IWorkspaceContextService private contextService: IWorkspaceContextService, @ITelemetryService private telemetryService: ITelemetryService, @IKeybindingService private keybindingService: IKeybindingService, - @IStorageService private storageService: IStorageService + @IStorageService private storageService: IStorageService, + @IMessageService private messageService: IMessageService ) { } @@ -42,7 +45,7 @@ export class ElectronIntegration { // Support runAction event ipc.on('vscode:runAction', (actionId: string) => { - this.keybindingService.executeCommand(actionId, { from: 'menu' }); + this.keybindingService.executeCommand(actionId, { from: 'menu' }).done(undefined, err => this.messageService.show(Severity.Error, err)); }); // Support options change