From 1fdb326aa7f7c628ca9eef002853404aaba9becb Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Sun, 29 Jun 2025 21:30:23 -0400 Subject: ignore: refactoring --- packages/opencode/src/format/definition.ts | 7 -- packages/opencode/src/format/formatter.ts | 133 +++++++++++++++++++++++++++++ packages/opencode/src/format/formatters.ts | 126 --------------------------- packages/opencode/src/format/index.ts | 8 +- 4 files changed, 136 insertions(+), 138 deletions(-) delete mode 100644 packages/opencode/src/format/definition.ts create mode 100644 packages/opencode/src/format/formatter.ts delete mode 100644 packages/opencode/src/format/formatters.ts (limited to 'packages') diff --git a/packages/opencode/src/format/definition.ts b/packages/opencode/src/format/definition.ts deleted file mode 100644 index afc45df88..000000000 --- a/packages/opencode/src/format/definition.ts +++ /dev/null @@ -1,7 +0,0 @@ -export interface Definition { - name: string - command: string[] - environment?: Record - extensions: string[] - enabled(): Promise -} diff --git a/packages/opencode/src/format/formatter.ts b/packages/opencode/src/format/formatter.ts new file mode 100644 index 000000000..dd0d9279c --- /dev/null +++ b/packages/opencode/src/format/formatter.ts @@ -0,0 +1,133 @@ +import { App } from "../app/app" +import { BunProc } from "../bun" + +export interface Info { + name: string + command: string[] + environment?: Record + extensions: string[] + enabled(): Promise +} + +export const gofmt: Info = { + name: "gofmt", + command: ["gofmt", "-w", "$FILE"], + extensions: [".go"], + async enabled() { + return Bun.which("gofmt") !== null + }, +} + +export const mix: Info = { + name: "mix", + command: ["mix", "format", "$FILE"], + extensions: [".ex", ".exs", ".eex", ".heex", ".leex", ".neex", ".sface"], + async enabled() { + return Bun.which("mix") !== null + }, +} + +export const prettier: Info = { + name: "prettier", + command: [BunProc.which(), "run", "prettier", "--write", "$FILE"], + environment: { + BUN_BE_BUN: "1", + }, + extensions: [ + ".js", + ".jsx", + ".mjs", + ".cjs", + ".ts", + ".tsx", + ".mts", + ".cts", + ".html", + ".htm", + ".css", + ".scss", + ".sass", + ".less", + ".vue", + ".svelte", + ".json", + ".jsonc", + ".yaml", + ".yml", + ".toml", + ".xml", + ".md", + ".mdx", + ".graphql", + ".gql", + ], + async enabled() { + // this is more complicated because we only want to use prettier if it's + // being used with the current project + try { + const proc = Bun.spawn({ + cmd: [BunProc.which(), "run", "prettier", "--version"], + cwd: App.info().path.cwd, + env: { + BUN_BE_BUN: "1", + }, + stdout: "ignore", + stderr: "ignore", + }) + const exit = await proc.exited + return exit === 0 + } catch { + return false + } + }, +} + +export const zig: Info = { + name: "zig", + command: ["zig", "fmt", "$FILE"], + extensions: [".zig", ".zon"], + async enabled() { + return Bun.which("zig") !== null + }, +} + +export const clang: Info = { + name: "clang-format", + command: ["clang-format", "-i", "$FILE"], + extensions: [ + ".c", + ".cc", + ".cpp", + ".cxx", + ".c++", + ".h", + ".hh", + ".hpp", + ".hxx", + ".h++", + ".ino", + ".C", + ".H", + ], + async enabled() { + return Bun.which("clang-format") !== null + }, +} + +export const ktlint: Info = { + name: "ktlint", + command: ["ktlint", "-F", "$FILE"], + extensions: [".kt", ".kts"], + async enabled() { + return Bun.which("ktlint") !== null + }, +} + +export const ruff: Info = { + name: "ruff", + command: ["ruff", "format", "$FILE"], + extensions: [".py", ".pyi"], + async enabled() { + return Bun.which("ruff") !== null + }, +} diff --git a/packages/opencode/src/format/formatters.ts b/packages/opencode/src/format/formatters.ts deleted file mode 100644 index e1235282b..000000000 --- a/packages/opencode/src/format/formatters.ts +++ /dev/null @@ -1,126 +0,0 @@ -import { App } from "../app/app" -import { BunProc } from "../bun" -import type { Definition } from "./definition" - -export const gofmt: Definition = { - name: "gofmt", - command: ["gofmt", "-w", "$FILE"], - extensions: [".go"], - async enabled() { - return Bun.which("gofmt") !== null - }, -} - -export const mix: Definition = { - name: "mix", - command: ["mix", "format", "$FILE"], - extensions: [".ex", ".exs", ".eex", ".heex", ".leex", ".neex", ".sface"], - async enabled() { - return Bun.which("mix") !== null - }, -} - -export const prettier: Definition = { - name: "prettier", - command: [BunProc.which(), "run", "prettier", "--write", "$FILE"], - environment: { - BUN_BE_BUN: "1", - }, - extensions: [ - ".js", - ".jsx", - ".mjs", - ".cjs", - ".ts", - ".tsx", - ".mts", - ".cts", - ".html", - ".htm", - ".css", - ".scss", - ".sass", - ".less", - ".vue", - ".svelte", - ".json", - ".jsonc", - ".yaml", - ".yml", - ".toml", - ".xml", - ".md", - ".mdx", - ".graphql", - ".gql", - ], - async enabled() { - // this is more complicated because we only want to use prettier if it's - // being used with the current project - try { - const proc = Bun.spawn({ - cmd: [BunProc.which(), "run", "prettier", "--version"], - cwd: App.info().path.cwd, - env: { - BUN_BE_BUN: "1", - }, - stdout: "ignore", - stderr: "ignore", - }) - const exit = await proc.exited - return exit === 0 - } catch { - return false - } - }, -} - -export const zig: Definition = { - name: "zig", - command: ["zig", "fmt", "$FILE"], - extensions: [".zig", ".zon"], - async enabled() { - return Bun.which("zig") !== null - }, -} - -export const clang: Definition = { - name: "clang-format", - command: ["clang-format", "-i", "$FILE"], - extensions: [ - ".c", - ".cc", - ".cpp", - ".cxx", - ".c++", - ".h", - ".hh", - ".hpp", - ".hxx", - ".h++", - ".ino", - ".C", - ".H", - ], - async enabled() { - return Bun.which("clang-format") !== null - }, -} - -export const ktlint: Definition = { - name: "ktlint", - command: ["ktlint", "-F", "$FILE"], - extensions: [".kt", ".kts"], - async enabled() { - return Bun.which("ktlint") !== null - }, -} - -export const ruff: Definition = { - name: "ruff", - command: ["ruff", "format", "$FILE"], - extensions: [".py", ".pyi"], - async enabled() { - return Bun.which("ruff") !== null - }, -} diff --git a/packages/opencode/src/format/index.ts b/packages/opencode/src/format/index.ts index 957698988..78cc6b926 100644 --- a/packages/opencode/src/format/index.ts +++ b/packages/opencode/src/format/index.ts @@ -4,9 +4,7 @@ import { File } from "../file" import { Log } from "../util/log" import path from "path" -import type { Definition } from "./definition" - -import * as Formatters from "./formatters" +import * as Formatter from "./formatter" export namespace Format { const log = Log.create({ service: "format" }) @@ -19,7 +17,7 @@ export namespace Format { } }) - async function isEnabled(item: Definition) { + async function isEnabled(item: Formatter.Info) { const s = state() let status = s.enabled[item.name] if (status === undefined) { @@ -31,7 +29,7 @@ export namespace Format { async function getFormatter(ext: string) { const result = [] - for (const item of Object.values(Formatters)) { + for (const item of Object.values(Formatter)) { if (!item.extensions.includes(ext)) continue if (!isEnabled(item)) continue result.push(item) -- cgit v1.2.3