跳转到内容

格式化程序

opencode 使用特定于语言的格式化程序。

使用特定于语言的格式化程序编写或编辑文件后,opencode 会自动格式化文件。这可以确保生成的代码遵循项目的代码风格。


Built-in

opencode 附带了多个适用于流行语言和框架的内置格式化程序。下面是格式化程序、支持的文件扩展名以及所需的命令或配置选项的列表。

FormatterExtensionsRequirements
gofmt.gogofmt command available
mix.ex, .exs, .eex, .heex, .leex, .neex, .sfacemix command available
prettier.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and moreprettier dependency in package.json
biome.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and morebiome.json(c) config file
zig.zig, .zonzig command available
clang-format.c, .cpp, .h, .hpp, .ino, and more.clang-format config file
ktlint.kt, .ktsktlint command available
ruff.py, .pyiruff command available with config
rustfmt.rsrustfmt command available
cargofmt.rscargo fmt command available
uv.py, .pyiuv command available
rubocop.rb, .rake, .gemspec, .rurubocop command available
standardrb.rb, .rake, .gemspec, .rustandardrb command available
htmlbeautifier.erb, .html.erbhtmlbeautifier command available
air.Rair command available
dart.dartdart command available
ocamlformat.ml, .mliocamlformat command available and .ocamlformat config file
terraform.tf, .tfvarsterraform command available
gleam.gleamgleam command available
nixfmt.nixnixfmt command available
shfmt.sh, .bashshfmt command available
pint.phplaravel/pint dependency in composer.json
oxfmt (Experimental).js, .jsx, .ts, .tsxoxfmt dependency in package.json and an experimental env variable flag
ormolu.hsormolu command available

因此,如果您的项目的 prettier 中有 package.json,opencode 将自动使用它。


它是如何工作的

当 opencode 写入或编辑文件时,它:

  1. 根据所有启用的格式化程序检查文件扩展名。
  2. 对文件运行适当的格式化程序命令。
  3. 自动应用格式更改。

此过程在后台进行,确保无需任何手动步骤即可维护您的代码样式。


配置

您可以通过 opencode 配置中的 formatter 部分自定义格式化程序。

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

每个格式化程序配置支持以下内容:

PropertyTypeDescription
disabledboolean将其设置为 true 以禁用格式化程序
commandstring[]格式化运行的命令
environmentobject运行格式化程序时要设置的环境变量
extensionsstring[]此格式化程序应处理的文件扩展名

让我们看一些例子。


禁用格式化程序

要全局禁用 所有 格式化程序,将 formatter 设置为 false

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

要禁用 特定 格式化程序,将 disabled 设置为 true

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

自定义格式化程序

您可以覆盖内置格式化程序或通过指定命令、环境变量和文件扩展名添加新格式化程序:

opencode.json
{
"$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 占位符 将替换为正在格式化的文件的路径。