formatters.mdx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ---
  2. title: "Formatters"
  3. description: ""
  4. ---
  5. OpenCode V2 accepts formatter configuration, but it does not yet include a
  6. formatter runtime. File writes and edits are not automatically formatted.
  7. <Warning>
  8. V2 currently has no built-in formatters. The built-in formatter list and
  9. automatic post-edit formatting documented for V1 do not apply to V2.
  10. </Warning>
  11. ## Configuration
  12. The `formatter` field accepts a boolean or an object keyed by formatter name:
  13. ```jsonc title="opencode.jsonc"
  14. {
  15. "$schema": "https://opencode.ai/config.json",
  16. "formatter": {
  17. "prettier": {
  18. "disabled": false,
  19. "command": ["prettier", "--write", "$FILE"],
  20. "environment": {
  21. "NODE_ENV": "development"
  22. },
  23. "extensions": [".js", ".jsx", ".ts", ".tsx"]
  24. }
  25. }
  26. }
  27. ```
  28. This example is valid V2 configuration, but V2 does not currently execute the
  29. command.
  30. Each named formatter entry supports these optional fields:
  31. | Field | Type | Current V2 behavior |
  32. | --- | --- | --- |
  33. | `disabled` | `boolean` | Accepted, but there is no runtime formatter to enable or disable. |
  34. | `command` | `string[]` | Accepted as an argument array, but not executed. |
  35. | `environment` | `Record<string, string>` | Accepts string environment variable names and values, but they are not applied. |
  36. | `extensions` | `string[]` | Accepted without extension-specific validation, but files are not matched against it. |
  37. All entry fields are optional. The schema therefore also accepts an empty entry
  38. such as `"prettier": {}`.
  39. ## Enable and disable
  40. The schema accepts all of the following forms:
  41. ```jsonc
  42. // Omit `formatter`, or use false, when formatting is not requested.
  43. {
  44. "formatter": false
  45. }
  46. ```
  47. ```jsonc
  48. // Reserved for enabling all built-ins once a V2 runtime provides them.
  49. {
  50. "formatter": true
  51. }
  52. ```
  53. ```jsonc
  54. // Configure named entries or mark one as disabled.
  55. {
  56. "formatter": {
  57. "prettier": { "disabled": true },
  58. "custom": {
  59. "command": ["custom-fmt", "$FILE"],
  60. "extensions": [".foo"]
  61. }
  62. }
  63. }
  64. ```
  65. At present, omitted, `false`, `true`, and object forms have the same runtime
  66. result: V2 runs no formatter. `disabled` is retained as configuration data but
  67. does not control an executable formatter.
  68. ## Commands and placeholders
  69. `command` is an array of strings, not a shell command string. `$FILE` is the V1
  70. file-path placeholder and is often retained in migrated configuration. V2 does
  71. not currently substitute `$FILE` or define another formatter placeholder.
  72. Likewise, V2 does not currently use `extensions` to select commands, merge
  73. `environment` into a child process, discover formatter executables or project
  74. configuration, or run multiple matching formatters. These behaviors will only
  75. be available after a V2 formatter runtime is implemented.