Salta ai contenuti

Formattatori

OpenCode usa formattatori specifici per linguaggio.

OpenCode formatta automaticamente i file dopo che vengono scritti o modificati usando formattatori specifici per linguaggio. Questo assicura che il codice generato segua lo stile del tuo progetto.


Built-in

OpenCode include diversi formattatori integrati per linguaggi e framework popolari. Qui sotto trovi la lista dei formattatori, delle estensioni supportate e dei comandi o opzioni di config richiesti.

FormattatoreEstensioniRequisiti
gofmt.gogofmt command available
mix.ex, .exs, .eex, .heex, .leex, .neex, .sfacemix command available
prettier.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and moreprettier dependency in package.json
biome.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and morebiome.json(c) config file
zig.zig, .zonzig command available
clang-format.c, .cpp, .h, .hpp, .ino, and more.clang-format config file
ktlint.kt, .ktsktlint command available
ruff.py, .pyiruff command available with config
rustfmt.rsrustfmt command available
cargofmt.rscargo fmt command available
uv.py, .pyiuv command available
rubocop.rb, .rake, .gemspec, .rurubocop command available
standardrb.rb, .rake, .gemspec, .rustandardrb command available
htmlbeautifier.erb, .html.erbhtmlbeautifier command available
air.Rair command available
dart.dartdart command available
ocamlformat.ml, .mliocamlformat command available and .ocamlformat config file
terraform.tf, .tfvarsterraform command available
gleam.gleamgleam command available
nixfmt.nixnixfmt command available
shfmt.sh, .bashshfmt command available
pint.phplaravel/pint dependency in composer.json
oxfmt (Experimental).js, .jsx, .ts, .tsxoxfmt dependency in package.json and an experimental env variable flag
ormolu.hsormolu command available

Quindi, se il progetto ha prettier in package.json, OpenCode lo usera automaticamente.


How it works

Quando OpenCode scrive o modifica un file:

  1. Controlla l’estensione del file rispetto a tutti i formattatori abilitati.
  2. Esegue il comando del formattatore appropriato sul file.
  3. Applica automaticamente le modifiche di formattazione.

Questo processo avviene in background, mantenendo lo stile del codice senza passaggi manuali.


Configure

Puoi personalizzare i formattatori nella sezione formatter della config di OpenCode.

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

Ogni configurazione di formattatore supporta:

ProprietaTipoDescrizione
disabledbooleanImpostalo a true per disabilitare il formattatore
commandstring[]Il comando da eseguire per la formattazione
environmentobjectVariabili d’ambiente da impostare quando si esegue
extensionsstring[]Estensioni file gestite da questo formattatore

Vediamo alcuni esempi.


Disabling formatters

Per disabilitare tutti i formattatori globalmente, imposta formatter a false:

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

Per disabilitare un formattatore specifico, imposta disabled a true:

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

Custom formatters

Puoi sovrascrivere i formattatori integrati o aggiungerne di nuovi specificando comando, variabili d’ambiente ed estensioni file:

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"]
}
}
}

Il placeholder $FILE nel comando viene sostituito con il percorso del file in fase di formattazione.