Skip to content

Commit c10ab0c

Browse files
Merge pull request #43 from reddb-io/afk/40-make-native-lsp-the-redcode-semantic-aut
resume: #40
2 parents 75a89df + 85de1fa commit c10ab0c

15 files changed

Lines changed: 199 additions & 32 deletions

File tree

‎.changeset/native-lsp-default.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"opencode": minor
3+
---
4+
5+
Enable native language servers and agent semantic tools by default, and surface language-server initialization failures in status views.

‎packages/core/src/config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class Info extends Schema.Class<Info>("Config.Info")({
7474
description: "Enable built-in formatters or configure formatter overrides",
7575
}),
7676
lsp: ConfigLSP.Info.pipe(Schema.optional).annotate({
77-
description: "Enable built-in language servers or configure server overrides",
77+
description: "Configure language servers, which are enabled by default; set to false to disable",
7878
}),
7979
attachments: ConfigAttachments.Info.pipe(Schema.optional).annotate({
8080
description: "Attachment processing configuration",

‎packages/core/src/v1/config/config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export const Info = Schema.Struct({
119119
}),
120120
lsp: Schema.optional(ConfigLSPV1.Info).annotate({
121121
description:
122-
"Enable or configure LSP servers. Omit or set to false to disable, true to enable built-ins, or an object to enable built-ins with overrides.",
122+
"Configure LSP servers. Omit or set to true to enable built-ins, set to false to disable, or use an object for overrides.",
123123
}),
124124
instructions: Schema.optional(Schema.mutable(Schema.Array(Schema.String))).annotate({
125125
description: "Additional instruction files or patterns to include",

‎packages/opencode/src/lsp/lsp.ts‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ type LocInput = { file: string; line: number; character: number }
112112
interface State {
113113
clients: LSPClient.Info[]
114114
servers: Record<string, LSPServer.Info>
115-
broken: Set<string>
115+
broken: Map<string, Status>
116116
spawning: Map<string, Promise<LSPClient.Info | undefined>>
117117
}
118118

@@ -148,7 +148,7 @@ const layer = Layer.effect(
148148

149149
const servers: Record<string, LSPServer.Info> = {}
150150

151-
if (!cfg.lsp) {
151+
if (cfg.lsp === false) {
152152
yield* Effect.logInfo("all LSPs are disabled")
153153
} else {
154154
for (const server of Object.values(LSPServer)) {
@@ -157,7 +157,7 @@ const layer = Layer.effect(
157157

158158
filterExperimentalServers(servers, flags)
159159

160-
if (cfg.lsp !== true) {
160+
if (cfg.lsp && cfg.lsp !== true) {
161161
for (const [name, item] of Object.entries(cfg.lsp)) {
162162
const existing = servers[name]
163163
if (item.disabled) {
@@ -191,7 +191,7 @@ const layer = Layer.effect(
191191
const s: State = {
192192
clients: [],
193193
servers,
194-
broken: new Set(),
194+
broken: new Map(),
195195
spawning: new Map(),
196196
}
197197

@@ -215,14 +215,22 @@ const layer = Layer.effect(
215215
let updated = 0
216216

217217
async function schedule(server: LSPServer.Info, root: string, key: string) {
218+
const failed = () => {
219+
s.broken.set(key, {
220+
id: server.id,
221+
name: server.id,
222+
root: path.relative(ctx.directory, root),
223+
status: "error",
224+
})
225+
}
218226
const handle = await server
219227
.spawn(root, ctx, flags)
220228
.then((value) => {
221-
if (!value) s.broken.add(key)
229+
if (!value) failed()
222230
return value
223231
})
224232
.catch(() => {
225-
s.broken.add(key)
233+
failed()
226234
return undefined
227235
})
228236

@@ -234,7 +242,7 @@ const layer = Layer.effect(
234242
directory: ctx.directory,
235243
instance: ctx,
236244
}).catch(async () => {
237-
s.broken.add(key)
245+
failed()
238246
await Process.stop(handle.process)
239247
return undefined
240248
})
@@ -282,10 +290,10 @@ const layer = Layer.effect(
282290
})
283291

284292
const client = await task
293+
updated++
285294
if (!client) continue
286295

287296
result.push(client)
288-
updated++
289297
}
290298

291299
return { result, updated }
@@ -322,6 +330,7 @@ const layer = Layer.effect(
322330
status: "connected",
323331
})
324332
}
333+
result.push(...s.broken.values())
325334
return result
326335
})
327336

‎packages/opencode/src/tool/registry.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ const layer = Layer.effect(
239239
tool.skill,
240240
tool.patch,
241241
...(tool.execute ? [tool.execute] : []),
242-
...(flags.experimentalLspTool ? [tool.lsp] : []),
242+
tool.lsp,
243243
...(flags.experimentalPlanMode && flags.client === "cli" ? [tool.plan] : []),
244244
],
245245
task: tool.task,

‎packages/opencode/test/lsp/index.test.ts‎

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("lsp.spawn", () => {
5050
{ config: { lsp: true } },
5151
)
5252

53-
it.instance("does not spawn builtin LSP for files inside instance when LSP is unset", () =>
53+
it.instance("spawns builtin LSP for files inside instance when LSP is unset", () =>
5454
LSP.Service.use((lsp) =>
5555
Effect.gen(function* () {
5656
const dir = (yield* TestInstance).directory
@@ -62,14 +62,98 @@ describe("lsp.spawn", () => {
6262
line: 0,
6363
character: 0,
6464
})
65-
expect(spy).toHaveBeenCalledTimes(0)
65+
expect(spy).toHaveBeenCalledTimes(1)
66+
expect(yield* lsp.status()).toContainEqual({
67+
id: "typescript",
68+
name: "typescript",
69+
root: "",
70+
status: "error",
71+
})
6672
} finally {
6773
spy.mockRestore()
6874
}
6975
}),
7076
),
7177
)
7278

79+
for (const [name, server, extension] of [
80+
["TypeScript", LSPServer.Typescript, ".ts"],
81+
["Rust", LSPServer.RustAnalyzer, ".rs"],
82+
["Go", LSPServer.Gopls, ".go"],
83+
["Python", LSPServer.Pyright, ".py"],
84+
] as const) {
85+
it.instance(`starts at most one ${name} server per root`, () =>
86+
LSP.Service.use((lsp) =>
87+
Effect.gen(function* () {
88+
const dir = (yield* TestInstance).directory
89+
const root = spyOn(server, "root").mockResolvedValue(dir)
90+
const spawn = spyOn(server, "spawn").mockResolvedValue(undefined)
91+
const file = path.join(dir, "src", `inside${extension}`)
92+
93+
try {
94+
yield* Effect.all(
95+
[
96+
lsp.hover({ file, line: 0, character: 0 }),
97+
lsp.definition({ file, line: 0, character: 0 }),
98+
lsp.references({ file, line: 0, character: 0 }),
99+
],
100+
{ concurrency: "unbounded" },
101+
)
102+
expect(spawn).toHaveBeenCalledTimes(1)
103+
} finally {
104+
root.mockRestore()
105+
spawn.mockRestore()
106+
}
107+
}),
108+
),
109+
)
110+
}
111+
112+
it.instance("publishes lsp.updated after failed initialization", () =>
113+
Effect.gen(function* () {
114+
const dir = (yield* TestInstance).directory
115+
const lsp = yield* LSP.Service
116+
const updated = yield* Deferred.make<void>()
117+
const events = yield* EventV2Bridge.Service
118+
const unsubscribe = yield* events.listen((event) => {
119+
if (event.type === LSP.Event.Updated.type) Deferred.doneUnsafe(updated, Effect.void)
120+
return Effect.void
121+
})
122+
yield* Effect.addFinalizer(() => unsubscribe)
123+
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
124+
125+
try {
126+
yield* lsp.touchFile(path.join(dir, "src", "inside.ts"))
127+
yield* awaitWithTimeout(Deferred.await(updated), "lsp.updated event was not published")
128+
} finally {
129+
spy.mockRestore()
130+
}
131+
}),
132+
)
133+
134+
it.instance(
135+
"does not spawn builtin LSP when explicitly disabled",
136+
() =>
137+
LSP.Service.use((lsp) =>
138+
Effect.gen(function* () {
139+
const dir = (yield* TestInstance).directory
140+
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
141+
142+
try {
143+
yield* lsp.hover({
144+
file: path.join(dir, "src", "inside.ts"),
145+
line: 0,
146+
character: 0,
147+
})
148+
expect(spy).toHaveBeenCalledTimes(0)
149+
} finally {
150+
spy.mockRestore()
151+
}
152+
}),
153+
),
154+
{ config: { lsp: false } },
155+
)
156+
73157
it.instance(
74158
"would spawn builtin LSP for files inside instance when lsp is true",
75159
() =>

‎packages/opencode/test/lsp/lifecycle.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ describe("LSP service lifecycle", () => {
4242
),
4343
)
4444

45-
it.instance("hasClients() returns false for .ts files in instance when LSP is unset", () =>
45+
it.instance("hasClients() returns true for .ts files in instance when LSP is unset", () =>
4646
LSP.Service.use((lsp) =>
4747
Effect.gen(function* () {
4848
const result = yield* lsp.hasClients(path.join((yield* TestInstance).directory, "test.ts"))
49-
expect(result).toBe(false)
49+
expect(result).toBe(true)
5050
}),
5151
),
5252
)

‎packages/opencode/test/tool/registry.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ afterEach(async () => {
100100
})
101101

102102
describe("tool.registry", () => {
103+
it.instance("exposes lsp without experimental flags", () =>
104+
Effect.gen(function* () {
105+
const registry = yield* ToolRegistry.Service
106+
const ids = yield* registry.ids()
107+
108+
expect(ids).toContain("lsp")
109+
}),
110+
)
111+
103112
it.instance("does not expose task_status", () =>
104113
Effect.gen(function* () {
105114
const registry = yield* ToolRegistry.Service

‎packages/tui/src/feature-plugins/home/tips-view.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ const TIPS: Tip[] = [
228228
'Set {highlight}"formatter": true{/highlight} to enable built-in formatters',
229229
'Set {highlight}"formatter": false{/highlight} to disable inherited formatters',
230230
"Define custom formatter commands with file extensions in config",
231-
'Set {highlight}"lsp": true{/highlight} to enable built-in LSP code analysis',
231+
'Built-in LSP code analysis activates when supported files are read; set {highlight}"lsp": false{/highlight} to disable it',
232232
"Create {highlight}.ts{/highlight} files in {highlight}.opencode/tools/{/highlight} to define new LLM tools",
233233
"Tool definitions can invoke scripts written in Python, Go, etc",
234234
"Add {highlight}.ts{/highlight} files to {highlight}.opencode/plugins/{/highlight} for event hooks",

‎packages/tui/src/feature-plugins/sidebar/lsp.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function View(props: { api: TuiPluginApi }) {
88
const [open, setOpen] = createSignal(true)
99
const theme = () => props.api.theme.current
1010
const list = createMemo(() => props.api.state.lsp())
11-
const off = createMemo(() => !props.api.state.config.lsp)
11+
const off = createMemo(() => props.api.state.config.lsp === false)
1212

1313
return (
1414
<box>
@@ -37,6 +37,7 @@ function View(props: { api: TuiPluginApi }) {
3737
</text>
3838
<text fg={theme().textMuted}>
3939
{item.id} {item.root}
40+
{item.status === "error" ? " (failed)" : ""}
4041
</text>
4142
</box>
4243
)}

0 commit comments

Comments
 (0)