Skip to content

Commit 72d4875

Browse files
committed
add ruby formatter and lsp
1 parent 986144b commit 72d4875

4 files changed

Lines changed: 133 additions & 68 deletions

File tree

packages/opencode/src/format/formatter.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,30 @@ export const ruff: Info = {
131131
return Bun.which("ruff") !== null
132132
},
133133
}
134+
135+
export const rubocop: Info = {
136+
name: "rubocop",
137+
command: ["rubocop", "--autocorrect", "$FILE"],
138+
extensions: [".rb", ".rake", ".gemspec", ".ru"],
139+
async enabled() {
140+
return Bun.which("rubocop") !== null
141+
},
142+
}
143+
144+
export const standardrb: Info = {
145+
name: "standardrb",
146+
command: ["standardrb", "--fix", "$FILE"],
147+
extensions: [".rb", ".rake", ".gemspec", ".ru"],
148+
async enabled() {
149+
return Bun.which("standardrb") !== null
150+
},
151+
}
152+
153+
export const htmlbeautifier: Info = {
154+
name: "htmlbeautifier",
155+
command: ["htmlbeautifier", "$FILE"],
156+
extensions: [".erb", ".html.erb"],
157+
async enabled() {
158+
return Bun.which("htmlbeautifier") !== null
159+
},
160+
}

packages/opencode/src/lsp/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export namespace LSP {
2828
export async function touchFile(input: string, waitForDiagnostics?: boolean) {
2929
const extension = path.parse(input).ext
3030
const s = await state()
31-
const matches = LSPServer.All.filter((x) =>
31+
const matches = Object.values(LSPServer).filter((x) =>
3232
x.extensions.includes(extension),
3333
)
3434
for (const match of matches) {

packages/opencode/src/lsp/language.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ export const LANGUAGE_EXTENSIONS: Record<string, string> = {
6363
".cshtml": "razor",
6464
".razor": "razor",
6565
".rb": "ruby",
66+
".rake": "ruby",
67+
".gemspec": "ruby",
68+
".ru": "ruby",
69+
".erb": "erb",
70+
".html.erb": "erb",
71+
".js.erb": "erb",
72+
".css.erb": "erb",
73+
".json.erb": "erb",
6674
".rs": "rust",
6775
".scss": "scss",
6876
".sass": "sass",

packages/opencode/src/lsp/server.ts

Lines changed: 97 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -19,78 +19,108 @@ export namespace LSPServer {
1919
spawn(app: App.Info): Promise<Handle | undefined>
2020
}
2121

22-
export const All: Info[] = [
23-
{
24-
id: "typescript",
25-
extensions: [
26-
".ts",
27-
".tsx",
28-
".js",
29-
".jsx",
30-
".mjs",
31-
".cjs",
32-
".mts",
33-
".cts",
34-
],
35-
async spawn(app) {
36-
const tsserver = await Bun.resolve(
37-
"typescript/lib/tsserver.js",
38-
app.path.cwd,
39-
).catch(() => {})
40-
if (!tsserver) return
41-
const proc = spawn(
42-
BunProc.which(),
43-
["x", "typescript-language-server", "--stdio"],
44-
{
45-
env: {
46-
...process.env,
47-
BUN_BE_BUN: "1",
48-
},
22+
export const Typescript: Info = {
23+
id: "typescript",
24+
extensions: [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".mts", ".cts"],
25+
async spawn(app) {
26+
const tsserver = await Bun.resolve(
27+
"typescript/lib/tsserver.js",
28+
app.path.cwd,
29+
).catch(() => {})
30+
if (!tsserver) return
31+
const proc = spawn(
32+
BunProc.which(),
33+
["x", "typescript-language-server", "--stdio"],
34+
{
35+
env: {
36+
...process.env,
37+
BUN_BE_BUN: "1",
4938
},
50-
)
51-
return {
52-
process: proc,
53-
initialization: {
54-
tsserver: {
55-
path: tsserver,
56-
},
39+
},
40+
)
41+
return {
42+
process: proc,
43+
initialization: {
44+
tsserver: {
45+
path: tsserver,
5746
},
58-
}
59-
},
47+
},
48+
}
6049
},
61-
{
62-
id: "golang",
63-
extensions: [".go"],
64-
async spawn() {
65-
let bin = Bun.which("gopls", {
66-
PATH: process.env["PATH"] + ":" + Global.Path.bin,
50+
}
51+
52+
export const Gopls: Info = {
53+
id: "golang",
54+
extensions: [".go"],
55+
async spawn() {
56+
let bin = Bun.which("gopls", {
57+
PATH: process.env["PATH"] + ":" + Global.Path.bin,
58+
})
59+
if (!bin) {
60+
log.info("installing gopls")
61+
const proc = Bun.spawn({
62+
cmd: ["go", "install", "golang.org/x/tools/gopls@latest"],
63+
env: { ...process.env, GOBIN: Global.Path.bin },
64+
stdout: "pipe",
65+
stderr: "pipe",
66+
stdin: "pipe",
67+
})
68+
const exit = await proc.exited
69+
if (exit !== 0) {
70+
log.error("Failed to install gopls")
71+
return
72+
}
73+
bin = path.join(
74+
Global.Path.bin,
75+
"gopls" + (process.platform === "win32" ? ".exe" : ""),
76+
)
77+
log.info(`installed gopls`, {
78+
bin,
6779
})
68-
if (!bin) {
69-
log.info("installing gopls")
70-
const proc = Bun.spawn({
71-
cmd: ["go", "install", "golang.org/x/tools/gopls@latest"],
72-
env: { ...process.env, GOBIN: Global.Path.bin },
73-
stdout: "pipe",
74-
stderr: "pipe",
75-
stdin: "pipe",
76-
})
77-
const exit = await proc.exited
78-
if (exit !== 0) {
79-
log.error("Failed to install gopls")
80-
return
81-
}
82-
bin = path.join(
83-
Global.Path.bin,
84-
"gopls" + (process.platform === "win32" ? ".exe" : ""),
85-
)
86-
log.info(`installed gopls`, {
87-
bin,
88-
})
80+
}
81+
return {
82+
process: spawn(bin!),
83+
}
84+
},
85+
}
86+
87+
export const RubyLsp: Info = {
88+
id: "ruby-lsp",
89+
extensions: [".rb", ".rake", ".gemspec", ".ru"],
90+
async spawn() {
91+
let bin = Bun.which("ruby-lsp", {
92+
PATH: process.env["PATH"] + ":" + Global.Path.bin,
93+
})
94+
if (!bin) {
95+
const ruby = Bun.which("ruby")
96+
const gem = Bun.which("gem")
97+
if (!ruby || !gem) {
98+
log.info("Ruby not found, please install Ruby first")
99+
return
89100
}
90-
return {
91-
process: spawn(bin!),
101+
log.info("installing ruby-lsp")
102+
const proc = Bun.spawn({
103+
cmd: ["gem", "install", "ruby-lsp", "--bindir", Global.Path.bin],
104+
stdout: "pipe",
105+
stderr: "pipe",
106+
stdin: "pipe",
107+
})
108+
const exit = await proc.exited
109+
if (exit !== 0) {
110+
log.error("Failed to install ruby-lsp")
111+
return
92112
}
93-
},
113+
bin = path.join(
114+
Global.Path.bin,
115+
"ruby-lsp" + (process.platform === "win32" ? ".exe" : ""),
116+
)
117+
log.info(`installed ruby-lsp`, {
118+
bin,
119+
})
120+
}
121+
return {
122+
process: spawn(bin!, ["--stdio"]),
123+
}
94124
},
95-
]
125+
}
96126
}

0 commit comments

Comments
 (0)