mirror of
https://github.com/aljazceru/opencode.git
synced 2026-01-06 17:34:58 +01:00
formatter config
This commit is contained in:
@@ -19,6 +19,12 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"formatter": {
|
||||||
|
"test": {
|
||||||
|
"extensions": [".json"],
|
||||||
|
"command": ["sed", "-i", "s/name/poop/g", "$FILE"]
|
||||||
|
}
|
||||||
|
},
|
||||||
"mcp": {
|
"mcp": {
|
||||||
"context7": {
|
"context7": {
|
||||||
"type": "remote",
|
"type": "remote",
|
||||||
|
|||||||
@@ -283,6 +283,9 @@ export namespace Config {
|
|||||||
z.string(),
|
z.string(),
|
||||||
z.object({
|
z.object({
|
||||||
disabled: z.boolean().optional(),
|
disabled: z.boolean().optional(),
|
||||||
|
command: z.array(z.string()).optional(),
|
||||||
|
environment: z.record(z.string(), z.string()).optional(),
|
||||||
|
extensions: z.array(z.string()).optional(),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.optional(),
|
.optional(),
|
||||||
|
|||||||
@@ -129,7 +129,9 @@ export const clang: Info = {
|
|||||||
command: ["clang-format", "-i", "$FILE"],
|
command: ["clang-format", "-i", "$FILE"],
|
||||||
extensions: [".c", ".cc", ".cpp", ".cxx", ".c++", ".h", ".hh", ".hpp", ".hxx", ".h++", ".ino", ".C", ".H"],
|
extensions: [".c", ".cc", ".cpp", ".cxx", ".c++", ".h", ".hh", ".hpp", ".hxx", ".h++", ".ino", ".C", ".H"],
|
||||||
async enabled() {
|
async enabled() {
|
||||||
return Bun.which("clang-format") !== null
|
const app = App.info()
|
||||||
|
const items = await Filesystem.findUp(".clang-format", app.path.cwd, app.path.root)
|
||||||
|
return items.length > 0
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,20 +6,39 @@ import path from "path"
|
|||||||
|
|
||||||
import * as Formatter from "./formatter"
|
import * as Formatter from "./formatter"
|
||||||
import { Config } from "../config/config"
|
import { Config } from "../config/config"
|
||||||
|
import { mergeDeep } from "remeda"
|
||||||
|
|
||||||
export namespace Format {
|
export namespace Format {
|
||||||
const log = Log.create({ service: "format" })
|
const log = Log.create({ service: "format" })
|
||||||
|
|
||||||
const state = App.state("format", () => {
|
const state = App.state("format", async () => {
|
||||||
const enabled: Record<string, boolean> = {}
|
const enabled: Record<string, boolean> = {}
|
||||||
|
const cfg = await Config.get()
|
||||||
|
|
||||||
|
const formatters = { ...Formatter } as Record<string, Formatter.Info>
|
||||||
|
for (const [name, item] of Object.entries(cfg.formatter ?? {})) {
|
||||||
|
if (item.disabled) {
|
||||||
|
delete formatters[name]
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const result: Formatter.Info = mergeDeep(formatters[name] ?? {}, {
|
||||||
|
command: [],
|
||||||
|
extensions: [],
|
||||||
|
...item,
|
||||||
|
})
|
||||||
|
result.enabled = async () => true
|
||||||
|
result.name = name
|
||||||
|
formatters[name] = result
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
enabled,
|
enabled,
|
||||||
|
formatters,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
async function isEnabled(item: Formatter.Info) {
|
async function isEnabled(item: Formatter.Info) {
|
||||||
const s = state()
|
const s = await state()
|
||||||
let status = s.enabled[item.name]
|
let status = s.enabled[item.name]
|
||||||
if (status === undefined) {
|
if (status === undefined) {
|
||||||
status = await item.enabled()
|
status = await item.enabled()
|
||||||
@@ -29,11 +48,11 @@ export namespace Format {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getFormatter(ext: string) {
|
async function getFormatter(ext: string) {
|
||||||
const cfg = await Config.get()
|
const formatters = await state().then((x) => x.formatters)
|
||||||
const result = []
|
const result = []
|
||||||
for (const item of Object.values(Formatter)) {
|
for (const item of Object.values(formatters)) {
|
||||||
|
log.info("checking", { name: item.name, ext })
|
||||||
if (!item.extensions.includes(ext)) continue
|
if (!item.extensions.includes(ext)) continue
|
||||||
if (cfg.formatter?.[item.name]?.disabled) continue
|
|
||||||
if (!(await isEnabled(item))) continue
|
if (!(await isEnabled(item))) continue
|
||||||
result.push(item)
|
result.push(item)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ export const EditTool = Tool.define("edit", {
|
|||||||
file: filePath,
|
file: filePath,
|
||||||
})
|
})
|
||||||
contentNew = await file.text()
|
contentNew = await file.text()
|
||||||
|
diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew))
|
||||||
})()
|
})()
|
||||||
|
|
||||||
FileTime.read(ctx.sessionID, filePath)
|
FileTime.read(ctx.sessionID, filePath)
|
||||||
|
|||||||
Reference in New Issue
Block a user