visual-studio-codeformatidetask

Can I execute a specified task on "Format Document"?


I am using a custom script to format my code. Currently, whenever I want to format, I have to call Tasks: Run Task and then choose my format task.

Is it possible to tell Format Document to execute my tasks?

This would be nice because it would allow me to do automatic formation on save.

About my VSCode:

Version: 1.44.2
Commit: ff915844119ce9485abfe8aa9076ec76b5300ddd
Date: 2020-04-16T17:50:03.709Z
Electron: 7.1.11
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Linux x64 4.15.0-96-generic snap

Solution

  • I haven't tested it with an actual formatter but you can call tasks from keybindings. try

    {
      "key": "shift+alt+f",
      "command": "-editor.action.formatDocument",
      "when": "editorHasDocumentFormattingProvider && editorHasDocumentFormattingProvider && editorTextFocus && !editorReadonly"
    },
    
    {
      "key": "shift+alt+f",         // or whatever keybinding you wish
      "command": "workbench.action.tasks.runTask",
      "args": "your format task label here"
    },
    

    I used the default Format Document keybinding but you could use whatever keybinding you wish.