|
|
@@ -1,15 +1,17 @@
|
|
|
import { Npm } from "../npm"
|
|
|
-import { Instance } from "../project/instance"
|
|
|
+import type { InstanceContext } from "../project/instance"
|
|
|
import { Filesystem } from "../util"
|
|
|
import { Process } from "../util"
|
|
|
import { which } from "../util/which"
|
|
|
import { Flag } from "@/flag/flag"
|
|
|
|
|
|
+export interface Context extends Pick<InstanceContext, "directory" | "worktree"> {}
|
|
|
+
|
|
|
export interface Info {
|
|
|
name: string
|
|
|
environment?: Record<string, string>
|
|
|
extensions: string[]
|
|
|
- enabled(): Promise<string[] | false>
|
|
|
+ enabled(context: Context): Promise<string[] | false>
|
|
|
}
|
|
|
|
|
|
export const gofmt: Info = {
|
|
|
@@ -65,8 +67,8 @@ export const prettier: Info = {
|
|
|
".graphql",
|
|
|
".gql",
|
|
|
],
|
|
|
- async enabled() {
|
|
|
- const items = await Filesystem.findUp("package.json", Instance.directory, Instance.worktree)
|
|
|
+ async enabled(context) {
|
|
|
+ const items = await Filesystem.findUp("package.json", context.directory, context.worktree)
|
|
|
for (const item of items) {
|
|
|
const json = await Filesystem.readJson<{
|
|
|
dependencies?: Record<string, string>
|
|
|
@@ -87,9 +89,9 @@ export const oxfmt: Info = {
|
|
|
BUN_BE_BUN: "1",
|
|
|
},
|
|
|
extensions: [".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".mts", ".cts"],
|
|
|
- async enabled() {
|
|
|
+ async enabled(context) {
|
|
|
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) {
|
|
|
const json = await Filesystem.readJson<{
|
|
|
dependencies?: Record<string, string>
|
|
|
@@ -137,10 +139,10 @@ export const biome: Info = {
|
|
|
".graphql",
|
|
|
".gql",
|
|
|
],
|
|
|
- async enabled() {
|
|
|
+ async enabled(context) {
|
|
|
const configs = ["biome.json", "biome.jsonc"]
|
|
|
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) {
|
|
|
const bin = await Npm.which("@biomejs/biome")
|
|
|
if (bin) return [bin, "format", "--write", "$FILE"]
|
|
|
@@ -163,8 +165,8 @@ export const zig: Info = {
|
|
|
export const clang: Info = {
|
|
|
name: "clang-format",
|
|
|
extensions: [".c", ".cc", ".cpp", ".cxx", ".c++", ".h", ".hh", ".hpp", ".hxx", ".h++", ".ino", ".C", ".H"],
|
|
|
- async enabled() {
|
|
|
- const items = await Filesystem.findUp(".clang-format", Instance.directory, Instance.worktree)
|
|
|
+ async enabled(context) {
|
|
|
+ const items = await Filesystem.findUp(".clang-format", context.directory, context.worktree)
|
|
|
if (items.length > 0) {
|
|
|
const match = which("clang-format")
|
|
|
if (match) return [match, "-i", "$FILE"]
|
|
|
@@ -186,11 +188,11 @@ export const ktlint: Info = {
|
|
|
export const ruff: Info = {
|
|
|
name: "ruff",
|
|
|
extensions: [".py", ".pyi"],
|
|
|
- async enabled() {
|
|
|
+ async enabled(context) {
|
|
|
if (!which("ruff")) return false
|
|
|
const configs = ["pyproject.toml", "ruff.toml", ".ruff.toml"]
|
|
|
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 (config === "pyproject.toml") {
|
|
|
const content = await Filesystem.readText(found[0])
|
|
|
@@ -202,7 +204,7 @@ export const ruff: Info = {
|
|
|
}
|
|
|
const deps = ["requirements.txt", "pyproject.toml", "Pipfile"]
|
|
|
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) {
|
|
|
const content = await Filesystem.readText(found[0])
|
|
|
if (content.includes("ruff")) return ["ruff", "format", "$FILE"]
|
|
|
@@ -233,8 +235,8 @@ export const rlang: Info = {
|
|
|
export const uvformat: Info = {
|
|
|
name: "uv",
|
|
|
extensions: [".py", ".pyi"],
|
|
|
- async enabled() {
|
|
|
- if (await ruff.enabled()) return false
|
|
|
+ async enabled(context) {
|
|
|
+ if (await ruff.enabled(context)) return false
|
|
|
const uv = which("uv")
|
|
|
if (uv == null) return false
|
|
|
const output = await Process.run([uv, "format", "--help"], { nothrow: true })
|
|
|
@@ -286,9 +288,9 @@ export const dart: Info = {
|
|
|
export const ocamlformat: Info = {
|
|
|
name: "ocamlformat",
|
|
|
extensions: [".ml", ".mli"],
|
|
|
- async enabled() {
|
|
|
+ async enabled(context) {
|
|
|
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"]
|
|
|
return false
|
|
|
},
|
|
|
@@ -357,8 +359,8 @@ export const rustfmt: Info = {
|
|
|
export const pint: Info = {
|
|
|
name: "pint",
|
|
|
extensions: [".php"],
|
|
|
- async enabled() {
|
|
|
- const items = await Filesystem.findUp("composer.json", Instance.directory, Instance.worktree)
|
|
|
+ async enabled(context) {
|
|
|
+ const items = await Filesystem.findUp("composer.json", context.directory, context.worktree)
|
|
|
for (const item of items) {
|
|
|
const json = await Filesystem.readJson<{
|
|
|
require?: Record<string, string>
|