refactor: pass formatter instance context explicitly (#23020)
This commit is contained in:
@@ -1,15 +1,17 @@
|
|||||||
import { Npm } from "../npm"
|
import { Npm } from "../npm"
|
||||||
import { Instance } from "../project/instance"
|
import type { InstanceContext } from "../project/instance"
|
||||||
import { Filesystem } from "../util"
|
import { Filesystem } from "../util"
|
||||||
import { Process } from "../util"
|
import { Process } from "../util"
|
||||||
import { which } from "../util/which"
|
import { which } from "../util/which"
|
||||||
import { Flag } from "@/flag/flag"
|
import { Flag } from "@/flag/flag"
|
||||||
|
|
||||||
|
export interface Context extends Pick<InstanceContext, "directory" | "worktree"> {}
|
||||||
|
|
||||||
export interface Info {
|
export interface Info {
|
||||||
name: string
|
name: string
|
||||||
environment?: Record<string, string>
|
environment?: Record<string, string>
|
||||||
extensions: string[]
|
extensions: string[]
|
||||||
enabled(): Promise<string[] | false>
|
enabled(context: Context): Promise<string[] | false>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const gofmt: Info = {
|
export const gofmt: Info = {
|
||||||
@@ -65,8 +67,8 @@ export const prettier: Info = {
|
|||||||
".graphql",
|
".graphql",
|
||||||
".gql",
|
".gql",
|
||||||
],
|
],
|
||||||
async enabled() {
|
async enabled(context) {
|
||||||
const items = await Filesystem.findUp("package.json", Instance.directory, Instance.worktree)
|
const items = await Filesystem.findUp("package.json", context.directory, context.worktree)
|
||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
const json = await Filesystem.readJson<{
|
const json = await Filesystem.readJson<{
|
||||||
dependencies?: Record<string, string>
|
dependencies?: Record<string, string>
|
||||||
@@ -87,9 +89,9 @@ export const oxfmt: Info = {
|
|||||||
BUN_BE_BUN: "1",
|
BUN_BE_BUN: "1",
|
||||||
},
|
},
|
||||||
extensions: [".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".mts", ".cts"],
|
extensions: [".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".mts", ".cts"],
|
||||||
async enabled() {
|
async enabled(context) {
|
||||||
if (!Flag.OPENCODE_EXPERIMENTAL_OXFMT) return false
|
if (!Flag.OPENCODE_EXPERIMENTAL_OXFMT) return false
|
||||||
const items = await Filesystem.findUp("package.json", Instance.directory, Instance.worktree)
|
const items = await Filesystem.findUp("package.json", context.directory, context.worktree)
|
||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
const json = await Filesystem.readJson<{
|
const json = await Filesystem.readJson<{
|
||||||
dependencies?: Record<string, string>
|
dependencies?: Record<string, string>
|
||||||
@@ -137,10 +139,10 @@ export const biome: Info = {
|
|||||||
".graphql",
|
".graphql",
|
||||||
".gql",
|
".gql",
|
||||||
],
|
],
|
||||||
async enabled() {
|
async enabled(context) {
|
||||||
const configs = ["biome.json", "biome.jsonc"]
|
const configs = ["biome.json", "biome.jsonc"]
|
||||||
for (const config of configs) {
|
for (const config of configs) {
|
||||||
const found = await Filesystem.findUp(config, Instance.directory, Instance.worktree)
|
const found = await Filesystem.findUp(config, context.directory, context.worktree)
|
||||||
if (found.length > 0) {
|
if (found.length > 0) {
|
||||||
const bin = await Npm.which("@biomejs/biome")
|
const bin = await Npm.which("@biomejs/biome")
|
||||||
if (bin) return [bin, "format", "--write", "$FILE"]
|
if (bin) return [bin, "format", "--write", "$FILE"]
|
||||||
@@ -163,8 +165,8 @@ export const zig: Info = {
|
|||||||
export const clang: Info = {
|
export const clang: Info = {
|
||||||
name: "clang-format",
|
name: "clang-format",
|
||||||
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(context) {
|
||||||
const items = await Filesystem.findUp(".clang-format", Instance.directory, Instance.worktree)
|
const items = await Filesystem.findUp(".clang-format", context.directory, context.worktree)
|
||||||
if (items.length > 0) {
|
if (items.length > 0) {
|
||||||
const match = which("clang-format")
|
const match = which("clang-format")
|
||||||
if (match) return [match, "-i", "$FILE"]
|
if (match) return [match, "-i", "$FILE"]
|
||||||
@@ -186,11 +188,11 @@ export const ktlint: Info = {
|
|||||||
export const ruff: Info = {
|
export const ruff: Info = {
|
||||||
name: "ruff",
|
name: "ruff",
|
||||||
extensions: [".py", ".pyi"],
|
extensions: [".py", ".pyi"],
|
||||||
async enabled() {
|
async enabled(context) {
|
||||||
if (!which("ruff")) return false
|
if (!which("ruff")) return false
|
||||||
const configs = ["pyproject.toml", "ruff.toml", ".ruff.toml"]
|
const configs = ["pyproject.toml", "ruff.toml", ".ruff.toml"]
|
||||||
for (const config of configs) {
|
for (const config of configs) {
|
||||||
const found = await Filesystem.findUp(config, Instance.directory, Instance.worktree)
|
const found = await Filesystem.findUp(config, context.directory, context.worktree)
|
||||||
if (found.length > 0) {
|
if (found.length > 0) {
|
||||||
if (config === "pyproject.toml") {
|
if (config === "pyproject.toml") {
|
||||||
const content = await Filesystem.readText(found[0])
|
const content = await Filesystem.readText(found[0])
|
||||||
@@ -202,7 +204,7 @@ export const ruff: Info = {
|
|||||||
}
|
}
|
||||||
const deps = ["requirements.txt", "pyproject.toml", "Pipfile"]
|
const deps = ["requirements.txt", "pyproject.toml", "Pipfile"]
|
||||||
for (const dep of deps) {
|
for (const dep of deps) {
|
||||||
const found = await Filesystem.findUp(dep, Instance.directory, Instance.worktree)
|
const found = await Filesystem.findUp(dep, context.directory, context.worktree)
|
||||||
if (found.length > 0) {
|
if (found.length > 0) {
|
||||||
const content = await Filesystem.readText(found[0])
|
const content = await Filesystem.readText(found[0])
|
||||||
if (content.includes("ruff")) return ["ruff", "format", "$FILE"]
|
if (content.includes("ruff")) return ["ruff", "format", "$FILE"]
|
||||||
@@ -233,8 +235,8 @@ export const rlang: Info = {
|
|||||||
export const uvformat: Info = {
|
export const uvformat: Info = {
|
||||||
name: "uv",
|
name: "uv",
|
||||||
extensions: [".py", ".pyi"],
|
extensions: [".py", ".pyi"],
|
||||||
async enabled() {
|
async enabled(context) {
|
||||||
if (await ruff.enabled()) return false
|
if (await ruff.enabled(context)) return false
|
||||||
const uv = which("uv")
|
const uv = which("uv")
|
||||||
if (uv == null) return false
|
if (uv == null) return false
|
||||||
const output = await Process.run([uv, "format", "--help"], { nothrow: true })
|
const output = await Process.run([uv, "format", "--help"], { nothrow: true })
|
||||||
@@ -286,9 +288,9 @@ export const dart: Info = {
|
|||||||
export const ocamlformat: Info = {
|
export const ocamlformat: Info = {
|
||||||
name: "ocamlformat",
|
name: "ocamlformat",
|
||||||
extensions: [".ml", ".mli"],
|
extensions: [".ml", ".mli"],
|
||||||
async enabled() {
|
async enabled(context) {
|
||||||
if (!which("ocamlformat")) return false
|
if (!which("ocamlformat")) return false
|
||||||
const items = await Filesystem.findUp(".ocamlformat", Instance.directory, Instance.worktree)
|
const items = await Filesystem.findUp(".ocamlformat", context.directory, context.worktree)
|
||||||
if (items.length > 0) return ["ocamlformat", "-i", "$FILE"]
|
if (items.length > 0) return ["ocamlformat", "-i", "$FILE"]
|
||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
@@ -357,8 +359,8 @@ export const rustfmt: Info = {
|
|||||||
export const pint: Info = {
|
export const pint: Info = {
|
||||||
name: "pint",
|
name: "pint",
|
||||||
extensions: [".php"],
|
extensions: [".php"],
|
||||||
async enabled() {
|
async enabled(context) {
|
||||||
const items = await Filesystem.findUp("composer.json", Instance.directory, Instance.worktree)
|
const items = await Filesystem.findUp("composer.json", context.directory, context.worktree)
|
||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
const json = await Filesystem.readJson<{
|
const json = await Filesystem.readJson<{
|
||||||
require?: Record<string, string>
|
require?: Record<string, string>
|
||||||
|
|||||||
@@ -37,14 +37,14 @@ export const layer = Layer.effect(
|
|||||||
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
|
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
|
||||||
|
|
||||||
const state = yield* InstanceState.make(
|
const state = yield* InstanceState.make(
|
||||||
Effect.fn("Format.state")(function* (_ctx) {
|
Effect.fn("Format.state")(function* (ctx) {
|
||||||
const commands: Record<string, string[] | false> = {}
|
const commands: Record<string, string[] | false> = {}
|
||||||
const formatters: Record<string, Formatter.Info> = {}
|
const formatters: Record<string, Formatter.Info> = {}
|
||||||
|
|
||||||
async function getCommand(item: Formatter.Info) {
|
async function getCommand(item: Formatter.Info) {
|
||||||
let cmd = commands[item.name]
|
let cmd = commands[item.name]
|
||||||
if (cmd === false || cmd === undefined) {
|
if (cmd === false || cmd === undefined) {
|
||||||
cmd = await item.enabled()
|
cmd = await item.enabled(ctx)
|
||||||
commands[item.name] = cmd
|
commands[item.name] = cmd
|
||||||
}
|
}
|
||||||
return cmd
|
return cmd
|
||||||
@@ -153,7 +153,7 @@ export const layer = Layer.effect(
|
|||||||
...info,
|
...info,
|
||||||
name,
|
name,
|
||||||
extensions: info.extensions ?? [],
|
extensions: info.extensions ?? [],
|
||||||
enabled: builtIn && !info.command ? builtIn.enabled : async () => info.command ?? false,
|
enabled: builtIn && !info.command ? builtIn.enabled : async (_context) => info.command ?? false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user