1

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).

Ashark
  • 643
  • 7
  • 16
  • `${input}` is not yet part of the variables supported by `htmlRelatedLinks.openFile` I will add an alternative variable because it is unknown what called the command. – rioV8 May 07 '22 at 06:28
  • That `${input:mycatinput}` is actually resolved by the command (extension) and not by the vscode itself before passing to command (extension)? – Ashark May 07 '22 at 07:17
  • I have not published `HTML Related Links` to VSCodium, is it available there – rioV8 May 07 '22 at 07:55
  • It is currently not available in Open VSX registry, but with the help of `vscodium-marketplace` package (see [here](https://wiki.archlinux.org/title/Visual_Studio_Code#Extensions_support)), I can install it from MS VSCode Marketplace. – Ashark May 07 '22 at 08:12

1 Answers1

1

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.

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • Thanks, you are awesome! There is a problem however. In the `fileName` field of the command, the `${workspaceFolder}` is not expanded, resulting that I have error `Unable to open 'Unknown.py': File not found`. But there is no such error when I write absolute path like `/home/user/myproject/pointer.txt`. – Ashark May 07 '22 at 08:38
  • @Ashark Fixed an issue in Command Variable, try v1.34.2 – rioV8 May 07 '22 at 10:37
  • With Command Variable v1.34.2 it worked! Thank you! – Ashark May 07 '22 at 10:42
  • Can you please take a look: https://stackoverflow.com/questions/72153504/prevent-asynchronous-order-of-command-sequence-execution-in-vs-codium – Ashark May 07 '22 at 14:39