포매터
OpenCode는 언어별 포매터를 사용합니다.
OpenCode는 파일을 작성하거나 편집한 후 언어별 포매터를 사용하여 파일을 포맷할 수 있습니다. 포매터는 기본적으로 비활성화되어 있으므로, OpenCode가 실행하기 전에 설정에서 활성화해야 합니다.
내장 포매터
OpenCode에는 인기 있는 언어와 프레임워크를 위한 여러 내장 포매터가 포함되어 있습니다. 아래는 포매터, 지원되는 파일 확장자, 필요한 명령어 또는 설정 옵션 목록입니다.
| 포매터 | 확장자 | 요구사항 |
|---|---|---|
| air | .R | air 명령어 사용 가능 |
| biome | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml 및 기타 (opens in a new tab) | biome.json(c) 설정 파일 |
| cargofmt | .rs | cargo fmt 명령어 사용 가능 |
| clang-format | .c, .cpp, .h, .hpp, .ino 및 기타 (opens in a new tab) | .clang-format 설정 파일 |
| cljfmt | .clj, .cljs, .cljc, .edn | cljfmt 명령어 사용 가능 |
| dart | .dart | dart 명령어 사용 가능 |
| dfmt | .d | dfmt 명령어 사용 가능 |
| gleam | .gleam | gleam 명령어 사용 가능 |
| gofmt | .go | gofmt 명령어 사용 가능 |
| htmlbeautifier | .erb, .html.erb | htmlbeautifier 명령어 사용 가능 |
| ktlint | .kt, .kts | ktlint 명령어 사용 가능 |
| mix | .ex, .exs, .eex, .heex, .leex, .neex, .sface | mix 명령어 사용 가능 |
| nixfmt | .nix | nixfmt 명령어 사용 가능 |
| ocamlformat | .ml, .mli | ocamlformat 명령어 사용 가능 및 .ocamlformat 설정 파일 |
| ormolu | .hs | ormolu 명령어 사용 가능 |
| oxfmt (실험적) | .js, .jsx, .ts, .tsx | package.json에 oxfmt 의존성 및 실험적 환경 변수 플래그 |
| pint | .php | composer.json에 laravel/pint 의존성 |
| prettier | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml 및 기타 (opens in a new tab) | package.json에 prettier 의존성 |
| rubocop | .rb, .rake, .gemspec, .ru | rubocop 명령어 사용 가능 |
| ruff | .py, .pyi | ruff 명령어 사용 가능 및 설정 |
| rustfmt | .rs | rustfmt 명령어 사용 가능 |
| shfmt | .sh, .bash | shfmt 명령어 사용 가능 |
| standardrb | .rb, .rake, .gemspec, .ru | standardrb 명령어 사용 가능 |
| terraform | .tf, .tfvars | terraform 명령어 사용 가능 |
| uv | .py, .pyi | uv 명령어 사용 가능 |
| zig | .zig, .zon | zig 명령어 사용 가능 |
포매터가 활성화되면, 프로젝트의 package.json에 prettier가 있을 경우 OpenCode는 일치하는 파일에 prettier를 사용합니다.
작동 방식
OpenCode가 파일을 작성하거나 편집하고 포매터가 활성화되어 있으면:
- 활성화된 모든 포매터에 대해 파일 확장자를 확인합니다.
- 파일에 적절한 포매터 명령어를 실행합니다.
- 포맷 변경 사항을 적용합니다.
이 프로세스는 활성화된 포매터에 대해 백그라운드에서 발생합니다.
설정
OpenCode 설정의 formatter 섹션을 통해 포매터를 활성화하고 사용자 정의할 수 있습니다.
모든 내장 포매터를 활성화하려면 formatter를 true로 설정합니다.
{
"$schema": "https://opencode.ai/config.json",
"formatter": true
}내장 포매터를 활성화한 상태로 유지하면서 재정의나 사용자 정의 포매터를 구성하려면 객체를 사용합니다.
{
"$schema": "https://opencode.ai/config.json",
"formatter": {}
}각 포매터 설정은 다음을 지원합니다:
| 속성 | 타입 | 설명 |
|---|---|---|
disabled | boolean | 포매터를 비활성화하려면 true로 설정 |
command | string[] | 포맷에 실행할 명령어. 사용자 정의 포매터에는 필수이며, 내장 포매터에는 선택 사항입니다. |
environment | object | 포매터 실행 시 설정할 환경 변수 |
extensions | string[] | 이 포매터가 처리할 파일 확장자 |
몇 가지 예를 살펴보겠습니다.
포매터 비활성화
formatter를 생략하면 모든 포매터가 비활성화됩니다. 다른 설정에서 포매터를 활성화한 후 모두 비활성화하려면 formatter를 false로 설정합니다:
{
"$schema": "https://opencode.ai/config.json",
"formatter": false
}특정 포매터를 비활성화하려면 disabled를 true로 설정합니다:
{
"$schema": "https://opencode.ai/config.json",
"formatter": {
"prettier": {
"disabled": true
}
}
}사용자 정의 포매터
environment나 extensions 같은 옵션으로 내장 포매터를 설정할 수 있습니다. 사용자 정의 포매터를 추가하려면 command와 extensions를 지정하세요:
{
"$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 플레이스홀더는 포맷되는 파일의 경로로 대체됩니다.