Home > Blockchain >  Is it possible to use input variables in keybindings in VS Codium?
Is it possible to use input variables in keybindings in VS Codium?

Time:05-08

In Visual Studio Codium I want to define a command that has a variable parameter.

I want the IDE to open specific file, which name is written in another file. Assume I have the following project structure:

/home/user/myproject/
/home/user/myproject/dir1/
/home/user/myproject/dir1/problem1.py
/home/user/myproject/dir1/problem2.py
/home/user/myproject/dir2/problem1.py
...
/home/user/myproject/pointer.txt

The pointer.txt contains path to the file I want to work on. For example, it contains: dir1/problem1.

I have read the documentation here. Now I created the following construction:

keybindings.json:

    {
        "key": "numpad3",
        "command": "htmlRelatedLinks.openFile",
        "args": {
            "file": "${workspaceFolder}/${input:mycatinput}.py",
            "method": "vscode.open",
            "viewColumn": 2,
        }
    },

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "mycat",
            "type": "shell",
            "command": "cat /home/user/myproject/pointer.txt"
        },
    ],
    "inputs": [
        {
          "id": "mycatinput",
          "type": "command",
          "command": "workbench.action.tasks.runTask",
          "args": "mycat"
        }
    ]
}

But when I press numpad3, I get an error notification with text: Unable to open '${input:mycatinput}.py': File not found.

Am I missing something? How do I specify a variable in keybindings.json command, which itself is a result of another command (a shell command, not a vscode command).

CodePudding user response:

In HTML Related Links v0.17.0 is it possible to use a ${command} variable.

Together with the extension Command Variable you can read the file content and use it.

  {
    "key": "numpad3",
    "command": "htmlRelatedLinks.openFile",
    "args": {
      "file": "${workspaceFolder}/${command:mypointer}.py",
      "method": "vscode.open",
      "viewColumn": "2",
      "command": {
        "mypointer": {
          "command": "extension.commandvariable.file.content",
          "args": {
            "fileName": "${workspaceFolder}/pointer.txt"
          }
        }
      }
    }
  }

Command Variable can also read Key-Value files, JSON files, and you can construct a pick list or prompt string, and you can transform the content if needed.

  • Related