Skip to content

Commit 30e1012

Browse files
committed
formatter config
1 parent 5e66fc2 commit 30e1012

5 files changed

Lines changed: 37 additions & 6 deletions

File tree

opencode.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
}
2020
}
2121
},
22+
"formatter": {
23+
"test": {
24+
"extensions": [".json"],
25+
"command": ["sed", "-i", "s/name/poop/g", "$FILE"]
26+
}
27+
},
2228
"mcp": {
2329
"context7": {
2430
"type": "remote",

packages/opencode/src/config/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ export namespace Config {
283283
z.string(),
284284
z.object({
285285
disabled: z.boolean().optional(),
286+
command: z.array(z.string()).optional(),
287+
environment: z.record(z.string(), z.string()).optional(),
288+
extensions: z.array(z.string()).optional(),
286289
}),
287290
)
288291
.optional(),

packages/opencode/src/format/formatter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ export const clang: Info = {
129129
command: ["clang-format", "-i", "$FILE"],
130130
extensions: [".c", ".cc", ".cpp", ".cxx", ".c++", ".h", ".hh", ".hpp", ".hxx", ".h++", ".ino", ".C", ".H"],
131131
async enabled() {
132-
return Bun.which("clang-format") !== null
132+
const app = App.info()
133+
const items = await Filesystem.findUp(".clang-format", app.path.cwd, app.path.root)
134+
return items.length > 0
133135
},
134136
}
135137

packages/opencode/src/format/index.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,39 @@ import path from "path"
66

77
import * as Formatter from "./formatter"
88
import { Config } from "../config/config"
9+
import { mergeDeep } from "remeda"
910

1011
export namespace Format {
1112
const log = Log.create({ service: "format" })
1213

13-
const state = App.state("format", () => {
14+
const state = App.state("format", async () => {
1415
const enabled: Record<string, boolean> = {}
16+
const cfg = await Config.get()
17+
18+
const formatters = { ...Formatter } as Record<string, Formatter.Info>
19+
for (const [name, item] of Object.entries(cfg.formatter ?? {})) {
20+
if (item.disabled) {
21+
delete formatters[name]
22+
continue
23+
}
24+
const result: Formatter.Info = mergeDeep(formatters[name] ?? {}, {
25+
command: [],
26+
extensions: [],
27+
...item,
28+
})
29+
result.enabled = async () => true
30+
result.name = name
31+
formatters[name] = result
32+
}
1533

1634
return {
1735
enabled,
36+
formatters,
1837
}
1938
})
2039

2140
async function isEnabled(item: Formatter.Info) {
22-
const s = state()
41+
const s = await state()
2342
let status = s.enabled[item.name]
2443
if (status === undefined) {
2544
status = await item.enabled()
@@ -29,11 +48,11 @@ export namespace Format {
2948
}
3049

3150
async function getFormatter(ext: string) {
32-
const cfg = await Config.get()
51+
const formatters = await state().then((x) => x.formatters)
3352
const result = []
34-
for (const item of Object.values(Formatter)) {
53+
for (const item of Object.values(formatters)) {
54+
log.info("checking", { name: item.name, ext })
3555
if (!item.extensions.includes(ext)) continue
36-
if (cfg.formatter?.[item.name]?.disabled) continue
3756
if (!(await isEnabled(item))) continue
3857
result.push(item)
3958
}

packages/opencode/src/tool/edit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export const EditTool = Tool.define("edit", {
9696
file: filePath,
9797
})
9898
contentNew = await file.text()
99+
diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew))
99100
})()
100101

101102
FileTime.read(ctx.sessionID, filePath)

0 commit comments

Comments
 (0)