Add support for R formatter in formatters (#3918)

Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
This commit is contained in:
Kyle F Butts
2025-11-04 18:18:33 -06:00
committed by GitHub
parent f51bd91af4
commit 04546c0873
4 changed files with 49 additions and 2 deletions

View File

@@ -8,7 +8,10 @@ import { readableStreamToText } from "bun"
export namespace BunProc {
const log = Log.create({ service: "bun" })
export async function run(cmd: string[], options?: Bun.SpawnOptions.OptionsObject<any, any, any>) {
export async function run(
cmd: string[],
options?: Bun.SpawnOptions.OptionsObject<any, any, any>,
) {
log.info("running", {
cmd: [which(), ...cmd],
...options,

View File

@@ -1,3 +1,4 @@
import { readableStreamToText } from "bun"
import { BunProc } from "../bun"
import { Instance } from "../project/instance"
import { Filesystem } from "../util/filesystem"
@@ -131,7 +132,21 @@ export const zig: Info = {
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"],
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)
return items.length > 0
@@ -177,6 +192,33 @@ export const ruff: Info = {
},
}
export const rlang: Info = {
name: "air",
command: ["air", "format", "$FILE"],
extensions: [".R"],
async enabled() {
const airPath = Bun.which("air")
if (airPath == null) return false
try {
const proc = Bun.spawn(["air", "--help"], {
stdout: "pipe",
stderr: "pipe",
})
await proc.exited
const output = await readableStreamToText(proc.stdout)
// Check for "Air: An R language server and formatter"
const firstLine = output.split("\n")[0]
const hasR = firstLine.includes("R language")
const hasFormatter = firstLine.includes("formatter")
return hasR && hasFormatter
} catch (error) {
return false
}
},
}
export const uvformat: Info = {
name: "uv format",
command: ["uv", "format", "--", "$FILE"],

View File

@@ -69,6 +69,7 @@ export namespace Format {
log.info("checking", { name: item.name, ext })
if (!item.extensions.includes(ext)) continue
if (!(await isEnabled(item))) continue
log.info("enabled", { name: item.name, ext })
result.push(item)
}
return result

View File

@@ -25,6 +25,7 @@ OpenCode comes with several built-in formatters for popular languages and framew
| rubocop | .rb, .rake, .gemspec, .ru | `rubocop` command available |
| standardrb | .rb, .rake, .gemspec, .ru | `standardrb` command available |
| htmlbeautifier | .erb, .html.erb | `htmlbeautifier` command available |
| air | .R | `air` command available |
So if your project has `prettier` in your `package.json`, OpenCode will automatically use it.