Visual Studio Code variable references

 

Visual Studio Code supports variable substitution in debugging and task configuration files and some selection settings. Use the ${variableName} syntax to perform variable substitution in the key and value strings in the launch.json and tasks.json files.

 

Preset variables

The supported variables are:

  • ${workspaceFolder}  -The top-level path (root folder) of the folder opened in VS Code
  • ${workspaceFolderBasename}  -The name of the folder opened in VS Code, excluding "/"
  • ${file}  -The name of the currently opened file (absolute path + main name + extension)
  • ${relativeFile}-relative  file name (folder name + "/" + full file name)
  • ${relativeFileDirname}-the  folder name of the current file (only the name, no path)
  • ${fileBasename}-the  full name of the current file (main name + extension)
  • ${fileBasenameNoExtension}-the  main name of the current file (without extension)
  • ${fileDirname}-the  absolute path of the file, excluding the file name and the path separator at the end
  • ${fileExtname}-the  extension of the currently opened file
  • ${cwd} - the task runner's current working directory on startup
  • ${lineNumber}-the  line number of the code at the cursor position
  • ${selectedText}-the selected  content in the currently edited file
  • ${execPath}  -Path to Code.exe
  • ${defaultBuildTask}-the  name of the default build task

Example of preset variables

 

Assume the following situation:

  1.  你在编辑器中打开了文件,文件路径为:/home/your-username/your-project/folder/file.ext;
  2.  Root file your workspace folder as follows:  /home/your-username/your-project.

Then the value of each preset variable is

  • ${workspaceFolder} - /home/your-username/your-project
  • ${workspaceFolderBasename} - your-project
  • ${file} - /home/your-username/your-project/folder/file.ext
  • ${relativeFile} - folder/file.ext
  • ${relativeFileDirname} - folder
  • ${fileBasename} - file.ext
  • ${fileBasenameNoExtension} - file
  • ${fileDirname} - /home/your-username/your-project/folder
  • ${fileExtname} - .ext
  • ${lineNumber} - line number of the cursor
  • ${selectedText} - text selected in your code editor
  • ${execPath} - location of Code.exe
提示:将鼠标放到tasks.json和launch.json的字符串上时,使用IntelliSense可以获得预定义变量的提示信息。

Variable scope of each workspace folder

Environment variable

You can also refer to the environment variables of the operating system through the ${ env:Name} syntax, such as:

{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "program": "${workspaceFolder}/app.js",
  "cwd": "${workspaceFolder}",
  "args": ["${env:USERNAME}"]
}

Introduce configuration file variables

You can  introduce variables in the configuration file through the   ${config:Name} syntax. For example:${config:editor.fontSize}。

Command variable

    If the above preset variables cannot meet your needs, you can also use VS Code commands like variables, syntax: ${command:commandID}  .

    When a command variable is inserted, the command will be executed and the variable will be replaced with the result of the command (type is string). Commands can be simple calculations without UI, or some complex functions with UI features provided by the extended API based on VS code.

An example for this functionality can be found in VS Code's Node.js debugger extension which provides an interactive command extension.pickNodeProcess for selecting a single process from the list of all running Node.js processes. The command returns the process ID of the selected process. This makes it possible to use the extension.pickNodeProcess command in an Attach by Process ID launch configuration in the following way:

    You can find a similar example in the Node.js debugger extension of VS Code, which provides an interactive command extension. Select the pickNodeProcess of a single process from the list of all running Node.js processes. This command returns the process ID of the selected process. This makes it possible to use the "pickNodeProcess command in the additional startup configuration by process ID". ,Methods as below:

{
  "configurations": [
    {
      "type": "node",
      "request": "attach",
      "name": "Attach by Process ID",
      "processId": "${command:extension.pickNodeProcess}"
    }
  ]
}

Input variable

    Command variables are already very powerful, but they lack a mechanism to run specific commands. For example, you cannot pass prompt information or default values ​​to the command prompt.

This limitation can be solved by using input variables with the syntax ${input:variableID}. The variableID will be associated with the entries in the inputs section of the launch.json and tasks.json files, and its other properties can be configured in the input section.

The following example shows the general structure of task.json using input variables:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "task name",
      //  input变量
      "command": "${input:variableID}"
      // ...
    }
  ],
  "inputs": [  //  input部分
    {   
      // 通过 id 属性与其他部分关联 
      "id": "variableID",  

      //输入变量的类型,目前支持 promptString,pickString,command
      "type": "type of input variable"        
    }
  ]
}

Input variable types currently supported by VS Code:

  • promptString : Display an input box to receive user input.
  • pickString:  Display a drop-down list for users to choose from several options.
  • command:  run any command

Each input variable type requires additional configuration attributes:

promptString:

  • description:  The input prompt information displayed.
  • default: The  specified default value, if the user does not input, this default value will be used.

pickString:

  • description:  The input prompt information displayed.
  • options:  an array of options.
  • default: The  specified default value. If the user does not input, this default value will be used. The default value must be one of the options.

command:

  • command:  The command to run on variable interpolation.
  • args:  The parameters of the running command.

The following is an  tasks.json example, which demonstrates how inputs to use Angular CLI through  variables:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "ng g",
      "type": "shell",
      "command": "ng",
      "args": ["g", "${input:componentType}", "${input:componentName}"]
    }
  ],
  "inputs": [
    {
      "type": "pickString",
      "id": "componentType",
      "description": "What type of component do you want to create?",
      "options": [
        "component",
        "directive",
        "pipe",
        "service",
        "class",
        "guard",
        "interface",
        "enum",
        "enum"
      ],
      "default": "component"
    },
    {
      "type": "promptString",
      "id": "componentName",
      "description": "Name your component.",
      "default": "my-new-component"
    }
  ]
}

    The following example demonstrates how to use the command type input variable in the debugging configuration. The configuration generates a list of all test cases from a specific folder, and then allows the user to select a test case. Suppose an extension provides the extension.mochaSupport.test picker command, which saves all test cases in one location and displays a picker UI for the language to select one of them. The input parameters of the command are defined by the command itself.

{
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Run specific test",
      "program": "${workspaceFolder}/${input:pickTest}"
    }
  ],
  "inputs": [
    {
      "id": "pickTest",
      "type": "command",
      "command": "extension.mochaSupport.testPicker",
      "args": {
        "testFolder": "/out/tests"
      }
    }
  ]
}

    Command input can also be used in tasks. In the following example, the built-in Terminate Task command is used. It can accept a parameter to terminate all tasks.

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Terminate All Tasks",
      "command": "echo ${input:terminate}",
      "type": "shell",
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "id": "terminate",
      "type": "command",
      "command": "workbench.action.tasks.terminate",
      "args": "terminateAll"
    }
  ]
}

How to test the value of a variable?

    The easiest way to know the value of a variable at runtime is to enter the value of the variable in the console. For example: if you want to know  ${workspaceFolder}的值是什么, you can create a tasks.json file like the following, enter "echo ${workspaceFolder}" in the command value of the file, and then select Terminal  >  Run Task in the menu to  run the  tasks.json file

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "echo",
      "type": "shell",
      "command": "echo ${workspaceFolder}"
    }
  ]
}

 Translated from: https://code.visualstudio.com/docs/editor/variables-reference

Guess you like

Origin blog.csdn.net/hn_tzy/article/details/104652114