Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plugin] Cache command arguments to safely pass the command over JSON-RPC #5961

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export interface CommandRegistryMain {
}

export interface CommandRegistryExt {
$executeCommand<T>(id: string, ...ars: any[]): PromiseLike<T>;
$executeCommand<T>(id: string, ...ars: any[]): PromiseLike<T | undefined>;
registerArgumentProcessor(processor: ArgumentProcessor): void;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class PluginTree extends TreeImpl {
contextValue: item.contextValue
};
const node = this.getNode(item.id);
if (item.collapsibleState !== TreeViewItemCollapsibleState.None) {
if (item.collapsibleState !== undefined && item.collapsibleState !== TreeViewItemCollapsibleState.None) {
if (CompositeTreeViewNode.is(node)) {
return Object.assign(node, update);
}
Expand All @@ -141,13 +141,14 @@ export class PluginTree extends TreeImpl {
}, update);
}
if (TreeViewNode.is(node)) {
return Object.assign(node, update);
return Object.assign(node, update, { command: item.command });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vinokurig command cannot be applied to composite elements? just checking

Copy link
Contributor Author

@vinokurig vinokurig Aug 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant whether it should be set on the composite according to VS Code expectations? If so it would be nice to adjust it as well. It could be done separately, but needs an issue at least.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raised an issue with question #6025

}
return Object.assign({
id: item.id,
parent,
visible: true,
selected: false
selected: false,
command: item.command
}, update);
}

Expand Down
66 changes: 63 additions & 3 deletions packages/plugin-ext/src/plugin/command-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as theia from '@theia/plugin';
import { CommandRegistryExt, PLUGIN_RPC_CONTEXT as Ext, CommandRegistryMain } from '../common/plugin-api-rpc';
import { RPCProtocol } from '../common/rpc-protocol';
import { Disposable } from './types-impl';
import { KnownCommands } from './type-converters';
import { DisposableCollection } from '@theia/core';

// tslint:disable-next-line:no-any
export type Handler = <T>(...args: any[]) => T | PromiseLike<T>;
export type Handler = <T>(...args: any[]) => T | PromiseLike<T | undefined>;

export interface ArgumentProcessor {
// tslint:disable-next-line:no-any
Expand All @@ -34,10 +39,16 @@ export class CommandRegistryImpl implements CommandRegistryExt {
private readonly commands = new Set<string>();
private readonly handlers = new Map<string, Handler>();
private readonly argumentProcessors: ArgumentProcessor[];
private readonly commandsConverter: CommandsConverter;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CommandRegistryImpl never does anything with the "commandsConverter". IMO, the converter should be owned by
"TreeViewExtImpl", not the command registry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be used by other APIs.


constructor(rpc: RPCProtocol) {
this.proxy = rpc.getProxy(Ext.COMMAND_REGISTRY_MAIN);
this.argumentProcessors = [];
this.commandsConverter = new CommandsConverter(this);
}

get converter(): CommandsConverter {
return this.commandsConverter;
}

// tslint:disable-next-line:no-any
Expand Down Expand Up @@ -78,7 +89,7 @@ export class CommandRegistryImpl implements CommandRegistryExt {
}

// tslint:disable-next-line:no-any
$executeCommand<T>(id: string, ...args: any[]): PromiseLike<T> {
$executeCommand<T>(id: string, ...args: any[]): PromiseLike<T | undefined> {
if (this.handlers.has(id)) {
return this.executeLocalCommand(id, ...args);
} else {
Expand All @@ -102,7 +113,7 @@ export class CommandRegistryImpl implements CommandRegistryExt {
}

// tslint:disable-next-line:no-any
private async executeLocalCommand<T>(id: string, ...args: any[]): Promise<T> {
private async executeLocalCommand<T>(id: string, ...args: any[]): Promise<T | undefined> {
const handler = this.handlers.get(id);
if (handler) {
return handler<T>(...args.map(arg => this.argumentProcessors.reduce((r, p) => p.processArgument(r), arg)));
Expand All @@ -123,3 +134,52 @@ export class CommandRegistryImpl implements CommandRegistryExt {
this.argumentProcessors.push(processor);
}
}

// copied and modified from https://github.com/microsoft/vscode/blob/1.37.1/src/vs/workbench/api/common/extHostCommands.ts#L217-L259
export class CommandsConverter {
akosyakov marked this conversation as resolved.
Show resolved Hide resolved

private readonly safeCommandId: string;
private readonly commands: CommandRegistryImpl;
private readonly commandsMap = new Map<number, theia.Command>();
private handle = 0;
private isSafeCommandRegistered: boolean;

constructor(commands: CommandRegistryImpl) {
this.safeCommandId = `theia_safe_cmd_${Date.now().toString()}`;
this.commands = commands;
this.isSafeCommandRegistered = false;
}

/**
* Convert to a command that can be safely passed over JSON-RPC.
*/
toSafeCommand(command: theia.Command, disposables: DisposableCollection): theia.Command {
if (!this.isSafeCommandRegistered) {
this.commands.registerCommand({ id: this.safeCommandId }, this.executeSafeCommand, this);
this.isSafeCommandRegistered = true;
}

const result: theia.Command = {};
Object.assign(result, command);

if (command.command && command.arguments && command.arguments.length > 0) {
tsmaeder marked this conversation as resolved.
Show resolved Hide resolved
const id = this.handle++;
this.commandsMap.set(id, command);
disposables.push(new Disposable(() => this.commandsMap.delete(id)));
result.command = this.safeCommandId;
result.arguments = [id];
}

return result;
}

// tslint:disable-next-line:no-any
private executeSafeCommand<R>(...args: any[]): PromiseLike<R | undefined> {
const command = this.commandsMap.get(args[0]);
if (!command || !command.command) {
return Promise.reject('command NOT FOUND');
}
return this.commands.executeCommand(command.command, ...(command.arguments || []));
}

}
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export function createAPIFactory(
return function (plugin: InternalPlugin): typeof theia {
const commands: typeof theia.commands = {
// tslint:disable-next-line:no-any
registerCommand(command: theia.CommandDescription, handler?: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any): Disposable {
registerCommand(command: theia.CommandDescription, handler?: <T>(...args: any[]) => T | Thenable<T | undefined>, thisArg?: any): Disposable {
return commandRegistry.registerCommand(command, handler, thisArg);
},
// tslint:disable-next-line:no-any
Expand Down
19 changes: 11 additions & 8 deletions packages/plugin-ext/src/plugin/tree/tree-views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ import { Emitter } from '@theia/core/lib/common/event';
import { Disposable, ThemeIcon } from '../types-impl';
import { Plugin, PLUGIN_RPC_CONTEXT, TreeViewsExt, TreeViewsMain, TreeViewItem } from '../../common/plugin-api-rpc';
import { RPCProtocol } from '../../common/rpc-protocol';
import { CommandRegistryImpl } from '../command-registry';
import { CommandRegistryImpl, CommandsConverter } from '../command-registry';
import { TreeViewSelection } from '../../common';
import { PluginPackage } from '../../common/plugin-protocol';
import { DisposableCollection } from '@theia/core/lib/common/disposable';
import { toInternalCommand } from '../type-converters';

export class TreeViewsExtImpl implements TreeViewsExt {

private proxy: TreeViewsMain;

private treeViews: Map<string, TreeViewExtImpl<any>> = new Map<string, TreeViewExtImpl<any>>();

constructor(rpc: RPCProtocol, commandRegistry: CommandRegistryImpl) {
constructor(rpc: RPCProtocol, readonly commandRegistry: CommandRegistryImpl) {
this.proxy = rpc.getProxy(PLUGIN_RPC_CONTEXT.TREE_VIEWS_MAIN);
commandRegistry.registerArgumentProcessor({
processArgument: arg => {
Expand Down Expand Up @@ -61,7 +63,7 @@ export class TreeViewsExtImpl implements TreeViewsExt {
throw new Error('Options with treeDataProvider is mandatory');
}

const treeView = new TreeViewExtImpl(plugin, treeViewId, options.treeDataProvider, this.proxy);
const treeView = new TreeViewExtImpl(plugin, treeViewId, options.treeDataProvider, this.proxy, this.commandRegistry.converter);
this.treeViews.set(treeViewId, treeView);

return {
Expand Down Expand Up @@ -120,6 +122,8 @@ class TreeViewExtImpl<T> extends Disposable {
private onDidCollapseElementEmitter: Emitter<TreeViewExpansionEvent<T>> = new Emitter<TreeViewExpansionEvent<T>>();
public readonly onDidCollapseElement = this.onDidCollapseElementEmitter.event;

private disposables = new DisposableCollection();

private selection: T[] = [];
get selectedElements(): T[] { return this.selection; }

Expand All @@ -129,7 +133,8 @@ class TreeViewExtImpl<T> extends Disposable {
private plugin: Plugin,
private treeViewId: string,
private treeDataProvider: TreeDataProvider<T>,
private proxy: TreeViewsMain) {
private proxy: TreeViewsMain,
readonly commandsConverter: CommandsConverter) {

super(() => {
proxy.$unregisterTreeDataProvider(treeViewId);
Expand Down Expand Up @@ -169,6 +174,7 @@ class TreeViewExtImpl<T> extends Disposable {
console.error(`No tree item with id '${parentId}' found.`);
return [];
}
this.disposables.dispose();

// ask data provider for children for cached element
const result = await this.treeDataProvider.getChildren(parent);
Expand Down Expand Up @@ -244,9 +250,6 @@ class TreeViewExtImpl<T> extends Disposable {
}
}

if (treeItem.command) {
treeItem.command.arguments = [id];
}
const treeViewItem = {
id,
label,
Expand All @@ -257,7 +260,7 @@ class TreeViewExtImpl<T> extends Disposable {
tooltip: treeItem.tooltip,
collapsibleState: treeItem.collapsibleState,
contextValue: treeItem.contextValue,
command: treeItem.command
command: treeItem.command ? toInternalCommand(this.commandsConverter.toSafeCommand(treeItem.command, this.disposables)) : undefined
} as TreeViewItem;

treeItems.push(treeViewItem);
Expand Down