6.2 代码格式化器

OpenCode内置了20+种代码格式化器,确保AI生成和修改的代码自动符合项目的代码风格规范。

自动格式化功能

当AI创建或修改文件时,OpenCode会自动应用相应的格式化器:

  • 写入文件后 - 自动格式化新创建的文件
  • 编辑文件后 - 自动格式化修改的部分
  • 保存时 - 在文件保存前进行格式化

格式化器会自动检测并使用项目中的配置文件(如.prettierrcbiome.json等)。

内置格式化器

OpenCode内置支持以下格式化器:

格式化器支持的语言配置文件
PrettierJS, TS, CSS, HTML, JSON, YAML, MD.prettierrc, prettier.config.js
BiomeJS, TS, JSON, CSSbiome.json
ESLintJS, TS.eslintrc, eslint.config.js
RuffPythonruff.toml, pyproject.toml
BlackPythonpyproject.toml
autopep8Pythonsetup.cfg, tox.ini
gofmtGo无需配置
goimportsGo无需配置
rustfmtRustrustfmt.toml
clang-formatC, C++, Java.clang-format
shfmtShell/Bash.editorconfig
sql-formatterSQL无需配置
terraform fmtTerraform无需配置
rubocopRuby.rubocop.yml
mix formatElixir.formatter.exs
swift-formatSwift.swift-format
ktlintKotlin.editorconfig
php-cs-fixerPHP.php-cs-fixer.php
styluaLuastylua.toml
zigfmtZig无需配置

配置方法

基本配置结构

{
  "format": {
    "javascript": {
      "command": ["prettier", "--write"],
      "extensions": [".js", ".jsx"],
      "disabled": false
    }
  }
}

指定格式化器

{
  "format": {
    "typescript": {
      "command": ["biome", "format", "--write"]
    },
    "python": {
      "command": ["ruff", "format"]
    },
    "go": {
      "command": ["goimports", "-w"]
    }
  }
}

带选项的配置

{
  "format": {
    "javascript": {
      "command": [
        "prettier",
        "--write",
        "--tab-width", "2",
        "--single-quote",
        "--trailing-comma", "es5"
      ]
    }
  }
}

使用项目本地的格式化器

{
  "format": {
    "typescript": {
      "command": ["npx", "prettier", "--write"]
    },
    "python": {
      "command": ["poetry", "run", "black"]
    }
  }
}

禁用方法

禁用特定语言的格式化

{
  "format": {
    "markdown": {
      "disabled": true
    },
    "json": {
      "disabled": true
    }
  }
}

禁用所有格式化

{
  "format": {
    "*": {
      "disabled": true
    }
  }
}

通过环境变量禁用

# 禁用所有格式化
export OPENCODE_FORMAT_DISABLED=true

# 禁用特定语言
export OPENCODE_FORMAT_DISABLED_PYTHON=true

自定义格式化器

可以为任意文件类型添加自定义格式化器:

添加新格式化器

{
  "format": {
    "custom": {
      "command": ["my-formatter", "--fix"],
      "extensions": [".custom", ".cust"]
    }
  }
}

使用脚本作为格式化器

{
  "format": {
    "special": {
      "command": ["./scripts/format.sh"],
      "extensions": [".special"]
    }
  }
}

链式格式化器

可以通过脚本将多个格式化器串联:

# scripts/format-ts.sh
#!/bin/bash
eslint --fix "$1"
prettier --write "$1"
{
  "format": {
    "typescript": {
      "command": ["./scripts/format-ts.sh"]
    }
  }
}

格式化优先级

当多个格式化器可用时,OpenCode按以下优先级选择:

  1. 配置文件中明确指定的格式化器
  2. 项目中检测到的格式化器配置文件
  3. 系统中安装的默认格式化器
提示

如果项目中有.prettierrc文件,Prettier会自动被选为JS/TS的格式化器。

如果有biome.json,Biome会被优先使用。

常见配置示例

前端项目(Prettier + ESLint)

{
  "format": {
    "javascript": {
      "command": ["npx", "prettier", "--write"]
    },
    "typescript": {
      "command": ["npx", "prettier", "--write"]
    },
    "css": {
      "command": ["npx", "prettier", "--write"]
    },
    "html": {
      "command": ["npx", "prettier", "--write"]
    }
  }
}

Python项目(Ruff)

{
  "format": {
    "python": {
      "command": ["ruff", "format"]
    }
  }
}

Go项目

{
  "format": {
    "go": {
      "command": ["goimports", "-w"]
    }
  }
}

多语言项目

{
  "format": {
    "typescript": {
      "command": ["npx", "biome", "format", "--write"]
    },
    "python": {
      "command": ["black"]
    },
    "go": {
      "command": ["gofmt", "-w"]
    },
    "rust": {
      "command": ["rustfmt"]
    }
  }
}

故障排除

格式化不生效

  • 检查格式化器是否已安装:which prettier
  • 确认文件扩展名在extensions列表中
  • 检查是否被disabled设置禁用

格式化结果不符合预期

  • 检查项目配置文件(如.prettierrc)的设置
  • 确认使用的是正确的格式化器
  • 尝试在命令行直接运行格式化器测试
注意
如果格式化器执行出错,OpenCode会保留原始内容,不会丢失代码。

下一步

接下来学习如何创建自定义命令,扩展OpenCode的功能。