한국어
문서
포매터

포매터

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

OpenCode는 파일을 작성하거나 편집한 후 언어별 포매터를 사용하여 파일을 포맷할 수 있습니다. 포매터는 기본적으로 비활성화되어 있으므로, OpenCode가 실행하기 전에 설정에서 활성화해야 합니다.


내장 포매터

OpenCode에는 인기 있는 언어와 프레임워크를 위한 여러 내장 포매터가 포함되어 있습니다. 아래는 포매터, 지원되는 파일 확장자, 필요한 명령어 또는 설정 옵션 목록입니다.

포매터확장자요구사항
air.Rair 명령어 사용 가능
biome.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml 및 기타 (opens in a new tab)biome.json(c) 설정 파일
cargofmt.rscargo fmt 명령어 사용 가능
clang-format.c, .cpp, .h, .hpp, .ino 및 기타 (opens in a new tab).clang-format 설정 파일
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 설정 파일
ormolu.hsormolu 명령어 사용 가능
oxfmt (실험적).js, .jsx, .ts, .tsxpackage.jsonoxfmt 의존성 및 실험적 환경 변수 플래그
pint.phpcomposer.jsonlaravel/pint 의존성
prettier.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml 및 기타 (opens in a new tab)package.jsonprettier 의존성
rubocop.rb, .rake, .gemspec, .rurubocop 명령어 사용 가능
ruff.py, .pyiruff 명령어 사용 가능 및 설정
rustfmt.rsrustfmt 명령어 사용 가능
shfmt.sh, .bashshfmt 명령어 사용 가능
standardrb.rb, .rake, .gemspec, .rustandardrb 명령어 사용 가능
terraform.tf, .tfvarsterraform 명령어 사용 가능
uv.py, .pyiuv 명령어 사용 가능
zig.zig, .zonzig 명령어 사용 가능

포매터가 활성화되면, 프로젝트의 package.jsonprettier가 있을 경우 OpenCode는 일치하는 파일에 prettier를 사용합니다.


작동 방식

OpenCode가 파일을 작성하거나 편집하고 포매터가 활성화되어 있으면:

  1. 활성화된 모든 포매터에 대해 파일 확장자를 확인합니다.
  2. 파일에 적절한 포매터 명령어를 실행합니다.
  3. 포맷 변경 사항을 적용합니다.

이 프로세스는 활성화된 포매터에 대해 백그라운드에서 발생합니다.


설정

OpenCode 설정의 formatter 섹션을 통해 포매터를 활성화하고 사용자 정의할 수 있습니다.

모든 내장 포매터를 활성화하려면 formattertrue로 설정합니다.

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

내장 포매터를 활성화한 상태로 유지하면서 재정의나 사용자 정의 포매터를 구성하려면 객체를 사용합니다.

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

각 포매터 설정은 다음을 지원합니다:

속성타입설명
disabledboolean포매터를 비활성화하려면 true로 설정
commandstring[]포맷에 실행할 명령어. 사용자 정의 포매터에는 필수이며, 내장 포매터에는 선택 사항입니다.
environmentobject포매터 실행 시 설정할 환경 변수
extensionsstring[]이 포매터가 처리할 파일 확장자

몇 가지 예를 살펴보겠습니다.


포매터 비활성화

formatter를 생략하면 모든 포매터가 비활성화됩니다. 다른 설정에서 포매터를 활성화한 후 모두 비활성화하려면 formatterfalse로 설정합니다:

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

특정 포매터를 비활성화하려면 disabledtrue로 설정합니다:

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

사용자 정의 포매터

environmentextensions 같은 옵션으로 내장 포매터를 설정할 수 있습니다. 사용자 정의 포매터를 추가하려면 commandextensions를 지정하세요:

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 플레이스홀더는 포맷되는 파일의 경로로 대체됩니다.