Skip to content

Register Command onto the Nodes of Project View

Sheng Chen edited this page Apr 12, 2023 · 12 revisions

Start from version 0.13.0, we refined the context value of each node in the Project view, so other extensions can register their commands onto the nodes if they want to.

Context Value Format

java:<node type>[+<attribute>]*

Available Node Types:

Currently, available node types are:

  • workspaceFolder
  • project
  • container
  • packageRoot
  • package
  • type (class, enum, interface,...)
  • jar
  • file
  • folder

Below picture illustrates the different node types image

Attributes:

For each node type, it may have some attributes. Every attribute will start with a +.

Common Attributes:

  • +uri: The node has uri with file scheme.

Specific Attributes:

Project Node:

  • +java: Java project.
  • +maven: Maven project.
  • +gradle: Gradle project.
  • +unmanagedFolder: Unmanaged folder (project without build tools).

Container Node:

  • +jre: Container for JRE.
  • +maven: Container for Maven.
  • +gradle: Container for Gradle.
  • +referencedLibrary: Container for Referenced Libraries.

Package Root Node:

  • +source: Source package root.
  • +resource: Resource package root.
  • +test: Is on test source path.

Package Node:

  • +source: Source package.
  • +test: Is on test source path.
  • +binary: Binary package (Expanded inside a binary jar).

Type Node

  • +enum: An enum.
  • +interface: An interface.
  • +class: A class.
  • +test: Is on test source path.

Jar Node:

  • +referencedLibrary: Referenced Library jar.

Sorting of groups

Menu items can be sorted into groups. They are sorted in lexicographical order with the following defaults/rules. You can add menu items to these groups or add new groups of menu items in between, below, or above.

The Project explorer context menu has these default groups:

  • 1_new: Commands related to create something, like Java class, package, etc...
  • 6_copypath: Commands related to copying file paths.
  • 7_modification: Commands related to the modification of file.
  • 8_execution: Commands related to execution, like run and debug.
  • 9_configuration: Commands related to configurations, like update project, configure classpath, etc...

image

Usage example:

For example, A node for a Maven project may have the following context value:

java:project+maven+uri

If you want to register a command on that node, you can write your when clause in package.json as:

"contributes": {
  ...
  "menus": {
    ...
    "view/item/context": [
      {
        "command": "...",
        "when": "view == javaProjectExplorer && viewItem =~ /java:project(?=.*?\\b\\+maven\\b)(?=.*?\\b\\+uri\\b)/",
        "group": "...
      }
    ]
  }
}

Note, the example uses positive lookahead (?=) to search the interested attributes, if you want to exclude attributes, you can also use negative lookahead: (?!)

Clone this wiki locally