한국어
문서
포매터

포매터

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

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

내장 포매터

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

포매터확장자요구사항
gofmt.gogofmt 명령어 사용 가능
mix.ex, .exs, .eex, .heex, .leex, .neex, .sfacemix 명령어 사용 가능
prettier.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml 및 기타 (opens in a new tab)package.jsonprettier 의존성
biome.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml 및 기타 (opens in a new tab)biome.json(c) 설정 파일
zig.zig, .zonzig 명령어 사용 가능
clang-format.c, .cpp, .h, .hpp, .ino 및 기타 (opens in a new tab).clang-format 설정 파일
ktlint.kt, .ktsktlint 명령어 사용 가능
ruff.py, .pyiruff 명령어 사용 가능 및 설정
rustfmt.rsrustfmt 명령어 사용 가능
cargofmt.rscargo fmt 명령어 사용 가능
uv.py, .pyiuv 명령어 사용 가능
rubocop.rb, .rake, .gemspec, .rurubocop 명령어 사용 가능
standardrb.rb, .rake, .gemspec, .rustandardrb 명령어 사용 가능
htmlbeautifier.erb, .html.erbhtmlbeautifier 명령어 사용 가능
air.Rair 명령어 사용 가능
dart.dartdart 명령어 사용 가능
ocamlformat.ml, .mliocamlformat 명령어 사용 가능 및 .ocamlformat 설정 파일
terraform.tf, .tfvarsterraform 명령어 사용 가능
gleam.gleamgleam 명령어 사용 가능
nixfmt.nixnixfmt 명령어 사용 가능
shfmt.sh, .bashshfmt 명령어 사용 가능
pint.phpcomposer.jsonlaravel/pint 의존성
oxfmt (실험적).js, .jsx, .ts, .tsxpackage.jsonoxfmt 의존성 및 실험적 환경 변수 플래그
ormolu.hsormolu 명령어 사용 가능

따라서 프로젝트의 package.jsonprettier가 있으면 OpenCode가 자동으로 사용합니다.

작동 방식

OpenCode가 파일을 작성하거나 편집할 때:

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

이 프로세스는 백그라운드에서 발생하여 수동 단계 없이 코드 스타일이 유지되도록 합니다.

설정

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

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

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

속성타입설명
disabledboolean포매터를 비활성화하려면 true로 설정
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
    }
  }
}

사용자 정의 포매터

명령어, 환경 변수, 파일 확장자를 지정하여 내장 포매터를 재정의하거나 새로운 포매터를 추가할 수 있습니다:

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