Skip to content

pass selected tree item context to view/title commands #42903

Closed

Description

@chrisdias

I would like to have the selected context passed to view commands so that I can act on the selected node in the tree. for example, let's say I want to have a command in the Docker explorer Navigation area (next to where it says "DOCKER" on the sash) that runs the selected image in the tree.

      "view/title": [
        {
          "command": "vscode-docker.explorer.run",
          "when": "view == dockerExplorer",
          "group": "navigation"
        }
      ],

I would register my command:

vscode.commands.registerCommand('vscode-docker.explorer.run', (context?: any, selectedContext?: any[]) => dockerExplorerProvider.run(context, selectedContext));

And then in my command I can get the selected node(s) from the tree

build(context?: any): void {
   if (selectedContext.length >0) {
     // work on multi selected images
   }
  ...
   if (context) {
      // work on selected image
   } else {
      // pop a quick pick to list images
   }
   // ...
}

Activity

  1. added
    feature-request
    tree-views
    on Feb 5, 2018
  2. eamodio commented on Apr 30, 2018

    @eamodio
    Contributor

    I would expand on this to also provide the treeview instance as well as the selected node. I have some generic commands (refresh, close, etc) that I use across a few views, and right now I am required to create new commands for each view (causing a command explosion in package.json and a maintenance issue). It the view was also provided all of that duplication could be eliminated.

    I would also love if the treeview was also returned on itself even when executing a command on a node (so that way the node doesn't have to carry around its parent if access is needed).

    Overall I keep coming back to this request: #25716 having more/structured context for how commands were invoked makes them much easier to deal with

  3. d-akara commented on May 5, 2018

    @d-akara

    Eric Amodio (@eamodio) Could you do something like this? I use an internal command to proxy to functions I want to invoke so I don't have to register every action as a command.

    Here is an example snippet of something I use...

    let disposable = vscode.commands.registerCommand('dakara-internal.oncommand', (onCommand:Function, ...params) => onCommand.apply(this, params))
    
    function showLine(lineNumber:number) {
        vscode.commands.executeCommand("revealLine", {lineNumber})
    }
    
    export function makeTreeItemFromSelection(line: vscode.TextLine) {
        return {
            label:  (line.lineNumber + 1) + ` ${Glyph.TRI_DOT_VERTICAL} ` + line.text,
            command: {title: 'reveal', command: 'dakara-internal.oncommand', arguments: [showLine, line.lineNumber]},
        }
    }```
  4. 28 remaining items

  5. alexr00 commented on Sep 21, 2022

    @alexr00
    Member

    Great to hear! Closing this issue so that it gets added to our testing for September.

  6. benibenj commented on Sep 27, 2022

    @benibenj
    Contributor

    Verifying:

    1. Register a view/title command (takes 2 arguments) in a panel which has a tree view
    2. set breakpoint in command function
    3. Select tree item(s) and run command from view/title
    4. First argument should always be the focused tree item (type T).
      Second argument should be (all) selected tree items as long as the focused tree item is one of the selected tree items.

    The arguments should behave the same as for commands in the context menu.

  7. irvnriir commented on Sep 28, 2022

    @irvnriir

    How to get them ?

    doesn't work:

    		let test = sy.vscode.commands.registerCommand('Extension_test.test', () => {
    			sy.console.log(this.selectedTreeItems)		// `undefined`
    		});
    		sy.ExtensionDisposables.push(test);
    
  8. gjsjohnmurray commented on Sep 28, 2022

    @gjsjohnmurray
    Contributor

    irvnriir are you using Insiders for this? If not you'll need to wait until 1.72 ships, probably by the end of next week,

  9. added
    verified
    on Sep 29, 2022
  10. roblourens commented on Sep 29, 2022

    @roblourens
    Member

    Am I doing it wrong?

    	context.subscriptions.push(vscode.commands.registerCommand('extension.helloWorld', async (arg1, arg2) => {
    		console.log(arg1, arg2);
    	}));
    

    All I get is undefined undefined. Registered it in all view/title, and clicked it everywhere I see it, for example, Explorer and Outline

  11. added
    verification-steps-needed
    and removed
    verified
    on Sep 29, 2022
  12. benibenj commented on Sep 29, 2022

    @benibenj
    Contributor

    Make sure you register the view/title command to a contributed TreeView (for example in Sample Tree Views extension or Pull Requests extension).
    Then select an element in the tree (or multiple if multiselect) and press the action. The arguments should then print the focused item and the selected items

  13. removed
    verification-steps-needed
    on Sep 29, 2022
  14. roblourens commented on Sep 29, 2022

    @roblourens
    Member

    Oh, I see it, thanks

  15. added
    verified
    on Sep 29, 2022
  16. locked and limited conversation to collaborators on Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

apiapi-finalizationapi-proposalfeature-requesttree-viewsverification-neededverified

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions