From b5525f9ac3c8f079e4171525a39cb6bb52177c57 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Wed, 9 Dec 2015 16:29:24 +0100 Subject: [PATCH] let plugin host commands internally also accept ICommandHandlerDescriptions and have a way to get them all --- .../api/browser/pluginHost.api.impl.ts | 17 ++++- .../common/extHostLanguageFeatureCommands.ts | 51 ++++++++++++-- .../api/common/pluginHostCommands.ts | 70 ++++++++++++++----- 3 files changed, 112 insertions(+), 26 deletions(-) diff --git a/src/vs/workbench/api/browser/pluginHost.api.impl.ts b/src/vs/workbench/api/browser/pluginHost.api.impl.ts index 9fbf2866629..413aefa5f35 100644 --- a/src/vs/workbench/api/browser/pluginHost.api.impl.ts +++ b/src/vs/workbench/api/browser/pluginHost.api.impl.ts @@ -105,7 +105,6 @@ export class PluginHostAPIImplementation { this._proxy = threadService.getRemotable(MainProcessVSCodeAPIHelper); this.version = contextService.getConfiguration().env.version; - this.commands = this._threadService.getRemotable(PluginHostCommands); this.Uri = URI; this.Location = extHostTypes.Location; this.Diagnostic = extHostTypes.Diagnostic; @@ -138,6 +137,22 @@ export class PluginHostAPIImplementation { this._proxy.onUnexpectedPluginHostError(errors.transformErrorForSerialization(err)); }); + const pluginHostCommands = this._threadService.getRemotable(PluginHostCommands); + this.commands = { + registerCommand(id: string, command: (...args: any[]) => T | Thenable, thisArgs?: any): vscode.Disposable { + return pluginHostCommands.registerCommand(id, command, thisArgs); + }, + registerTextEditorCommand(commandId: string, callback: (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => void, thisArg?: any): vscode.Disposable { + return pluginHostCommands.registerTextEditorCommand(commandId, callback, thisArg); + }, + executeCommand(id: string, ...args: any[]): Thenable { + return pluginHostCommands.executeCommand(id, args); + }, + getCommands(filterInternal: boolean = false):Thenable { + return pluginHostCommands.getCommands(filterInternal); + } + }; + const pluginHostEditors = this._threadService.getRemotable(PluginHostEditors); const pluginHostMessageService = new PluginHostMessageService(this._threadService, this.commands); const pluginHostQuickOpen = new PluginHostQuickOpen(this._threadService); diff --git a/src/vs/workbench/api/common/extHostLanguageFeatureCommands.ts b/src/vs/workbench/api/common/extHostLanguageFeatureCommands.ts index d8dc7867334..48b5b1879ca 100644 --- a/src/vs/workbench/api/common/extHostLanguageFeatureCommands.ts +++ b/src/vs/workbench/api/common/extHostLanguageFeatureCommands.ts @@ -20,6 +20,7 @@ import * as typeConverters from 'vs/workbench/api/common/pluginHostTypeConverter import * as types from 'vs/workbench/api/common/pluginHostTypes'; import {IPosition, IRange, ISingleEditOperation} from 'vs/editor/common/editorCommon'; import * as modes from 'vs/editor/common/modes'; +import {ICommandHandlerDescription} from 'vs/platform/keybinding/common/keybindingService'; import {CancellationTokenSource} from 'vs/base/common/cancellation'; import {PluginHostModelService} from 'vs/workbench/api/common/pluginHostDocuments'; import {IMarkerService, IMarker} from 'vs/platform/markers/common/markers'; @@ -44,10 +45,47 @@ export class ExtHostLanguageFeatureCommands { constructor(commands: PluginHostCommands) { this._commands = commands; - this._register('vscode.executeWorkspaceSymbolProvider', this._executeWorkspaceSymbolProvider); - this._register('vscode.executeDefinitionProvider', this._executeDefinitionProvider); - this._register('vscode.executeHoverProvider', this._executeHoverProvider); - this._register('vscode.executeDocumentHighlights', this._executeDocumentHighlights); + this._register('vscode.executeWorkspaceSymbolProvider', this._executeWorkspaceSymbolProvider, { + description: 'Execute all workspace symbol provider.', + signature: { + args: [{ name: 'query', constraint: String }], + returns: 'A promise that resolves to an array of SymbolInformation-instances.' + } + }); + + this._register('vscode.executeDefinitionProvider', this._executeDefinitionProvider, { + description: 'Execute all definition provider.', + signature: { + args: [ + { name: 'uri', description: 'Uri of a text document', constraint: URI }, + { name: 'position', description: 'Position of a symbol', constraint: types.Position } + ], + returns: 'A promise that resolves to an array of Location-instances.' + } + }); + + this._register('vscode.executeHoverProvider', this._executeHoverProvider, { + description: 'Execute all definition provider.', + signature: { + args: [ + { name: 'uri', description: 'Uri of a text document', constraint: URI }, + { name: 'position', description: 'Position of a symbol', constraint: types.Position } + ], + returns: 'A promise that resolves to an array of Hover-instances.' + } + }); + + this._register('vscode.executeDocumentHighlights', this._executeDocumentHighlights, { + description: 'Execute document highlight provider.', + signature: { + args: [ + { name: 'uri', description: 'Uri of a text document', constraint: URI }, + { name: 'position', description: 'Position in a text document', constraint: types.Position } + ], + returns: 'A promise that resolves to an array of DocumentHighlight-instances.' + } + }); + this._register('vscode.executeReferenceProvider', this._executeReferenceProvider); this._register('vscode.executeDocumentRenameProvider', this._executeDocumentRenameProvider); this._register('vscode.executeSignatureHelpProvider', this._executeSignatureHelpProvider); @@ -62,8 +100,9 @@ export class ExtHostLanguageFeatureCommands { // --- command impl - private _register(id: string, handler: (...args: any[]) => any): void { - this._disposables.push(this._commands.registerCommand(id, handler, this)); + private _register(id: string, handler: (...args: any[]) => any, description?: ICommandHandlerDescription): void { + let disposable = this._commands.registerCommand(id, handler, this, description); + this._disposables.push(disposable); } /** diff --git a/src/vs/workbench/api/common/pluginHostCommands.ts b/src/vs/workbench/api/common/pluginHostCommands.ts index e6867b96d4b..f17ade3c69f 100644 --- a/src/vs/workbench/api/common/pluginHostCommands.ts +++ b/src/vs/workbench/api/common/pluginHostCommands.ts @@ -9,7 +9,8 @@ import {IEventService} from 'vs/platform/event/common/event'; import {PluginsRegistry} from 'vs/platform/plugins/common/pluginsRegistry'; import {SyncActionDescriptor} from 'vs/platform/actions/common/actions'; import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry'; -import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService'; +import {KeybindingsUtils} from 'vs/platform/keybinding/common/keybindingsUtils'; +import {IKeybindingService, ICommandHandlerDescription} from 'vs/platform/keybinding/common/keybindingService'; import {TPromise} from 'vs/base/common/winjs.base'; import {PluginHostEditors} from 'vs/workbench/api/common/pluginHostEditors'; import {IMessageService, Severity} from 'vs/platform/message/common/message'; @@ -17,10 +18,16 @@ import {canSerialize} from 'vs/base/common/marshalling'; import {toErrorMessage} from 'vs/base/common/errors'; import * as vscode from 'vscode'; +interface CommandHandler { + callback: Function; + thisArg: any; + description: ICommandHandlerDescription; +} + @Remotable.PluginHostContext('PluginHostCommands') export class PluginHostCommands { - private _commands: { [n: string]: Function } = Object.create(null); + private _commands: { [n: string]: CommandHandler } = Object.create(null); private _proxy: MainThreadCommands; private _pluginHostEditors: PluginHostEditors; @@ -29,7 +36,7 @@ export class PluginHostCommands { this._proxy = threadService.getRemotable(MainThreadCommands); } - registerCommand(id: string, command: (...args: any[]) => T | Thenable, thisArgs?: any): vscode.Disposable { + registerCommand(id: string, callback: (...args: any[]) => T | Thenable, thisArg?: any, description?: ICommandHandlerDescription): vscode.Disposable { if (!id.trim().length) { throw new Error('invalid id'); @@ -39,8 +46,8 @@ export class PluginHostCommands { throw new Error('command with id already exists'); } - this._commands[id] = thisArgs ? command.bind(thisArgs) : command; - this._proxy._registerCommand(id); + this._commands[id] = { callback, thisArg, description }; + this._proxy.$registerCommand(id); return { dispose: () => { @@ -49,12 +56,12 @@ export class PluginHostCommands { } } - registerTextEditorCommand(commandId: string, callback: (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => void, thisArg?: any): vscode.Disposable { + registerTextEditorCommand(id: string, callback: (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => void, thisArg?: any): vscode.Disposable { let actualCallback: (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => void = thisArg ? callback.bind(thisArg) : callback; - return this.registerCommand(commandId, () => { + return this.registerCommand(id, () => { let activeTextEditor = this._pluginHostEditors.getActiveTextEditor(); if (!activeTextEditor) { - console.warn('Cannot execute ' + commandId + ' because there is no active text editor.'); + console.warn('Cannot execute ' + id + ' because there is no active text editor.'); return; } @@ -62,10 +69,10 @@ export class PluginHostCommands { actualCallback(activeTextEditor, edit); }).then((result) => { if (!result) { - console.warn('Edits from command ' + commandId + ' were not applied.') + console.warn('Edits from command ' + id + ' were not applied.') } }, (err) => { - console.warn('An error occured while running command ' + commandId, err); + console.warn('An error occured while running command ' + id, err); }); }) } @@ -75,7 +82,7 @@ export class PluginHostCommands { if (this._commands[id]) { // we stay inside the extension host and support // to pass any kind of parameters around - return this._executeContributedCommand(id, ...args); + return this.$executeContributedCommand(id, ...args); } else { // // check that we can get all parameters over to @@ -86,18 +93,19 @@ export class PluginHostCommands { // } // } - return this._proxy._executeCommand(id, args); + return this._proxy.$executeCommand(id, args); } } - _executeContributedCommand(id: string, ...args: any[]): Thenable { + $executeContributedCommand(id: string, ...args: any[]): Thenable { let command = this._commands[id]; if (!command) { return Promise.reject(id); } try { - let result = command.apply(undefined, args); + let {callback, thisArg} = command; + let result = callback.apply(thisArg, args); return Promise.resolve(result); } catch (err) { try { @@ -111,13 +119,24 @@ export class PluginHostCommands { } getCommands(filterUnderscoreCommands: boolean = false): Thenable { - return this._proxy._getCommands().then(result => { + return this._proxy.$getCommands().then(result => { if (filterUnderscoreCommands) { result = result.filter(command => command[0] !== '_'); } return result; }); } + + $getContributedCommandHandlerDescriptions(): TPromise<{ [id: string]: string | ICommandHandlerDescription }> { + const result: { [id: string]: string | ICommandHandlerDescription } = Object.create(null); + for (let id in this._commands) { + let {description} = this._commands[id]; + if (description) { + result[id] = description; + } + } + return TPromise.as(result); + } } @Remotable.MainContext('MainThreadCommands') @@ -133,12 +152,12 @@ export class MainThreadCommands { this._proxy = this._threadService.getRemotable(PluginHostCommands); } - _registerCommand(id: string): TPromise { + $registerCommand(id: string): TPromise { KeybindingsRegistry.registerCommandDesc({ id, handler: (serviceAccessor, ...args: any[]) => { - return this._proxy._executeContributedCommand(id, ...args); //TODO@Joh - we cannot serialize the args + return this._proxy.$executeContributedCommand(id, ...args); //TODO@Joh - we cannot serialize the args }, weight: undefined, context: undefined, @@ -152,11 +171,24 @@ export class MainThreadCommands { return undefined; } - _executeCommand(id: string, args: any[]): Thenable { + $executeCommand(id: string, args: any[]): Thenable { return this._keybindingService.executeCommand(id, args); } - _getCommands(): Thenable { + $getCommands(): Thenable { return TPromise.as(Object.keys(KeybindingsRegistry.getCommands())); } + + getCommandHandlerDescriptions(): TPromise<{ [id: string]: string | ICommandHandlerDescription }> { + return this._proxy.$getContributedCommandHandlerDescriptions().then(result => { + const commands = KeybindingsRegistry.getCommands(); + for (let id in commands) { + let {description} = commands[id]; + if (description) { + result[id] = description; + } + } + return result; + }); + } } \ No newline at end of file -- GitLab