microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.59k stars 29.03k forks source link

contextValue for TreeItem support string and object #46236

Closed axetroy closed 6 years ago

axetroy commented 6 years ago

I some case, I need to custom when the context for treeItem should show.

Here are 2 contexts for star and unstar

when star project, star context hide, unstar context show when unstar project, unstar context hide, star context show

Here is what I define in view/item/context

               {
                    "command": "gpm.unstarProject",
                    "group": "context",
                    "when": "viewItem.type == project && viewItem.stared == true"
                },
                {
                    "command": "gpm.starProject",
                    "group": "context",
                    "when": "viewItem.type == project && viewItem.stared == false"
                }

But unfortunately, contextValue?: string; not support object

sandy081 commented 6 years ago

@axetroy contextValue on TreeNode does not refers to the menu location view/item/context

/**
         * Context value of the tree item. This can be used to contribute item specific actions in the tree.
         * For example, a tree item is given a context value as `folder`. When contributing actions to `view/item/context`
         * using `menus` extension point, you can specify context value for key `viewItem` in `when` expression like `viewItem == folder`.
         * ```
         *  "contributes": {
         *      "menus": {
         *          "view/item/context": [
         *              {
         *                  "command": "extension.deleteFolder",
         *                  "when": "viewItem == folder"
         *              }
         *          ]
         *      }
         *  }
         * ```
         * This will show action `extension.deleteFolder` only for items with `contextValue` is `folder`.
         */
        contextValue?: string;

As mentioned in the documentation, contextValue is the value your provide in the when property. In the above example folder is the contextValue

axetroy commented 6 years ago

@sandy081

contextValue is the value your provide in the when property. In the above example folder is the contextValue

I knew it. but contextValue is only accept string type. isn't it?

How about support an object?

sandy081 commented 6 years ago

contextValue is always a string and is used internally and in extensions.

What is the purpose of supporting an object?

axetroy commented 6 years ago

@sandy081

What is the purpose of supporting an object?

For more complicated conditions, control the command/context when to show.

and support an object, should be more scalable

sandy081 commented 6 years ago

@axetroy Can you please be more specific about complicated conditions? Currently and, not and regex conditions are supported.