Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I configure a task.json file for more then one language in vs code?

I want to configure a tasks.json file in VS Code to run python and java code just pressing:

  • Ctrl + Shift + B

Python and Java are configured but it needs two different tasks.json files.

But I can just keep one tasks.json file in the .vscode folder.

How can I merge two config file in a tasks.json file ?

For Python:

{
  "version": "2.0.0",
  "tasks": [{
    "label": "Compile and run",
    "type": "shell",
    "command": "",
    "args": [
      "/usr/bin/time",
      "-v",
      "--output",
      "sys.txt",
      "timeout",
      "5",
      "python3",
      "${relativeFile}",
      "<",
      "input.txt",
      ">",
      "output.txt",
    ],
    "group": {
      "kind": "build",
      "isDefault": true
    },
    "problemMatcher": {
      "owner": "py",
      "fileLocation": [
        "relative",
        "${workspaceRoot}"
      ],
      "pattern": {
        "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
        "file": 1,
        "line": 2,
        "column": 3,
        "severity": 4,
        "message": 5
      }
    }
  }],


}

For Java :

{
  "version": "2.0.0",
  "tasks": [{
    "label": "Compile and run",
    "type": "shell",
    "command": "",
    "args": [
      "/usr/bin/time",
      "-v",
      "--output",
      "sys.txt",
      "timeout",
      "5",
      "java",
      "${relativeFile}",
      "<",
      "input.txt",
      ">",
      "output.txt",
    ],
    "group": {
      "kind": "build",
      "isDefault": true
    },
    "problemMatcher": {
      "owner": "java",
      "fileLocation": [
        "relative",
        "${workspaceRoot}"
      ],
      "pattern": {
        "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
        "file": 1,
        "line": 2,
        "column": 3,
        "severity": 4,
        "message": 5
      }
    }
  }],
}
like image 556
ShifaT Avatar asked Jun 25 '20 00:06

ShifaT


1 Answers

If you have the java or python file open (and the two tasks "merged" as @tHeSID suggested), you can just overload the keybindings ala:

  {
    "key": "ctrl+shift+B",
    "command": "workbench.action.tasks.runTask",
    "args": "Compile and run Python",
    "when": "editorLangId == python"
  },
  {
    "key": "ctrl+shift+B",
    "command": "workbench.action.tasks.runTask",
    "args": "Compile and run Java",
    "when": "editorLangId == java"
  },
like image 89
Mark Avatar answered Sep 22 '22 14:09

Mark