How to set file/folder selection in workspace

Hi All,

Is there a way, I can select a folder in the workspace programmatically

I tried this.selectionService.selection=new URL(/file/path/in/workspace);

But this didn’t highlight the tree node representing the folder in the workspace navigator view.

regards,
Abhishek

@chakraborty-slx thank you for the discussion, you can use the tree-model to select a node:

You can look at how it is used across the framework, most notably the navigator/explorer.

1 Like

I have the following class,

import {SelectableTreeNode, TreeModel} from ‘@theia/core/lib/browser’;


@injectable()
export class HomeViewService {
@inject(TreeModel) readonly model: TreeModel;
@inject(BackendService)
private readonly backendService: BackendService;
@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;

async createNewProject(projectMessage: ProjectMessage) {

    //create new project
    await this.backendService.createNewProject(projectMessage);
    const projectURI = new URI(projectMessage.destination + Path.separator + projectMessage.name);
  


    //select the project in navigator
    const targetNode = this.model.getNode(projectURI.path.toString());
    if (targetNode && SelectableTreeNode.is(targetNode)) {
        this.model.selectNode(targetNode);
    }

}

}

I am getting the following error:

planner.ts:113 Uncaught (in promise) Error: No matching bindings found for serviceIdentifier: Symbol(TreeModel)
at _validateActiveBindingCount (planner.ts:113)
at _getActiveBindings (planner.ts:91)
at _createSubRequests (planner.ts:163)
at planner.ts:197
at Array.forEach ()
at planner.ts:196
at Array.forEach ()
at _createSubRequests (planner.ts:167)
at planner.ts:197
at Array.forEach ()

Do i need to add something like this

     child.unbind(FileTreeModel);
    child.bind(FileNavigatorModel).toSelf();
    child.rebind(TreeModel).toService(FileNavigatorModel);

@chakraborty-slx it looks like an error with your injections, from what I can tell the tree-model is only accessible in trees (constructor).

Thanks @vince-fugnitto, i think you are right, but is there any other way to do selection on FileNavigatorWidget, from outside the view?

@chakraborty-slx you can get an instance of the FileNavigatorTree (ex: through the ApplicationShell), and will have access to the tree’s model (from which you can perform selections).

The following is an example on how I was able to select the first node in a standard workspace (for a simple POC):

import { injectable, inject } from '@theia/core/shared/inversify';
import { CommandContribution, CommandRegistry } from '@theia/core/lib/common';
import { ApplicationShell } from '@theia/core/lib/browser/shell/application-shell';
import { FileNavigatorWidget, FILE_NAVIGATOR_ID } from '@theia/navigator/lib/browser/navigator-widget';
import { SelectableTreeNode } from '@theia/core/lib/browser';
import { WorkspaceNode } from '@theia/navigator/lib/browser/navigator-tree';

export const SelectNavigatorCommand = {
    id: 'navigator.select-node',
    category: 'Navigator',
    label: 'Select First Child'
};

@injectable()
export class TheiaSelectNavCommandContribution implements CommandContribution {

    @inject(ApplicationShell)
    protected readonly shell: ApplicationShell;

    registerCommands(registry: CommandRegistry): void {
        registry.registerCommand(SelectNavigatorCommand, {
            execute: () => {
                const widget = this.shell.getWidgetById(FILE_NAVIGATOR_ID) as FileNavigatorWidget | undefined;
                if (!widget) {
                    return;
                }

                if (WorkspaceNode.is(widget.model.root)) {
                    const rootNodes = widget.model.root.children;
                    for (const root of rootNodes) {
                        const children = root.children;
                        if (SelectableTreeNode.is(children[0])) {
                            widget.model.selectNode(children[0]);
                            return;
                        }
                    }
                }
            }
        });
    }
}
1 Like

Thanks, a lot @vince-fugnitto this solution works.