Skip to content

Commit 4be2752

Browse files
kitlangtonmrsimpson
authored andcommitted
refactor(lsp): remove async facade exports (anomalyco#22321)
1 parent e33a469 commit 4be2752

6 files changed

Lines changed: 160 additions & 182 deletions

File tree

packages/opencode/src/cli/cmd/debug/lsp.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { LSP } from "../../../lsp"
2+
import { AppRuntime } from "../../../effect/app-runtime"
3+
import { Effect } from "effect"
24
import { bootstrap } from "../../bootstrap"
35
import { cmd } from "../cmd"
46
import { Log } from "../../../util/log"
@@ -19,9 +21,16 @@ const DiagnosticsCommand = cmd({
1921
builder: (yargs) => yargs.positional("file", { type: "string", demandOption: true }),
2022
async handler(args) {
2123
await bootstrap(process.cwd(), async () => {
22-
await LSP.touchFile(args.file, true)
23-
await sleep(1000)
24-
process.stdout.write(JSON.stringify(await LSP.diagnostics(), null, 2) + EOL)
24+
const out = await AppRuntime.runPromise(
25+
LSP.Service.use((lsp) =>
26+
Effect.gen(function* () {
27+
yield* lsp.touchFile(args.file, true)
28+
yield* Effect.sleep(1000)
29+
return yield* lsp.diagnostics()
30+
}),
31+
),
32+
)
33+
process.stdout.write(JSON.stringify(out, null, 2) + EOL)
2534
})
2635
},
2736
})
@@ -33,7 +42,7 @@ export const SymbolsCommand = cmd({
3342
async handler(args) {
3443
await bootstrap(process.cwd(), async () => {
3544
using _ = Log.Default.time("symbols")
36-
const results = await LSP.workspaceSymbol(args.query)
45+
const results = await AppRuntime.runPromise(LSP.Service.use((lsp) => lsp.workspaceSymbol(args.query)))
3746
process.stdout.write(JSON.stringify(results, null, 2) + EOL)
3847
})
3948
},
@@ -46,7 +55,7 @@ export const DocumentSymbolsCommand = cmd({
4655
async handler(args) {
4756
await bootstrap(process.cwd(), async () => {
4857
using _ = Log.Default.time("document-symbols")
49-
const results = await LSP.documentSymbol(args.uri)
58+
const results = await AppRuntime.runPromise(LSP.Service.use((lsp) => lsp.documentSymbol(args.uri)))
5059
process.stdout.write(JSON.stringify(results, null, 2) + EOL)
5160
})
5261
},

packages/opencode/src/lsp/index.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { Process } from "../util/process"
1313
import { spawn as lspspawn } from "./launch"
1414
import { Effect, Layer, Context } from "effect"
1515
import { InstanceState } from "@/effect/instance-state"
16-
import { makeRuntime } from "@/effect/run-service"
1716

1817
export namespace LSP {
1918
const log = Log.create({ service: "lsp" })
@@ -508,37 +507,6 @@ export namespace LSP {
508507

509508
export const defaultLayer = layer.pipe(Layer.provide(Config.defaultLayer))
510509

511-
const { runPromise } = makeRuntime(Service, defaultLayer)
512-
513-
export const init = async () => runPromise((svc) => svc.init())
514-
515-
export const status = async () => runPromise((svc) => svc.status())
516-
517-
export const hasClients = async (file: string) => runPromise((svc) => svc.hasClients(file))
518-
519-
export const touchFile = async (input: string, waitForDiagnostics?: boolean) =>
520-
runPromise((svc) => svc.touchFile(input, waitForDiagnostics))
521-
522-
export const diagnostics = async () => runPromise((svc) => svc.diagnostics())
523-
524-
export const hover = async (input: LocInput) => runPromise((svc) => svc.hover(input))
525-
526-
export const definition = async (input: LocInput) => runPromise((svc) => svc.definition(input))
527-
528-
export const references = async (input: LocInput) => runPromise((svc) => svc.references(input))
529-
530-
export const implementation = async (input: LocInput) => runPromise((svc) => svc.implementation(input))
531-
532-
export const documentSymbol = async (uri: string) => runPromise((svc) => svc.documentSymbol(uri))
533-
534-
export const workspaceSymbol = async (query: string) => runPromise((svc) => svc.workspaceSymbol(query))
535-
536-
export const prepareCallHierarchy = async (input: LocInput) => runPromise((svc) => svc.prepareCallHierarchy(input))
537-
538-
export const incomingCalls = async (input: LocInput) => runPromise((svc) => svc.incomingCalls(input))
539-
540-
export const outgoingCalls = async (input: LocInput) => runPromise((svc) => svc.outgoingCalls(input))
541-
542510
export namespace Diagnostic {
543511
const MAX_PER_FILE = 20
544512

packages/opencode/src/server/instance/file.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ export const FileRoutes = lazy(() =>
105105
}),
106106
),
107107
async (c) => {
108-
/*
109-
const query = c.req.valid("query").query
110-
const result = await LSP.workspaceSymbol(query)
111-
return c.json(result)
112-
*/
113108
return c.json([])
114109
},
115110
)

packages/opencode/src/server/instance/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono =>
256256
},
257257
}),
258258
async (c) => {
259-
return c.json(await LSP.status())
259+
const items = await AppRuntime.runPromise(LSP.Service.use((lsp) => lsp.status()))
260+
return c.json(items)
260261
},
261262
)
262263
.get(
Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
import { describe, expect, spyOn, test } from "bun:test"
1+
import { describe, expect, spyOn } from "bun:test"
22
import path from "path"
3-
import * as Lsp from "../../src/lsp/index"
3+
import { Effect, Layer } from "effect"
4+
import { LSP } from "../../src/lsp"
45
import { LSPServer } from "../../src/lsp/server"
5-
import { Instance } from "../../src/project/instance"
6-
import { tmpdir } from "../fixture/fixture"
6+
import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
7+
import { provideTmpdirInstance } from "../fixture/fixture"
8+
import { testEffect } from "../lib/effect"
79

8-
describe("lsp.spawn", () => {
9-
test("does not spawn builtin LSP for files outside instance", async () => {
10-
await using tmp = await tmpdir()
11-
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
12-
13-
try {
14-
await Instance.provide({
15-
directory: tmp.path,
16-
fn: async () => {
17-
await Lsp.LSP.touchFile(path.join(tmp.path, "..", "outside.ts"))
18-
await Lsp.LSP.hover({
19-
file: path.join(tmp.path, "..", "hover.ts"),
20-
line: 0,
21-
character: 0,
22-
})
23-
},
24-
})
10+
const it = testEffect(Layer.mergeAll(LSP.defaultLayer, CrossSpawnSpawner.defaultLayer))
2511

26-
expect(spy).toHaveBeenCalledTimes(0)
27-
} finally {
28-
spy.mockRestore()
29-
await Instance.disposeAll()
30-
}
31-
})
12+
describe("lsp.spawn", () => {
13+
it.live("does not spawn builtin LSP for files outside instance", () =>
14+
provideTmpdirInstance((dir) =>
15+
LSP.Service.use((lsp) =>
16+
Effect.gen(function* () {
17+
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
3218

33-
test("would spawn builtin LSP for files inside instance", async () => {
34-
await using tmp = await tmpdir()
35-
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
19+
try {
20+
yield* lsp.touchFile(path.join(dir, "..", "outside.ts"))
21+
yield* lsp.hover({
22+
file: path.join(dir, "..", "hover.ts"),
23+
line: 0,
24+
character: 0,
25+
})
26+
expect(spy).toHaveBeenCalledTimes(0)
27+
} finally {
28+
spy.mockRestore()
29+
}
30+
}),
31+
),
32+
),
33+
)
3634

37-
try {
38-
await Instance.provide({
39-
directory: tmp.path,
40-
fn: async () => {
41-
await Lsp.LSP.hover({
42-
file: path.join(tmp.path, "src", "inside.ts"),
43-
line: 0,
44-
character: 0,
45-
})
46-
},
47-
})
35+
it.live("would spawn builtin LSP for files inside instance", () =>
36+
provideTmpdirInstance((dir) =>
37+
LSP.Service.use((lsp) =>
38+
Effect.gen(function* () {
39+
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
4840

49-
expect(spy).toHaveBeenCalledTimes(1)
50-
} finally {
51-
spy.mockRestore()
52-
await Instance.disposeAll()
53-
}
54-
})
41+
try {
42+
yield* lsp.hover({
43+
file: path.join(dir, "src", "inside.ts"),
44+
line: 0,
45+
character: 0,
46+
})
47+
expect(spy).toHaveBeenCalledTimes(1)
48+
} finally {
49+
spy.mockRestore()
50+
}
51+
}),
52+
),
53+
),
54+
)
5555
})

0 commit comments

Comments
 (0)