|
|
@@ -1,19 +1,21 @@
|
|
|
---
|
|
|
title: Formatters
|
|
|
-description: Code formatting with opencode
|
|
|
+description: opencode uses language specific formatters.
|
|
|
---
|
|
|
|
|
|
-opencode automatically formats code files after they are written or edited using language-specific formatters. This ensures consistent code style across your project without manual intervention.
|
|
|
+opencode automatically formats files after they are written or edited using language-specific formatters. This ensures that the code that is generated follows the code styles of your project.
|
|
|
|
|
|
-## Built-in Formatters
|
|
|
+---
|
|
|
+
|
|
|
+## Built-in
|
|
|
|
|
|
-opencode comes with several built-in formatters for popular languages and frameworks:
|
|
|
+opencode comes with several built-in formatters for popular languages and frameworks. Below is a list of the formatters, supported file extensions, and commands or config options it needs.
|
|
|
|
|
|
-| Formatter | Languages/Extensions | Requirements |
|
|
|
+| Formatter | Extensions | Requirements |
|
|
|
| -------------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------- |
|
|
|
| gofmt | .go | `gofmt` command available |
|
|
|
| mix | .ex, .exs, .eex, .heex, .leex, .neex, .sface | `mix` command available |
|
|
|
-| prettier | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and [more](https://prettier.io/docs/en/index.html) | `prettier` dependency in package.json |
|
|
|
+| prettier | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and [more](https://prettier.io/docs/en/index.html) | `prettier` dependency in `package.json` |
|
|
|
| biome | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and [more](https://biomejs.dev/) | `biome.json` config file |
|
|
|
| zig | .zig, .zon | `zig` command available |
|
|
|
| clang-format | .c, .cpp, .h, .hpp, .ino, and [more](https://clang.llvm.org/docs/ClangFormat.html) | `.clang-format` config file |
|
|
|
@@ -23,17 +25,51 @@ opencode comes with several built-in formatters for popular languages and framew
|
|
|
| standardrb | .rb, .rake, .gemspec, .ru | `standardrb` command available |
|
|
|
| htmlbeautifier | .erb, .html.erb | `htmlbeautifier` command available |
|
|
|
|
|
|
-Formatters are automatically enabled when their requirements are met in your project environment.
|
|
|
+So if your project has `prettier` in your `package.json`, opencode will automatically use it.
|
|
|
+
|
|
|
+---
|
|
|
|
|
|
-## Configuration
|
|
|
+## How it works
|
|
|
|
|
|
-You can customize formatters through the `formatter` section in your `opencode.json` configuration file.
|
|
|
+When opencode writes or edits a file, it:
|
|
|
|
|
|
-### Disabling Formatters
|
|
|
+1. Checks the file extension against all enabled formatters.
|
|
|
+2. Runs the appropriate formatter command on the file.
|
|
|
+3. Applies the formatting changes automatically.
|
|
|
|
|
|
-To disable a specific formatter, set its `disabled` property to `true`:
|
|
|
+This process happens in the background, ensuring your code styles are maintained without any manual steps.
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## Configure
|
|
|
+
|
|
|
+You can customize formatters through the `formatter` section in your opencode config.
|
|
|
|
|
|
```json title="opencode.json"
|
|
|
+{
|
|
|
+ "$schema": "https://opencode.ai/config.json",
|
|
|
+ "formatter": { }
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+Each formatter configuration supports the following:
|
|
|
+
|
|
|
+| Property | Type | Description |
|
|
|
+| ------------- | -------- | ------------------------------------------------------- |
|
|
|
+| `disabled` | boolean | Set this to `true` to disable the formatter |
|
|
|
+| `command` | string[] | The command to run for formatting |
|
|
|
+| `environment` | object | Environment variables to set when running the formatter |
|
|
|
+| `extensions` | string[] | File extensions this formatter should handle |
|
|
|
+
|
|
|
+Let's look at some examples.
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+### Disabling formatters
|
|
|
+
|
|
|
+To disable a specific formatter, set `disabled` to `true`:
|
|
|
+
|
|
|
+```json title="opencode.json" {5}
|
|
|
{
|
|
|
"$schema": "https://opencode.ai/config.json",
|
|
|
"formatter": {
|
|
|
@@ -44,11 +80,13 @@ To disable a specific formatter, set its `disabled` property to `true`:
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-### Custom Formatters
|
|
|
+---
|
|
|
|
|
|
-You can override the default formatters or add new ones by specifying the command, environment variables, and file extensions:
|
|
|
+### Custom formatters
|
|
|
|
|
|
-```json title="opencode.json"
|
|
|
+You can override the built-in formatters or add new ones by specifying the command, environment variables, and file extensions:
|
|
|
+
|
|
|
+```json title="opencode.json" {4-10}
|
|
|
{
|
|
|
"$schema": "https://opencode.ai/config.json",
|
|
|
"formatter": {
|
|
|
@@ -63,25 +101,4 @@ You can override the default formatters or add new ones by specifying the comman
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-The `$FILE` placeholder in the command will be replaced with the path to the file being formatted.
|
|
|
-
|
|
|
-### Configuration Options
|
|
|
-
|
|
|
-Each formatter configuration supports these properties:
|
|
|
-
|
|
|
-| Property | Type | Description |
|
|
|
-| ------------- | -------- | ------------------------------------------------------- |
|
|
|
-| `disabled` | boolean | Set to `true` to disable the formatter |
|
|
|
-| `command` | string[] | The command to run for formatting |
|
|
|
-| `environment` | object | Environment variables to set when running the formatter |
|
|
|
-| `extensions` | string[] | File extensions this formatter should handle |
|
|
|
-
|
|
|
-## How It Works
|
|
|
-
|
|
|
-When opencode writes or edits a file, it:
|
|
|
-
|
|
|
-1. Checks the file extension against all enabled formatters
|
|
|
-2. Runs the appropriate formatter command on the file
|
|
|
-3. Applies the formatting changes automatically
|
|
|
-
|
|
|
-This process happens seamlessly in the background, ensuring your code maintains consistent formatting without requiring manual steps.
|
|
|
+The **`$FILE` placeholder** in the command will be replaced with the path to the file being formatted.
|