跳到主要內容
文件
啟用

格式化工具

OpenCode 可以在 write、edit 或 patch 工具變更檔案後格式化 檔案。格式化工具預設為停用,因此請在設定中啟用:

opencode.jsonc
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": true,
}

將 formatter 設為 true 可啟用所有內建格式化工具。OpenCode 只會在 可執行檔與任何專案專屬需求都可用時執行內建工具。

opencode.jsonc
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": true,
}

物件也會啟用內建工具,並讓你覆寫它們或新增自訂 格式化工具。空物件會保留繼承的格式化設定,而 true 會將它們重設為內建工具。

opencode.jsonc
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": {},
}

OpenCode 內建下列格式化工具的定義。大多數工具都需要對應的指令;有額外偵測條件的工具會在下表註明。

格式化工具副檔名需求
gofmt.gogofmt 指令
mix.ex、.exs、.eex、.heex、.leex、.neex、.sfacemix 指令
oxfmt.js、.jsx、.mjs、.cjs、.ts、.tsx、.mts、.ctspackage.json 中的 oxfmt 相依性
prettier.js、.jsx、.mjs、.cjs、.ts、.tsx、.mts、.cts、.html、.htm、.css、.scss、.sass、.less、.vue、.svelte、.json、.jsonc、.yaml、.yml、.toml、.xml、.md、.mdx、.graphql、.gqlpackage.json 中的 prettier 相依性
biome與上述 prettier 相同的副檔名biome.json 或 biome.jsonc,以及已安裝的 @biomejs/biome 二進位檔
zig.zig、.zonzig 指令
clang-format.c、.cc、.cpp、.cxx、.c++、.h、.hh、.hpp、.hxx、.h++、.ino、.C、.Hclang-format 指令與 .clang-format
ktlint.kt、.ktsktlint 指令
ruff.py、.pyiruff 指令,以及 Ruff 設定或相依性宣告
air.R自我識別為 R 格式化工具的 air 指令
uv.py、.pyi支援 uv format 的 uv 指令
rubocop.rb、.rake、.gemspec、.rurubocop 指令
standardrb.rb、.rake、.gemspec、.rustandardrb 指令
htmlbeautifier.erbhtmlbeautifier 指令
dart.dartdart 指令
ocamlformat.ml、.mliocamlformat 指令與 .ocamlformat
terraform.tf、.tfvarsterraform 指令
latexindent.texlatexindent 指令
gleam.gleamgleam 指令
shfmt.sh、.bashshfmt 指令
nixfmt.nixnixfmt 指令
rustfmt.rsrustfmt 指令
pint.phpcomposer.json 中的 laravel/pint
ormolu.hsormolu 指令
cljfmt.clj、.cljs、.cljc、.edncljfmt 指令
dfmt.ddfmt 指令

例如,啟用內建工具後,OpenCode 就能找到專案內安裝的 Prettier,並用它格式化符合條件的檔案:

opencode.jsonc
{
  "formatter": true,
}

新增具名項目即可變更內建工具或定義自訂格式化工具。自訂 格式化工具需要同時具備 command 與 extensions 才能執行。

opencode.jsonc
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": {
    "prettier": {
      "environment": {
        "NODE_ENV": "development",
      },
      "extensions": [".js", ".ts"],
    },
    "deno-markdown": {
      "command": ["deno", "fmt", "$FILE"],
      "extensions": [".md"],
    },
  },
}
欄位型別行為
disabledboolean設為 true 時移除具名格式化工具。
commandstring[]取代內建指令或定義自訂指令。
environmentRecord<string, string>新增環境變數,同時保留父環境。
extensionsstring[]取代內建副檔名清單或定義自訂清單。請包含開頭的點。

所有欄位都是選用的。內建項目會繼承省略的值,而缺少 command 或 extensions 的新項目 無法執行。

opencode.jsonc
{
  "formatter": {
    "prettier": {
      "extensions": [".md", ".mdx"],
    },
  },
}

command 是引數陣列,不是 shell 指令字串。OpenCode 會將 $FILE 替換為檔案的絕對路徑,並從作用中的專案目錄執行指令。

opencode.jsonc
{
  "formatter": {
    "custom": {
      "command": ["custom-fmt", "--write", "$FILE"],
      "extensions": [".custom"],
    },
  },
}

OpenCode 會將檔案的最終副檔名與 extensions 比對。比對區分 大小寫,而 .part.md 等複合項目不會比對 notes.part.md,因為其最終副檔名是 .md。

opencode.jsonc
{
  "formatter": {
    "markdown": {
      "command": ["deno", "fmt", "$FILE"],
      "extensions": [".md"],
    },
  },
}

有多個格式化工具相符時,OpenCode 會依註冊順序嘗試,並在第一個成功的指令後 停止。內建工具會保留其內建順序;自訂項目則依物件順序接續。若某個指令以失敗結束, OpenCode 會記錄失敗並嘗試下一個相符項目。

opencode.jsonc
{
  "formatter": {
    "preferred": {
      "command": ["preferred-fmt", "$FILE"],
      "extensions": [".foo"],
    },
    "fallback": {
      "command": ["fallback-fmt", "$FILE"],
      "extensions": [".foo"],
    },
  },
}

若沒有設定檔設定 formatter,格式化就會停用。若要停用優先順序較低的檔案 所啟用的格式化,請將它設為 false。

opencode.jsonc
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": false,
}

若要在保留其他工具的情況下停用某個內建工具,請將其具名項目 標記為已停用。

opencode.jsonc
{
  "formatter": {
    "prettier": {
      "disabled": true,
    },
  },
}