Skip to content

Commit 1aee8b4

Browse files
authored
feat: add experimental oxfmt formatter (anomalyco#5620)
1 parent 984f17d commit 1aee8b4

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

packages/opencode/src/flag/flag.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export namespace Flag {
2626
truthy("OPENCODE_ENABLE_EXA") || OPENCODE_EXPERIMENTAL || truthy("OPENCODE_EXPERIMENTAL_EXA")
2727
export const OPENCODE_EXPERIMENTAL_BASH_MAX_OUTPUT_LENGTH = number("OPENCODE_EXPERIMENTAL_BASH_MAX_OUTPUT_LENGTH")
2828
export const OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS = number("OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS")
29+
export const OPENCODE_EXPERIMENTAL_OXFMT = OPENCODE_EXPERIMENTAL || truthy("OPENCODE_EXPERIMENTAL_OXFMT")
2930

3031
function truthy(key: string) {
3132
const value = process.env[key]?.toLowerCase()

packages/opencode/src/format/formatter.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { readableStreamToText } from "bun"
22
import { BunProc } from "../bun"
33
import { Instance } from "../project/instance"
44
import { Filesystem } from "../util/filesystem"
5+
import { Flag } from "@/flag/flag"
56

67
export interface Info {
78
name: string
@@ -74,6 +75,25 @@ export const prettier: Info = {
7475
},
7576
}
7677

78+
export const oxfmt: Info = {
79+
name: "oxfmt",
80+
command: [BunProc.which(), "x", "oxfmt", "$FILE"],
81+
environment: {
82+
BUN_BE_BUN: "1",
83+
},
84+
extensions: [".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".mts", ".cts"],
85+
async enabled() {
86+
if (!Flag.OPENCODE_EXPERIMENTAL_OXFMT) return false
87+
const items = await Filesystem.findUp("package.json", Instance.directory, Instance.worktree)
88+
for (const item of items) {
89+
const json = await Bun.file(item).json()
90+
if (json.dependencies?.oxfmt) return true
91+
if (json.devDependencies?.oxfmt) return true
92+
}
93+
return false
94+
},
95+
}
96+
7797
export const biome: Info = {
7898
name: "biome",
7999
command: [BunProc.which(), "x", "@biomejs/biome", "format", "--write", "$FILE"],

packages/web/src/content/docs/cli.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,4 @@ These environment variables enable experimental features that may change or be r
326326
| `OPENCODE_EXPERIMENTAL_BASH_MAX_OUTPUT_LENGTH` | number | Max output length for bash commands |
327327
| `OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS` | number | Default timeout for bash commands in ms |
328328
| `OPENCODE_EXPERIMENTAL_FILEWATCHER` | boolean | Enable file watcher for entire dir |
329+
| `OPENCODE_EXPERIMENTAL_OXFMT` | boolean | Enable oxfmt formatter |

packages/web/src/content/docs/formatters.mdx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,26 @@ OpenCode automatically formats files after they are written or edited using lang
1111

1212
OpenCode comes with several built-in formatters for popular languages and frameworks. Below is a list of the formatters, supported file extensions, and commands or config options it needs.
1313

14-
| Formatter | Extensions | Requirements |
15-
| -------------- | -------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
16-
| gofmt | .go | `gofmt` command available |
17-
| mix | .ex, .exs, .eex, .heex, .leex, .neex, .sface | `mix` command available |
18-
| prettier | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and [more](https://prettier.io/docs/en/index.html) | `prettier` dependency in `package.json` |
19-
| biome | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and [more](https://biomejs.dev/) | `biome.json(c)` config file |
20-
| zig | .zig, .zon | `zig` command available |
21-
| clang-format | .c, .cpp, .h, .hpp, .ino, and [more](https://clang.llvm.org/docs/ClangFormat.html) | `.clang-format` config file |
22-
| ktlint | .kt, .kts | `ktlint` command available |
23-
| ruff | .py, .pyi | `ruff` command available with config |
24-
| uv | .py, .pyi | `uv` command available |
25-
| rubocop | .rb, .rake, .gemspec, .ru | `rubocop` command available |
26-
| standardrb | .rb, .rake, .gemspec, .ru | `standardrb` command available |
27-
| htmlbeautifier | .erb, .html.erb | `htmlbeautifier` command available |
28-
| air | .R | `air` command available |
29-
| dart | .dart | `dart` command available |
30-
| ocamlformat | .ml, .mli | `ocamlformat` command available and `.ocamlformat` config file |
31-
| terraform | .tf, .tfvars | `terraform` command available |
32-
| gleam | .gleam | `gleam` command available |
14+
| Formatter | Extensions | Requirements |
15+
| -------------------- | -------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
16+
| gofmt | .go | `gofmt` command available |
17+
| mix | .ex, .exs, .eex, .heex, .leex, .neex, .sface | `mix` command available |
18+
| prettier | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and [more](https://prettier.io/docs/en/index.html) | `prettier` dependency in `package.json` |
19+
| biome | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and [more](https://biomejs.dev/) | `biome.json(c)` config file |
20+
| zig | .zig, .zon | `zig` command available |
21+
| clang-format | .c, .cpp, .h, .hpp, .ino, and [more](https://clang.llvm.org/docs/ClangFormat.html) | `.clang-format` config file |
22+
| ktlint | .kt, .kts | `ktlint` command available |
23+
| ruff | .py, .pyi | `ruff` command available with config |
24+
| uv | .py, .pyi | `uv` command available |
25+
| rubocop | .rb, .rake, .gemspec, .ru | `rubocop` command available |
26+
| standardrb | .rb, .rake, .gemspec, .ru | `standardrb` command available |
27+
| htmlbeautifier | .erb, .html.erb | `htmlbeautifier` command available |
28+
| air | .R | `air` command available |
29+
| dart | .dart | `dart` command available |
30+
| ocamlformat | .ml, .mli | `ocamlformat` command available and `.ocamlformat` config file |
31+
| terraform | .tf, .tfvars | `terraform` command available |
32+
| gleam | .gleam | `gleam` command available |
33+
| oxfmt (Experimental) | .js, .jsx, .ts, .tsx | `oxfmt` dependency in `package.json` and an [experiental env variable flag](/docs/cli/#experimental) |
3334

3435
So if your project has `prettier` in your `package.json`, OpenCode will automatically use it.
3536

0 commit comments

Comments
 (0)