mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-01 02:06:41 +00:00
131 lines
8.3 KiB
Plaintext
131 lines
8.3 KiB
Plaintext
---
|
||
title: 格式化程序
|
||
description: opencode 使用特定於語言的格式化程序。
|
||
---
|
||
|
||
使用特定於語言的格式化程序編寫或編輯文件後,opencode 會自動格式化文件。這可確保生成的代碼遵循項目的代碼風格。
|
||
|
||
---
|
||
|
||
## 內建
|
||
|
||
opencode 附帶了多個適用於流行語言和框架的內置格式化程序。下面是格式化程序、支持的文件擴展名以及所需的命令或配置選項的列表。
|
||
|
||
| 格式化程序 | 擴展 | 要求 |
|
||
| -------------------- | ------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
|
||
| gofmt | .go | `gofmt` 命令可用 |
|
||
| mix | .ex, .exs, .eex, .heex, .leex, .neex, .sface | `mix` 命令可用 |
|
||
| prettier | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, 和 [更多](https://prettier.io/docs/en/index.html) | `package.json` 中有 `prettier` 依賴 |
|
||
| biome | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, 和 [更多](https://biomejs.dev/) | `biome.json(c)` 配置文件 |
|
||
| zig | .zig, .zon | `zig` 命令可用 |
|
||
| clang-format | .c, .cpp, .h, .hpp, .ino, 和 [更多](https://clang.llvm.org/docs/ClangFormat.html) | `.clang-format` 配置文件 |
|
||
| ktlint | .kt, .kts | `ktlint` 命令可用 |
|
||
| ruff | .py, .pyi | `ruff` 命令可用并配置完成 |
|
||
| rustfmt | .rs | `rustfmt` 命令可用 |
|
||
| cargofmt | .rs | `cargo fmt` 命令可用 |
|
||
| uv | .py, .pyi | `uv` 命令可用 |
|
||
| rubocop | .rb, .rake, .gemspec, .ru | `rubocop` 命令可用 |
|
||
| standardrb | .rb, .rake, .gemspec, .ru | `standardrb` 命令可用 |
|
||
| htmlbeautifier | .erb, .html.erb | `htmlbeautifier` 命令可用 |
|
||
| air | .R | `air` 命令可用 |
|
||
| dart | .dart | `dart` 命令可用 |
|
||
| ocamlformat | .ml, .mli | `ocamlformat` 命令可用,且存在 `.ocamlformat` 配置文件 |
|
||
| terraform | .tf, .tfvars | `terraform` 命令可用 |
|
||
| gleam | .gleam | `gleam` 命令可用 |
|
||
| nixfmt | .nix | `nixfmt` 命令可用 |
|
||
| shfmt | .sh, .bash | `shfmt` 命令可用 |
|
||
| pint | .php | `composer.json` 中有 `laravel/pint` 依賴 |
|
||
| oxfmt (Experimental) | .js, .jsx, .ts, .tsx | `package.json` 中有 `oxfmt` 依賴且啟用[實驗環境變量旗標](/docs/cli/#experimental) |
|
||
| ormolu | .hs | `ormolu` 命令可用 |
|
||
|
||
因此,如果您的項目的`package.json`中有`prettier`,opencode將自動使用它。
|
||
|
||
---
|
||
|
||
## 它是如何運作的
|
||
|
||
當 opencode 寫入或編輯文件時,它:
|
||
|
||
1. 根據所有啟用的格式化程序檢查文件擴展名。
|
||
2. 對文件運行適當的格式化程序命令。
|
||
3. 自動應用格式更改。
|
||
|
||
此過程在後台進行,確保無需任何手動步驟即可維護您的代碼樣式。
|
||
|
||
---
|
||
|
||
## 配置
|
||
|
||
您可以通過 opencode 配置中的 `formatter` 部分自定義格式化程序。
|
||
|
||
```json title="opencode.json"
|
||
{
|
||
"$schema": "https://opencode.ai/config.json",
|
||
"formatter": {}
|
||
}
|
||
```
|
||
|
||
每個格式化程序配置支持以下內容:
|
||
|
||
| 物業 | 類型 | 描述 |
|
||
| ------------- | -------- | ---------------------------------- |
|
||
| `disabled` | 布爾 | 將其設置為 `true` 以禁用格式化程序 |
|
||
| `command` | 字符串[] | 格式化運行的命令 |
|
||
| `environment` | 對象 | 運行格式化程序時要設置的環境變量 |
|
||
| `extensions` | 字符串[] | 此格式化程序應處理的文件擴展名 |
|
||
|
||
讓我們看一些例子。
|
||
|
||
---
|
||
|
||
### 禁用格式化程序
|
||
|
||
要全局禁用**所有**格式化程序,請將`formatter`設置為`false`:
|
||
|
||
```json title="opencode.json" {3}
|
||
{
|
||
"$schema": "https://opencode.ai/config.json",
|
||
"formatter": false
|
||
}
|
||
```
|
||
|
||
要禁用**特定**格式化程序,請將`disabled`設置為`true`:
|
||
|
||
```json title="opencode.json" {5}
|
||
{
|
||
"$schema": "https://opencode.ai/config.json",
|
||
"formatter": {
|
||
"prettier": {
|
||
"disabled": true
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
### 自定義格式化程序
|
||
|
||
您可以覆蓋內置格式化程序或通過指定命令、環境變量和文件擴展名添加新格式化程序:
|
||
|
||
```json title="opencode.json" {4-14}
|
||
{
|
||
"$schema": "https://opencode.ai/config.json",
|
||
"formatter": {
|
||
"prettier": {
|
||
"command": ["npx", "prettier", "--write", "$FILE"],
|
||
"environment": {
|
||
"NODE_ENV": "development"
|
||
},
|
||
"extensions": [".js", ".ts", ".jsx", ".tsx"]
|
||
},
|
||
"custom-markdown-formatter": {
|
||
"command": ["deno", "fmt", "$FILE"],
|
||
"extensions": [".md"]
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
命令中的 **`$FILE` 佔位符** 將替換為正在格式化的文件的路徑。
|