콘텐츠로 이동

포매터

OpenCode는 언어별 포매터를 사용합니다.

OpenCode는 파일을 write하거나 edit한 뒤, 언어별 포매터를 사용해 자동으로 포맷합니다. 이를 통해 생성된 코드가 프로젝트의 코드 스타일을 따르도록 보장합니다.


내장

OpenCode는 주요 언어와 프레임워크를 위한 여러 내장 포매터를 제공합니다. 아래는 포매터 목록, 지원 확장자, 필요한 명령 또는 config 옵션입니다.

포매터확장자요구 사항
air.Rair 명령 사용 가능
biome.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, 기타biome.json(c) config 파일
cargofmt.rscargo fmt 명령 사용 가능
clang-format.c, .cpp, .h, .hpp, .ino, 기타.clang-format config 파일
cljfmt.clj, .cljs, .cljc, .edncljfmt 명령 사용 가능
dart.dartdart 명령 사용 가능
dfmt.ddfmt 명령 사용 가능
gleam.gleamgleam 명령 사용 가능
gofmt.gogofmt 명령 사용 가능
htmlbeautifier.erb, .html.erbhtmlbeautifier 명령 사용 가능
ktlint.kt, .ktsktlint 명령 사용 가능
mix.ex, .exs, .eex, .heex, .leex, .neex, .sfacemix 명령 사용 가능
nixfmt.nixnixfmt 명령 사용 가능
ocamlformat.ml, .mliocamlformat 명령 사용 가능 및 .ocamlformat config 파일 필요
ormolu.hsormolu 명령 사용 가능
oxfmt (Experimental).js, .jsx, .ts, .tsxpackage.jsonoxfmt dependency 필요 및 experimental env variable flag
pint.phpcomposer.jsonlaravel/pint dependency 필요
prettier.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, 기타package.jsonprettier dependency 필요
rubocop.rb, .rake, .gemspec, .rurubocop 명령 사용 가능
ruff.py, .pyiruff 명령 사용 가능 및 관련 config 필요
rustfmt.rsrustfmt 명령 사용 가능
shfmt.sh, .bashshfmt 명령 사용 가능
standardrb.rb, .rake, .gemspec, .rustandardrb 명령 사용 가능
terraform.tf, .tfvarsterraform 명령 사용 가능
uv.py, .pyiuv 명령 사용 가능
zig.zig, .zonzig 명령 사용 가능

예를 들어 프로젝트 package.jsonprettier가 있으면 OpenCode가 자동으로 해당 포매터를 사용합니다.


작동 방식

OpenCode가 파일을 write하거나 edit할 때 다음 순서로 동작합니다.

  1. 활성화된 모든 포매터와 파일 확장자를 대조합니다.
  2. 파일에 맞는 포매터 명령을 실행합니다.
  3. 포맷 변경 사항을 자동으로 적용합니다.

이 과정은 background에서 실행되며, 수동 작업 없이 코드 스타일이 유지됩니다.


구성

OpenCode config의 formatter 섹션에서 포매터를 커스터마이즈할 수 있습니다.

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

각 formatter 설정에서 지원하는 항목은 다음과 같습니다.

속성타입설명
disabledbooleantrue로 설정하면 해당 포매터를 비활성화합니다
commandstring[]포맷 실행 명령입니다
environmentobject포매터 실행 시 설정할 환경 변수입니다
extensionsstring[]해당 포매터가 처리할 파일 확장자입니다

아래 예시를 참고하세요.


포매터 비활성화

전체 포매터를 전역에서 비활성화하려면 formatterfalse로 설정하세요.

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

특정 포매터만 비활성화하려면 disabledtrue로 설정하세요.

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

사용자 정의 포매터

명령, 환경 변수, 파일 확장자를 지정해 내장 포매터를 override하거나 새 포매터를 추가할 수 있습니다.

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 placeholder는 포맷 대상 파일 경로로 치환됩니다.