dprint / dprint-plugin-exec

Formatting plugin for dprint that formats code via other formatting CLI tools.
MIT License
21 stars 1 forks source link

dprint-plugin-exec

Plugin that formats code via mostly any formatting CLI found on the host machine.

This plugin executes CLI commands to format code via stdin (recommended) or via a file path.

Install

  1. Install dprint
  2. Follow instructions at https://github.com/dprint/dprint-plugin-exec/releases/

Configuration

  1. Add general configuration if desired (shown below).
  2. Add binaries similar to what's shown below and specify what file extensions they match via a exts property.
{
  // ...etc...
  "exec": {
    // lets the plugin know the cwd, see https://dprint.dev/config/#configuration-variables
    "cwd": "${configDir}",

    // general config (optional -- shown are the defaults)
    "lineWidth": 120,
    "indentWidth": 2,
    "useTabs": false,
    "cacheKey": "1",
    "timeout": 30,

    // now define your commands, for example...
    "commands": [{
      "command": "rustfmt",
      "exts": ["rs"]
    }, {
      "command": "java -jar formatter.jar {{file_path}}",
      "exts": ["java"]
    }, {
      "command": "yapf",
      "exts": ["py"]
    }]
  },
  "plugins": [
    // run `dprint config add exec` to add the latest exec plugin's url here
  ]
}

General config:

Command config:

Command templates (ex. see the prettier example above):

Example - yapf

{
  // ...etc...
  "exec": {
    "cwd": "${configDir}",
    "commands": [{
      "command": "yapf",
      "exts": ["py"]
    }]
  },
  "plugins": [
    // run `dprint config add exec` to add the latest exec plugin's url here
  ]
}

Example - java

{
  // ...etc...
  "exec": {
    "cwd": "${configDir}",
    "commands": [{
      "command": "java -jar formatter.jar {{file_path}}",
      "exts": ["java"]
    }]
  },
  "plugins": [
    // run `dprint config add exec` to add the latest exec plugin's url here
  ]
}

Example - rustfmt

Use the rustfmt binary so you can format stdin.

{
  // ...etc...
  "exec": {
    "cwd": "${configDir}",
    "commands": [{
      "command": "rustfmt --edition 2021",
      "exts": ["rs"]
    }]
  },
  "plugins": [
    // run `dprint config add exec` to add the latest exec plugin's url here
  ]
}

Example - prettier

Consider using dprint-plugin-prettier instead as it will be much faster.

{
  // ...etc...
  "exec": {
    "cwd": "${configDir}",
    "commands": [{
      "command": "prettier --stdin-filepath {{file_path}} --tab-width {{indent_width}} --print-width {{line_width}}",
      // add more extensions that prettier should format
      "exts": ["js", "ts", "html"]
    }]
  },
  "plugins": [
    // run `dprint config add exec` to add the latest exec plugin's url here
  ]
}