File size: 12,200 Bytes
0dbc9de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | ---
title: ฟอร์แมตเตอร์
description: OpenCode ใช้ตัวจัดรูปแบบเฉพาะภาษา
---
OpenCode จะจัดรูปแบบไฟล์โดยอัตโนมัติหลังจากที่เขียนหรือแก้ไขโดยใช้ตัวจัดรูปแบบเฉพาะภาษา เพื่อให้แน่ใจว่าโค้ดที่สร้างขึ้นเป็นไปตามสไตล์โค้ดของโปรเจ็กต์ของคุณ
---
## บิวท์อิน
OpenCode มาพร้อมกับฟอร์แมตเตอร์ในตัวหลายตัวสำหรับภาษาและเฟรมเวิร์กยอดนิยม ด้านล่างนี้คือรายการฟอร์แมตเตอร์ นามสกุลไฟล์ที่รองรับ และคำสั่งหรือตัวเลือกการกำหนดค่าที่ต้องการ
| ฟอร์แมตเตอร์ | ส่วนขยาย | ความต้องการ |
| -------------- | ------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------- |
| gofmt | .go | `gofmt` คำสั่งใช้ได้ |
| mix | .ex, .exs, .eex, .heex, .leex, .neex, .sface | `mix` คำสั่งใช้ได้ |
| prettier | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml และ [เพิ่มเติม](https://prettier.io/docs/en/index.html) | `prettier` การพึ่งพาใน `package.json` |
| biome | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml และ [เพิ่มเติม](https://biomejs.dev/) | `biome.json(c)` ไฟล์ปรับแต่ง |
| zig | .zig, .zon | `zig` คำสั่งใช้ได้ |
| clang-format | .c, .cpp, .h, .hpp, .ino และ [เพิ่มเติม](https://clang.llvm.org/docs/ClangFormat.html) | `.clang-format` ไฟล์ปรับแต่ง |
| ktlint | .kt, .kts | `ktlint` คำสั่งใช้ได้ |
| ruff | .py, .pyi | คำสั่ง `ruff` ใช้ได้กับ config |
| rust | .rs | `rustfmt` คำสั่งใช้ได้ |
| cargo fmt | .rs | `cargo fmt` คำสั่งใช้ได้ |
| uv | .py, .pyi | `uv` คำสั่งใช้ได้ |
| rubocop | .rb, .rake, .gemspec, .ru | `rubocop` คำสั่งใช้ได้ |
| standardrb | .rb, .rake, .gemspec, .ru | `standardrb` คำสั่งใช้ได้ |
| htmlbeautifier | .erb, .html.erb | `htmlbeautifier` คำสั่งใช้ได้ |
| air | .r | `air` คำสั่งใช้ได้ |
| dart | .dart | `dart` คำสั่งใช้ได้ |
| dfmt | .d | `dfmt` คำสั่งใช้ได้ |
| ocamlformat | .ml, .mli | มีคำสั่ง `ocamlformat` และไฟล์ปรับแต่ง `.ocamlformat` |
| terraform | .tf, .tfvars | `terraform` คำสั่งใช้ได้ |
| gleam | .gleam | `gleam` คำสั่งใช้ได้ |
| nix | .nix | `nixfmt` คำสั่งใช้ได้ |
| shfmt | .sh, .bash | `shfmt` คำสั่งใช้ได้ |
| pint | .php | `laravel/pint` การพึ่งพาใน `composer.json` |
| oxfmt (ทดลอง) | .js, .jsx, .ts, .tsx | `oxfmt` การพึ่งพาใน `package.json` และ [แฟล็กตัวแปร env ทดลอง](/docs/cli/#experimental) |
| ormolu | .hs | `ormolu` คำสั่งใช้ได้ |
ดังนั้นหากโปรเจ็กต์ของคุณมี `prettier` ใน `package.json` OpenCode จะใช้มันโดยอัตโนมัติ
---
## มันทำงานอย่างไร
เมื่อ OpenCode เขียนหรือแก้ไขไฟล์ มันจะ:
1. ตรวจสอบนามสกุลไฟล์กับฟอร์แมตเตอร์ที่เปิดใช้งานทั้งหมด
2. รันคำสั่งฟอร์แมตเตอร์ที่เหมาะสมบนไฟล์
3. ใช้การเปลี่ยนแปลงการจัดรูปแบบโดยอัตโนมัติ
กระบวนการนี้เกิดขึ้นในเบื้องหลัง ทำให้มั่นใจได้ว่าสไตล์โค้ดของคุณจะถูกรักษาไว้โดยไม่มีขั้นตอนที่ต้องทำเอง
---
## กำหนดค่า
คุณสามารถปรับแต่งฟอร์แมตเตอร์ได้ผ่านทางส่วน `formatter` ในการกำหนดค่า OpenCode ของคุณ
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"formatter": {}
}
```
การกำหนดค่าฟอร์แมตเตอร์แต่ละรายการรองรับสิ่งต่อไปนี้:
| คุณสมบัติ | พิมพ์ | คำอธิบาย |
| ------------- | ------- | ------------------------------------------------- |
| `disabled` | บูลีน | ตั้งค่านี้เป็น `true` เพื่อปิดใช้งานฟอร์แมตเตอร์ |
| `command` | สตริง[] | คำสั่งให้เรียกใช้การจัดรูปแบบ |
| `environment` | วัตถุ | ตัวแปรสภาพแวดล้อมที่จะตั้งค่าเมื่อรันฟอร์แมตเตอร์ |
| `extensions` | สตริง[] | นามสกุลไฟล์ที่ฟอร์แมตเตอร์นี้ควรรองรับ |
ลองดูตัวอย่างบางส่วน
---
### ปิดการใช้งานฟอร์แมตเตอร์
หากต้องการปิดการใช้งาน **ทั้งหมด** ฟอร์แมตเตอร์ทั่วโลก ให้ตั้งค่า `formatter` เป็น `false`:
```json title="opencode.json" {3}
{
"$schema": "https://opencode.ai/config.json",
"formatter": false
}
```
หากต้องการปิดใช้งานฟอร์แมตเตอร์ **เฉพาะ** ให้ตั้งค่า `disabled` เป็น `true`:
```json title="opencode.json" {5}
{
"$schema": "https://opencode.ai/config.json",
"formatter": {
"prettier": {
"disabled": true
}
}
}
```
---
### ตัวจัดรูปแบบที่กำหนดเอง
คุณสามารถแทนที่ฟอร์แมตเตอร์ที่มีอยู่แล้วภายในหรือเพิ่มฟอร์แมตใหม่ได้โดยการระบุคำสั่ง ตัวแปรสภาพแวดล้อม และนามสกุลไฟล์:
```json title="opencode.json" {4-14}
{
"$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`** ในคำสั่งจะถูกแทนที่ด้วยเส้นทางไปยังไฟล์ที่กำลังจัดรูปแบบ
|