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

let plugin host commands internally also accept ICommandHandlerDescriptions...

let plugin host commands internally also accept ICommandHandlerDescriptions and have a way to get them all
上级 2f26afe5
...@@ -105,7 +105,6 @@ export class PluginHostAPIImplementation { ...@@ -105,7 +105,6 @@ export class PluginHostAPIImplementation {
this._proxy = threadService.getRemotable(MainProcessVSCodeAPIHelper); this._proxy = threadService.getRemotable(MainProcessVSCodeAPIHelper);
this.version = contextService.getConfiguration().env.version; this.version = contextService.getConfiguration().env.version;
this.commands = this._threadService.getRemotable(PluginHostCommands);
this.Uri = URI; this.Uri = URI;
this.Location = extHostTypes.Location; this.Location = extHostTypes.Location;
this.Diagnostic = <any> extHostTypes.Diagnostic; this.Diagnostic = <any> extHostTypes.Diagnostic;
...@@ -138,6 +137,22 @@ export class PluginHostAPIImplementation { ...@@ -138,6 +137,22 @@ export class PluginHostAPIImplementation {
this._proxy.onUnexpectedPluginHostError(errors.transformErrorForSerialization(err)); this._proxy.onUnexpectedPluginHostError(errors.transformErrorForSerialization(err));
}); });
const pluginHostCommands = this._threadService.getRemotable(PluginHostCommands);
this.commands = {
registerCommand<T>(id: string, command: <T>(...args: any[]) => T | Thenable<T>, 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<T>(id: string, ...args: any[]): Thenable<T> {
return pluginHostCommands.executeCommand(id, args);
},
getCommands(filterInternal: boolean = false):Thenable<string[]> {
return pluginHostCommands.getCommands(filterInternal);
}
};
const pluginHostEditors = this._threadService.getRemotable(PluginHostEditors); const pluginHostEditors = this._threadService.getRemotable(PluginHostEditors);
const pluginHostMessageService = new PluginHostMessageService(this._threadService, this.commands); const pluginHostMessageService = new PluginHostMessageService(this._threadService, this.commands);
const pluginHostQuickOpen = new PluginHostQuickOpen(this._threadService); const pluginHostQuickOpen = new PluginHostQuickOpen(this._threadService);
......
...@@ -20,6 +20,7 @@ import * as typeConverters from 'vs/workbench/api/common/pluginHostTypeConverter ...@@ -20,6 +20,7 @@ import * as typeConverters from 'vs/workbench/api/common/pluginHostTypeConverter
import * as types from 'vs/workbench/api/common/pluginHostTypes'; import * as types from 'vs/workbench/api/common/pluginHostTypes';
import {IPosition, IRange, ISingleEditOperation} from 'vs/editor/common/editorCommon'; import {IPosition, IRange, ISingleEditOperation} from 'vs/editor/common/editorCommon';
import * as modes from 'vs/editor/common/modes'; import * as modes from 'vs/editor/common/modes';
import {ICommandHandlerDescription} from 'vs/platform/keybinding/common/keybindingService';
import {CancellationTokenSource} from 'vs/base/common/cancellation'; import {CancellationTokenSource} from 'vs/base/common/cancellation';
import {PluginHostModelService} from 'vs/workbench/api/common/pluginHostDocuments'; import {PluginHostModelService} from 'vs/workbench/api/common/pluginHostDocuments';
import {IMarkerService, IMarker} from 'vs/platform/markers/common/markers'; import {IMarkerService, IMarker} from 'vs/platform/markers/common/markers';
...@@ -44,10 +45,47 @@ export class ExtHostLanguageFeatureCommands { ...@@ -44,10 +45,47 @@ export class ExtHostLanguageFeatureCommands {
constructor(commands: PluginHostCommands) { constructor(commands: PluginHostCommands) {
this._commands = commands; this._commands = commands;
this._register('vscode.executeWorkspaceSymbolProvider', this._executeWorkspaceSymbolProvider); this._register('vscode.executeWorkspaceSymbolProvider', this._executeWorkspaceSymbolProvider, {
this._register('vscode.executeDefinitionProvider', this._executeDefinitionProvider); description: 'Execute all workspace symbol provider.',
this._register('vscode.executeHoverProvider', this._executeHoverProvider); signature: {
this._register('vscode.executeDocumentHighlights', this._executeDocumentHighlights); 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.executeReferenceProvider', this._executeReferenceProvider);
this._register('vscode.executeDocumentRenameProvider', this._executeDocumentRenameProvider); this._register('vscode.executeDocumentRenameProvider', this._executeDocumentRenameProvider);
this._register('vscode.executeSignatureHelpProvider', this._executeSignatureHelpProvider); this._register('vscode.executeSignatureHelpProvider', this._executeSignatureHelpProvider);
...@@ -62,8 +100,9 @@ export class ExtHostLanguageFeatureCommands { ...@@ -62,8 +100,9 @@ export class ExtHostLanguageFeatureCommands {
// --- command impl // --- command impl
private _register(id: string, handler: (...args: any[]) => any): void { private _register(id: string, handler: (...args: any[]) => any, description?: ICommandHandlerDescription): void {
this._disposables.push(this._commands.registerCommand(id, handler, this)); let disposable = this._commands.registerCommand(id, handler, this, description);
this._disposables.push(disposable);
} }
/** /**
......
...@@ -9,7 +9,8 @@ import {IEventService} from 'vs/platform/event/common/event'; ...@@ -9,7 +9,8 @@ import {IEventService} from 'vs/platform/event/common/event';
import {PluginsRegistry} from 'vs/platform/plugins/common/pluginsRegistry'; import {PluginsRegistry} from 'vs/platform/plugins/common/pluginsRegistry';
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions'; import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry'; 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 {TPromise} from 'vs/base/common/winjs.base';
import {PluginHostEditors} from 'vs/workbench/api/common/pluginHostEditors'; import {PluginHostEditors} from 'vs/workbench/api/common/pluginHostEditors';
import {IMessageService, Severity} from 'vs/platform/message/common/message'; import {IMessageService, Severity} from 'vs/platform/message/common/message';
...@@ -17,10 +18,16 @@ import {canSerialize} from 'vs/base/common/marshalling'; ...@@ -17,10 +18,16 @@ import {canSerialize} from 'vs/base/common/marshalling';
import {toErrorMessage} from 'vs/base/common/errors'; import {toErrorMessage} from 'vs/base/common/errors';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
interface CommandHandler {
callback: Function;
thisArg: any;
description: ICommandHandlerDescription;
}
@Remotable.PluginHostContext('PluginHostCommands') @Remotable.PluginHostContext('PluginHostCommands')
export class PluginHostCommands { export class PluginHostCommands {
private _commands: { [n: string]: Function } = Object.create(null); private _commands: { [n: string]: CommandHandler } = Object.create(null);
private _proxy: MainThreadCommands; private _proxy: MainThreadCommands;
private _pluginHostEditors: PluginHostEditors; private _pluginHostEditors: PluginHostEditors;
...@@ -29,7 +36,7 @@ export class PluginHostCommands { ...@@ -29,7 +36,7 @@ export class PluginHostCommands {
this._proxy = threadService.getRemotable(MainThreadCommands); this._proxy = threadService.getRemotable(MainThreadCommands);
} }
registerCommand(id: string, command: <T>(...args: any[]) => T | Thenable<T>, thisArgs?: any): vscode.Disposable { registerCommand(id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, description?: ICommandHandlerDescription): vscode.Disposable {
if (!id.trim().length) { if (!id.trim().length) {
throw new Error('invalid id'); throw new Error('invalid id');
...@@ -39,8 +46,8 @@ export class PluginHostCommands { ...@@ -39,8 +46,8 @@ export class PluginHostCommands {
throw new Error('command with id already exists'); throw new Error('command with id already exists');
} }
this._commands[id] = thisArgs ? command.bind(thisArgs) : command; this._commands[id] = { callback, thisArg, description };
this._proxy._registerCommand(id); this._proxy.$registerCommand(id);
return { return {
dispose: () => { dispose: () => {
...@@ -49,12 +56,12 @@ export class PluginHostCommands { ...@@ -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; 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(); let activeTextEditor = this._pluginHostEditors.getActiveTextEditor();
if (!activeTextEditor) { 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; return;
} }
...@@ -62,10 +69,10 @@ export class PluginHostCommands { ...@@ -62,10 +69,10 @@ export class PluginHostCommands {
actualCallback(activeTextEditor, edit); actualCallback(activeTextEditor, edit);
}).then((result) => { }).then((result) => {
if (!result) { if (!result) {
console.warn('Edits from command ' + commandId + ' were not applied.') console.warn('Edits from command ' + id + ' were not applied.')
} }
}, (err) => { }, (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 { ...@@ -75,7 +82,7 @@ export class PluginHostCommands {
if (this._commands[id]) { if (this._commands[id]) {
// we stay inside the extension host and support // we stay inside the extension host and support
// to pass any kind of parameters around // to pass any kind of parameters around
return this._executeContributedCommand(id, ...args); return this.$executeContributedCommand(id, ...args);
} else { } else {
// // check that we can get all parameters over to // // check that we can get all parameters over to
...@@ -86,18 +93,19 @@ export class PluginHostCommands { ...@@ -86,18 +93,19 @@ export class PluginHostCommands {
// } // }
// } // }
return this._proxy._executeCommand(id, args); return this._proxy.$executeCommand(id, args);
} }
} }
_executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> { $executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
let command = this._commands[id]; let command = this._commands[id];
if (!command) { if (!command) {
return Promise.reject<T>(id); return Promise.reject<T>(id);
} }
try { try {
let result = command.apply(undefined, args); let {callback, thisArg} = command;
let result = callback.apply(thisArg, args);
return Promise.resolve(result); return Promise.resolve(result);
} catch (err) { } catch (err) {
try { try {
...@@ -111,13 +119,24 @@ export class PluginHostCommands { ...@@ -111,13 +119,24 @@ export class PluginHostCommands {
} }
getCommands(filterUnderscoreCommands: boolean = false): Thenable<string[]> { getCommands(filterUnderscoreCommands: boolean = false): Thenable<string[]> {
return this._proxy._getCommands().then(result => { return this._proxy.$getCommands().then(result => {
if (filterUnderscoreCommands) { if (filterUnderscoreCommands) {
result = result.filter(command => command[0] !== '_'); result = result.filter(command => command[0] !== '_');
} }
return result; 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') @Remotable.MainContext('MainThreadCommands')
...@@ -133,12 +152,12 @@ export class MainThreadCommands { ...@@ -133,12 +152,12 @@ export class MainThreadCommands {
this._proxy = this._threadService.getRemotable(PluginHostCommands); this._proxy = this._threadService.getRemotable(PluginHostCommands);
} }
_registerCommand(id: string): TPromise<any> { $registerCommand(id: string): TPromise<any> {
KeybindingsRegistry.registerCommandDesc({ KeybindingsRegistry.registerCommandDesc({
id, id,
handler: (serviceAccessor, ...args: any[]) => { 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, weight: undefined,
context: undefined, context: undefined,
...@@ -152,11 +171,24 @@ export class MainThreadCommands { ...@@ -152,11 +171,24 @@ export class MainThreadCommands {
return undefined; return undefined;
} }
_executeCommand<T>(id: string, args: any[]): Thenable<T> { $executeCommand<T>(id: string, args: any[]): Thenable<T> {
return this._keybindingService.executeCommand(id, args); return this._keybindingService.executeCommand(id, args);
} }
_getCommands(): Thenable<string[]> { $getCommands(): Thenable<string[]> {
return TPromise.as(Object.keys(KeybindingsRegistry.getCommands())); 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册