Skip to content

lxl66566/anyformatter-vscode

Repository files navigation

anyformatter-vscode

English | 简体中文

VS Code extension for formatting any file using external command.

Usage

  1. Install from marketplace: https://marketplace.visualstudio.com/items?itemName=lxl66566.anyformatter-vscode
  2. Set anyformatter as default formatter for the languages you want:
    "[toml]": {
      "editor.defaultFormatter": "lxl66566.anyformatter-vscode"
    },
    "[nix]": {
      "editor.defaultFormatter": "lxl66566.anyformatter-vscode"
    },
  3. Then: see below ↓↓↓ for all changes, the extension must be restarted to take effect.

stdin and stdout

If the external formatter accepts stdin and outputs to stdout:

"anyformatter": {
  "toml": { "command": "taplo fmt -" },
  "nix": { "command": "nixfmt -" }
},

That's simple.

file

If your external formatter cannot use stdin/stdout, and must be called with a file path:

"anyformatter": {
  "toml": {
    "command": "taplo fmt",
    "file": true,
  }
  "[toml]": {
    "editor.formatOnSave": false  // important!
  },
},

the command will be executed as taplo fmt "<file_path>".

Note that the formatOnSave setting must be set to false, because we need to save first, then format, but the vscode formatOnSave will trigger format first, and then save.

If you still want to format on save, you can use the format_on_save option provided by this extension:

"anyformatter": {
  "toml": {
    "command": "taplo fmt",
    "file": true,
    "format_on_save": true
  }
  "[toml]": {
    "editor.formatOnSave": false
  },
},

So that you can format on save. Now the key("toml") matches the file extension.