GeezerVison is showing github.com — formatter config · steipete/opencode@30e1012
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: opencode.json +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | } | 20 | 20 | } | 21 | 21 | }, | | 22 | + "formatter": { | | 23 | + "test": { | | 24 | + "extensions": [".json"], | | 25 | + "command": ["sed", "-i", "s/name/poop/g", "$FILE"] | | 26 | + } | | 27 | + }, | 22 | 28 | "mcp": { | 23 | 29 | "context7": { | 24 | 30 | "type": "remote", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/config/config.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -283,6 +283,9 @@ export namespace Config { | 283 | 283 | z.string(), | 284 | 284 | z.object({ | 285 | 285 | 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(), | 286 | 289 | }), | 287 | 290 | ) | 288 | 291 | .optional(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/format/formatter.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -129,7 +129,9 @@ export const clang: Info = { | 129 | 129 | command: ["clang-format", "-i", "$FILE"], | 130 | 130 | extensions: [".c", ".cc", ".cpp", ".cxx", ".c++", ".h", ".hh", ".hpp", ".hxx", ".h++", ".ino", ".C", ".H"], | 131 | 131 | 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 | 133 | 135 | }, | 134 | 136 | } | 135 | 137 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/format/index.ts +24-5Lines changed: 24 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,20 +6,39 @@ import path from "path" | 6 | 6 |
| 7 | 7 | import * as Formatter from "./formatter" | 8 | 8 | import { Config } from "../config/config" | | 9 | +import { mergeDeep } from "remeda" | 9 | 10 |
| 10 | 11 | export namespace Format { | 11 | 12 | const log = Log.create({ service: "format" }) | 12 | 13 |
| 13 | | - const state = App.state("format", () => { | | 14 | + const state = App.state("format", async () => { | 14 | 15 | 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 | + } | 15 | 33 |
| 16 | 34 | return { | 17 | 35 | enabled, | | 36 | + formatters, | 18 | 37 | } | 19 | 38 | }) | 20 | 39 |
| 21 | 40 | async function isEnabled(item: Formatter.Info) { | 22 | | - const s = state() | | 41 | + const s = await state() | 23 | 42 | let status = s.enabled[item.name] | 24 | 43 | if (status === undefined) { | 25 | 44 | status = await item.enabled() | @@ -29,11 +48,11 @@ export namespace Format { | 29 | 48 | } | 30 | 49 |
| 31 | 50 | async function getFormatter(ext: string) { | 32 | | - const cfg = await Config.get() | | 51 | + const formatters = await state().then((x) => x.formatters) | 33 | 52 | 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 }) | 35 | 55 | if (!item.extensions.includes(ext)) continue | 36 | | - if (cfg.formatter?.[item.name]?.disabled) continue | 37 | 56 | if (!(await isEnabled(item))) continue | 38 | 57 | result.push(item) | 39 | 58 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/tool/edit.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -96,6 +96,7 @@ export const EditTool = Tool.define("edit", { | 96 | 96 | file: filePath, | 97 | 97 | }) | 98 | 98 | contentNew = await file.text() | | 99 | + diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew)) | 99 | 100 | })() | 100 | 101 |
| 101 | 102 | FileTime.read(ctx.sessionID, filePath) | |
|