mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-21 04:12:08 +03:00
fix(core): merge formatter config across files (#52739)
This commit is contained in:
committed by
GitHub
Unverified
parent
e2a700dc90
commit
77eceb8c85
@@ -1,6 +1,8 @@
|
||||
export * as ConfigFormatterPlugin from "./formatter.js"
|
||||
|
||||
import { define } from "@opencode/plugin/effect/plugin"
|
||||
import type { Entry } from "@opencode/schema/config"
|
||||
import type { ConfigFormatter } from "@opencode/schema/config/formatter"
|
||||
import { FSUtil } from "@opencode/util/fs-util"
|
||||
import { Global } from "@opencode/util/global"
|
||||
import { Npm } from "@opencode/util/npm"
|
||||
@@ -25,7 +27,7 @@ export const Plugin = define({
|
||||
const loaded = yield* ConfigEntryObserver.observe(config, ctx.event, formatter.reload())
|
||||
|
||||
yield* formatter.transform((editor) => {
|
||||
const configured = Config.latest(loaded.entries, "formatter")
|
||||
const configured = resolve(loaded.entries)
|
||||
if (!configured) return
|
||||
const builtIns = make({
|
||||
directory: location.directory,
|
||||
@@ -56,3 +58,19 @@ export const Plugin = define({
|
||||
})
|
||||
}),
|
||||
})
|
||||
|
||||
// Documents apply from lowest to highest priority. A boolean resets everything before it; an object
|
||||
// merges each named formatter's supplied fields over the same name from earlier objects.
|
||||
function resolve(entries: readonly Entry[]) {
|
||||
return entries.reduce<boolean | Record<string, ConfigFormatter.Entry> | undefined>((result, entry) => {
|
||||
if (entry.type !== "document" || entry.info.formatter === undefined) return result
|
||||
if (typeof entry.info.formatter === "boolean") return entry.info.formatter
|
||||
return Object.entries(entry.info.formatter).reduce(
|
||||
(merged, [name, item]) => ({
|
||||
...merged,
|
||||
[name]: { ...merged[name], ...item, environment: { ...merged[name]?.environment, ...item.environment } },
|
||||
}),
|
||||
typeof result === "object" ? result : {},
|
||||
)
|
||||
}, undefined)
|
||||
}
|
||||
|
||||
@@ -20,10 +20,10 @@ import { tmpdir } from "./fixture/tmpdir"
|
||||
import { testEffect } from "./lib/effect"
|
||||
|
||||
const it = testEffect(
|
||||
AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node, SdkPlugins.node, LocationServiceMap.node]), [
|
||||
Global.node.replace(tempGlobalLayer),
|
||||
offlineModels,
|
||||
]),
|
||||
AppNodeBuilder.build(
|
||||
LayerNode.group([Database.node, Bus.node, SdkPlugins.node, LocationServiceMap.node, Global.node]),
|
||||
[Global.node.replace(tempGlobalLayer), offlineModels],
|
||||
),
|
||||
)
|
||||
type ConfigInput = typeof Info.Encoded
|
||||
|
||||
@@ -59,7 +59,117 @@ function withFormatter<A, E, R>(
|
||||
)
|
||||
}
|
||||
|
||||
function append(expression: string) {
|
||||
return [process.execPath, "-e", `require('fs').appendFileSync(process.argv.at(-1), ${expression})`, "$FILE"]
|
||||
}
|
||||
|
||||
const markdown = { command: append("'md'"), extensions: [".md"] }
|
||||
const project = { command: append("'project'"), extensions: [".project"] }
|
||||
|
||||
// Writes formatter config layers from lowest to highest priority: global, project, then project .opencode.
|
||||
function withLayers<A, E, R>(
|
||||
layers: ConfigInput["formatter"][],
|
||||
body: (formatter: Formatter.Interface, directory: string) => Effect.Effect<A, E, R>,
|
||||
) {
|
||||
return withTemp((directory) =>
|
||||
Effect.gen(function* () {
|
||||
const global = yield* Global.Service
|
||||
yield* Effect.promise(async () => {
|
||||
await fs.mkdir(path.join(directory, ".opencode"))
|
||||
const files = [
|
||||
path.join(global.config, "opencode.jsonc"),
|
||||
path.join(directory, "opencode.json"),
|
||||
path.join(directory, ".opencode", "opencode.jsonc"),
|
||||
]
|
||||
await Promise.all(layers.map((formatter, index) => fs.writeFile(files[index], JSON.stringify({ formatter }))))
|
||||
})
|
||||
return yield* Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
yield* plugins.awaitActivation
|
||||
return yield* body(yield* Formatter.Service, directory)
|
||||
}).pipe(
|
||||
Effect.scoped,
|
||||
Effect.provide(LocationServiceMap.Service.get(Location.Ref.make({ directory: AbsolutePath.make(directory) }))),
|
||||
)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
const layered: { name: string; layers: ConfigInput["formatter"][]; expected: Record<string, string | false> }[] = [
|
||||
{
|
||||
name: "keeps lower formatters when a higher config adds another",
|
||||
layers: [{ markdown }, undefined, { project }],
|
||||
expected: { "test.md": "md", "test.project": "project" },
|
||||
},
|
||||
{
|
||||
name: "inherits omitted fields and replaces supplied arrays",
|
||||
layers: [
|
||||
{ markdown: { command: append("'A'"), extensions: [".md", ".project"] } },
|
||||
{ markdown: { command: append("'B'"), extensions: [".project"] } },
|
||||
],
|
||||
expected: { "test.md": false, "test.project": "B" },
|
||||
},
|
||||
{
|
||||
name: "merges environment variables by key",
|
||||
layers: [
|
||||
{
|
||||
markdown: {
|
||||
...markdown,
|
||||
command: append("process.env.FIRST + process.env.SECOND"),
|
||||
environment: { FIRST: "global", SECOND: "global" },
|
||||
},
|
||||
},
|
||||
{ markdown: { environment: { SECOND: "project" } } },
|
||||
],
|
||||
expected: { "test.md": "globalproject" },
|
||||
},
|
||||
{
|
||||
name: "keeps a lower disable through overrides that omit it",
|
||||
layers: [{ markdown: { ...markdown, disabled: true } }, { markdown: { command: append("'override'") }, project }],
|
||||
expected: { "test.md": false, "test.project": "project" },
|
||||
},
|
||||
{
|
||||
name: "re-enables a lower disabled formatter",
|
||||
layers: [{ markdown: { ...markdown, disabled: true } }, { markdown: { disabled: false } }],
|
||||
expected: { "test.md": "md" },
|
||||
},
|
||||
{
|
||||
name: "keeps lower formatters through an empty object",
|
||||
layers: [{ markdown }, {}],
|
||||
expected: { "test.md": "md" },
|
||||
},
|
||||
{
|
||||
name: "clears lower formatters when a higher config is false",
|
||||
layers: [{ markdown }, false],
|
||||
expected: { "test.md": false },
|
||||
},
|
||||
{
|
||||
name: "clears lower formatters before a later object when a higher config is true",
|
||||
layers: [{ markdown }, true, { project }],
|
||||
expected: { "test.md": false, "test.project": "project" },
|
||||
},
|
||||
{
|
||||
name: "enables only formatters configured after false",
|
||||
layers: [{ markdown }, false, { project }],
|
||||
expected: { "test.md": false, "test.project": "project" },
|
||||
},
|
||||
]
|
||||
|
||||
describe("Formatter", () => {
|
||||
layered.forEach((entry) =>
|
||||
it.live(entry.name, () =>
|
||||
withLayers(entry.layers, (formatter, directory) =>
|
||||
Effect.forEach(Object.entries(entry.expected), ([name, expected]) =>
|
||||
Effect.gen(function* () {
|
||||
const file = path.join(directory, name)
|
||||
yield* Effect.promise(() => fs.writeFile(file, ""))
|
||||
const formatted = yield* formatter.file(file)
|
||||
expect(formatted && (yield* Effect.promise(() => fs.readFile(file, "utf8")))).toBe(expected)
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
;[
|
||||
{ file: "test.match", extension: ".match", matches: true },
|
||||
{ file: "test.other", extension: ".match", matches: false },
|
||||
|
||||
@@ -26,7 +26,8 @@ available.
|
||||
```
|
||||
|
||||
An object also enables the built-ins and lets you override them or add custom
|
||||
formatters. An empty object is therefore equivalent to `true`.
|
||||
formatters. An empty object preserves inherited formatter settings, while `true`
|
||||
resets them to the built-ins.
|
||||
|
||||
```jsonc title="opencode.jsonc"
|
||||
{
|
||||
@@ -177,8 +178,8 @@ OpenCode logs the failure and tries the next match.
|
||||
|
||||
## Disable
|
||||
|
||||
Omit `formatter` or set it to `false` to disable all formatting. An explicit
|
||||
`false` can override a lower-priority configuration that enabled formatters.
|
||||
Formatting is disabled when no config file sets `formatter`. To disable
|
||||
formatting that a lower-priority file enabled, set it to `false`.
|
||||
|
||||
```jsonc title="opencode.jsonc"
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user