From f7e55b93a0b111159ee023e9bdbba59c3bd99bc6 Mon Sep 17 00:00:00 2001 From: Vladyslav Zhukovskyi Date: Mon, 20 May 2019 12:01:19 +0300 Subject: [PATCH] Add ability to match browser displayed node with the plugin created node Signed-off-by: Vladyslav Zhukovskyi --- .../src/hosted/browser/worker/worker-main.ts | 5 +++- .../src/hosted/node/plugin-host-rpc.ts | 5 +++- .../plugin-ext/src/plugin/command-registry.ts | 5 ++-- .../plugin-ext/src/plugin/plugin-context.ts | 8 ++++-- .../src/plugin/selection-provider-ext.ts | 28 +++++++++++++++++++ .../plugin-ext/src/plugin/tree/tree-views.ts | 14 +++++++--- 6 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 packages/plugin-ext/src/plugin/selection-provider-ext.ts diff --git a/packages/plugin-ext/src/hosted/browser/worker/worker-main.ts b/packages/plugin-ext/src/hosted/browser/worker/worker-main.ts index d11ef144db951..5d510edc78fcd 100644 --- a/packages/plugin-ext/src/hosted/browser/worker/worker-main.ts +++ b/packages/plugin-ext/src/hosted/browser/worker/worker-main.ts @@ -28,6 +28,7 @@ import { EditorsAndDocumentsExtImpl } from '../../../plugin/editors-and-document import { WorkspaceExtImpl } from '../../../plugin/workspace'; import { MessageRegistryExt } from '../../../plugin/message-registry'; import { WorkerEnvExtImpl } from './worker-env-ext'; +import { SelectionServiceExt } from '../../../plugin/selection-provider-ext'; // tslint:disable-next-line:no-any const ctx = self as any; @@ -55,6 +56,7 @@ const messageRegistryExt = new MessageRegistryExt(rpc); const workspaceExt = new WorkspaceExtImpl(rpc, editorsAndDocuments, messageRegistryExt); const preferenceRegistryExt = new PreferenceRegistryExtImpl(rpc, workspaceExt); const debugExt = createDebugExtStub(rpc); +const selectionServiceExt = new SelectionServiceExt(); const pluginManager = new PluginManagerExtImpl({ // tslint:disable-next-line:no-any @@ -133,7 +135,8 @@ const apiFactory = createAPIFactory( preferenceRegistryExt, editorsAndDocuments, workspaceExt, - messageRegistryExt + messageRegistryExt, + selectionServiceExt ); let defaultApi: typeof theia; diff --git a/packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts b/packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts index ac237ce0b2405..c04ef3a8c53ab 100644 --- a/packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts +++ b/packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts @@ -26,6 +26,7 @@ import { EditorsAndDocumentsExtImpl } from '../../plugin/editors-and-documents'; import { WorkspaceExtImpl } from '../../plugin/workspace'; import { MessageRegistryExt } from '../../plugin/message-registry'; import { EnvNodeExtImpl } from '../../plugin/node/env-node-ext'; +import { SelectionServiceExt } from '../../plugin/selection-provider-ext'; /** * Handle the RPC calls. @@ -47,6 +48,7 @@ export class PluginHostRPC { const messageRegistryExt = new MessageRegistryExt(this.rpc); const workspaceExt = new WorkspaceExtImpl(this.rpc, editorsAndDocumentsExt, messageRegistryExt); const preferenceRegistryExt = new PreferenceRegistryExtImpl(this.rpc, workspaceExt); + const selectionServiceExt = new SelectionServiceExt(); this.pluginManager = this.createPluginManager(envExt, preferenceRegistryExt, this.rpc); this.rpc.set(MAIN_RPC_CONTEXT.HOSTED_PLUGIN_MANAGER_EXT, this.pluginManager); this.rpc.set(MAIN_RPC_CONTEXT.EDITORS_AND_DOCUMENTS_EXT, editorsAndDocumentsExt); @@ -61,7 +63,8 @@ export class PluginHostRPC { preferenceRegistryExt, editorsAndDocumentsExt, workspaceExt, - messageRegistryExt + messageRegistryExt, + selectionServiceExt ); } diff --git a/packages/plugin-ext/src/plugin/command-registry.ts b/packages/plugin-ext/src/plugin/command-registry.ts index 8467f4883e2bb..73e2fa69ce2c7 100644 --- a/packages/plugin-ext/src/plugin/command-registry.ts +++ b/packages/plugin-ext/src/plugin/command-registry.ts @@ -19,6 +19,7 @@ import { CommandRegistryExt, PLUGIN_RPC_CONTEXT as Ext, CommandRegistryMain } fr import { RPCProtocol } from '../api/rpc-protocol'; import { Disposable } from './types-impl'; import { KnownCommands } from './type-converters'; +import { SelectionServiceExt } from './selection-provider-ext'; // tslint:disable-next-line:no-any export type Handler = (...args: any[]) => T | PromiseLike; @@ -29,7 +30,7 @@ export class CommandRegistryImpl implements CommandRegistryExt { private readonly commands = new Set(); private readonly handlers = new Map(); - constructor(rpc: RPCProtocol) { + constructor(rpc: RPCProtocol, private selectionService: SelectionServiceExt) { this.proxy = rpc.getProxy(Ext.COMMAND_REGISTRY_MAIN); } @@ -98,7 +99,7 @@ export class CommandRegistryImpl implements CommandRegistryExt { private executeLocalCommand(id: string, ...args: any[]): PromiseLike { const handler = this.handlers.get(id); if (handler) { - return Promise.resolve(handler(...args)); + return Promise.resolve(this.selectionService.selection !== undefined ? handler(this.selectionService.selection) : handler(...args)); } else { return Promise.reject(new Error(`Command ${id} doesn't exist`)); } diff --git a/packages/plugin-ext/src/plugin/plugin-context.ts b/packages/plugin-ext/src/plugin/plugin-context.ts index 4becec41284e7..68a38a8885d25 100644 --- a/packages/plugin-ext/src/plugin/plugin-context.ts +++ b/packages/plugin-ext/src/plugin/plugin-context.ts @@ -124,6 +124,7 @@ import { TasksExtImpl } from './tasks/tasks'; import { DebugExtImpl } from './node/debug/debug'; import { FileSystemExtImpl } from './file-system'; import { QuickPick, QuickPickItem } from '@theia/plugin'; +import { SelectionServiceExt } from './selection-provider-ext'; export function createAPIFactory( rpc: RPCProtocol, @@ -133,10 +134,11 @@ export function createAPIFactory( preferenceRegistryExt: PreferenceRegistryExtImpl, editorsAndDocumentsExt: EditorsAndDocumentsExtImpl, workspaceExt: WorkspaceExtImpl, - messageRegistryExt: MessageRegistryExt + messageRegistryExt: MessageRegistryExt, + selectionServiceExt: SelectionServiceExt ): PluginAPIFactory { - const commandRegistry = rpc.set(MAIN_RPC_CONTEXT.COMMAND_REGISTRY_EXT, new CommandRegistryImpl(rpc)); + const commandRegistry = rpc.set(MAIN_RPC_CONTEXT.COMMAND_REGISTRY_EXT, new CommandRegistryImpl(rpc, selectionServiceExt)); const quickOpenExt = rpc.set(MAIN_RPC_CONTEXT.QUICK_OPEN_EXT, new QuickOpenExtImpl(rpc)); const dialogsExt = new DialogsExtImpl(rpc); const windowStateExt = rpc.set(MAIN_RPC_CONTEXT.WINDOW_STATE_EXT, new WindowStateExtImpl()); @@ -148,7 +150,7 @@ export function createAPIFactory( const terminalExt = rpc.set(MAIN_RPC_CONTEXT.TERMINAL_EXT, new TerminalServiceExtImpl(rpc)); const outputChannelRegistryExt = new OutputChannelRegistryExt(rpc); const languagesExt = rpc.set(MAIN_RPC_CONTEXT.LANGUAGES_EXT, new LanguagesExtImpl(rpc, documents)); - const treeViewsExt = rpc.set(MAIN_RPC_CONTEXT.TREE_VIEWS_EXT, new TreeViewsExtImpl(rpc, commandRegistry)); + const treeViewsExt = rpc.set(MAIN_RPC_CONTEXT.TREE_VIEWS_EXT, new TreeViewsExtImpl(rpc, commandRegistry, selectionServiceExt)); const webviewExt = rpc.set(MAIN_RPC_CONTEXT.WEBVIEWS_EXT, new WebviewsExtImpl(rpc)); const tasksExt = rpc.set(MAIN_RPC_CONTEXT.TASKS_EXT, new TasksExtImpl(rpc)); const connectionExt = rpc.set(MAIN_RPC_CONTEXT.CONNECTION_EXT, new ConnectionExtImpl(rpc)); diff --git a/packages/plugin-ext/src/plugin/selection-provider-ext.ts b/packages/plugin-ext/src/plugin/selection-provider-ext.ts new file mode 100644 index 0000000000000..4781a4c0febd8 --- /dev/null +++ b/packages/plugin-ext/src/plugin/selection-provider-ext.ts @@ -0,0 +1,28 @@ +/******************************************************************************** + * Copyright (C) 2019 Red Hat, Inc. and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the Eclipse + * Public License v. 2.0 are satisfied: GNU General Public License, version 2 + * with the GNU Classpath Exception which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + ********************************************************************************/ + +export class SelectionServiceExt { + + private currentSelection: Object | undefined; + + get selection(): Object | undefined { + return this.currentSelection; + } + + set selection(selection: Object | undefined) { + this.currentSelection = selection; + } +} diff --git a/packages/plugin-ext/src/plugin/tree/tree-views.ts b/packages/plugin-ext/src/plugin/tree/tree-views.ts index 30c0c49bcafa6..726fd39acd11b 100644 --- a/packages/plugin-ext/src/plugin/tree/tree-views.ts +++ b/packages/plugin-ext/src/plugin/tree/tree-views.ts @@ -25,6 +25,7 @@ import { Plugin, PLUGIN_RPC_CONTEXT, TreeViewsExt, TreeViewsMain, TreeViewItem } import { RPCProtocol } from '../../api/rpc-protocol'; import { CommandRegistryImpl } from '../command-registry'; import { PluginPackage } from '../../common'; +import { SelectionServiceExt } from '../selection-provider-ext'; export class TreeViewsExtImpl implements TreeViewsExt { @@ -32,7 +33,7 @@ export class TreeViewsExtImpl implements TreeViewsExt { private treeViews: Map> = new Map>(); - constructor(rpc: RPCProtocol, private commandRegistry: CommandRegistryImpl) { + constructor(rpc: RPCProtocol, private commandRegistry: CommandRegistryImpl, private selectionService: SelectionServiceExt) { this.proxy = rpc.getProxy(PLUGIN_RPC_CONTEXT.TREE_VIEWS_MAIN); } @@ -50,7 +51,7 @@ export class TreeViewsExtImpl implements TreeViewsExt { throw new Error('Options with treeDataProvider is mandatory'); } - const treeView = new TreeViewExtImpl(plugin, treeViewId, options.treeDataProvider, this.proxy, this.commandRegistry); + const treeView = new TreeViewExtImpl(plugin, treeViewId, options.treeDataProvider, this.proxy, this.commandRegistry, this.selectionService); this.treeViews.set(treeViewId, treeView); return { @@ -129,7 +130,8 @@ class TreeViewExtImpl extends Disposable { private treeViewId: string, private treeDataProvider: TreeDataProvider, private proxy: TreeViewsMain, - private commandRegistry: CommandRegistryImpl) { + private commandRegistry: CommandRegistryImpl, + private selectionService: SelectionServiceExt) { super(() => { this.dispose(); @@ -288,6 +290,8 @@ class TreeViewExtImpl extends Disposable { // get element from a cache const cachedElement: T | undefined = this.cache.get(treeItemId); + this.selectionService.selection = undefined; + if (cachedElement) { this.selection = [cachedElement]; @@ -295,9 +299,11 @@ class TreeViewExtImpl extends Disposable { const treeItem = await this.treeDataProvider.getTreeItem(cachedElement); if (!contextSelection && treeItem.command) { - this.commandRegistry.executeCommand((treeItem.command.command || treeItem.command.id)!, ...(treeItem.command.arguments || [])); + await this.commandRegistry.executeCommand((treeItem.command.command || treeItem.command.id)!, ...(treeItem.command.arguments || [])); } } + + this.selectionService.selection = cachedElement; } }