refactor: remove ambient instance reads from lsp (#23023)

This commit is contained in:
Kit Langton 2026-04-17 21:47:59 -04:00 committed by GitHub
parent a5d99e7a3c
commit e6fd57165e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 75 additions and 67 deletions

View file

@ -11,7 +11,6 @@ import z from "zod"
import type * as LSPServer from "./server"
import { NamedError } from "@opencode-ai/shared/util/error"
import { withTimeout } from "../util/timeout"
import { Instance } from "../project/instance"
import { Filesystem } from "../util"
const DIAGNOSTICS_DEBOUNCE_MS = 150
@ -39,7 +38,7 @@ export const Event = {
),
}
export async function create(input: { serverID: string; server: LSPServer.Handle; root: string }) {
export async function create(input: { serverID: string; server: LSPServer.Handle; root: string; directory: string }) {
const l = log.clone().tag("serverID", input.serverID)
l.info("starting client")
@ -145,33 +144,33 @@ export async function create(input: { serverID: string; server: LSPServer.Handle
return connection
},
notify: {
async open(input: { path: string }) {
input.path = path.isAbsolute(input.path) ? input.path : path.resolve(Instance.directory, input.path)
const text = await Filesystem.readText(input.path)
const extension = path.extname(input.path)
async open(request: { path: string }) {
request.path = path.isAbsolute(request.path) ? request.path : path.resolve(input.directory, request.path)
const text = await Filesystem.readText(request.path)
const extension = path.extname(request.path)
const languageId = LANGUAGE_EXTENSIONS[extension] ?? "plaintext"
const version = files[input.path]
const version = files[request.path]
if (version !== undefined) {
log.info("workspace/didChangeWatchedFiles", input)
log.info("workspace/didChangeWatchedFiles", request)
await connection.sendNotification("workspace/didChangeWatchedFiles", {
changes: [
{
uri: pathToFileURL(input.path).href,
uri: pathToFileURL(request.path).href,
type: 2, // Changed
},
],
})
const next = version + 1
files[input.path] = next
files[request.path] = next
log.info("textDocument/didChange", {
path: input.path,
path: request.path,
version: next,
})
await connection.sendNotification("textDocument/didChange", {
textDocument: {
uri: pathToFileURL(input.path).href,
uri: pathToFileURL(request.path).href,
version: next,
},
contentChanges: [{ text }],
@ -179,36 +178,36 @@ export async function create(input: { serverID: string; server: LSPServer.Handle
return
}
log.info("workspace/didChangeWatchedFiles", input)
log.info("workspace/didChangeWatchedFiles", request)
await connection.sendNotification("workspace/didChangeWatchedFiles", {
changes: [
{
uri: pathToFileURL(input.path).href,
uri: pathToFileURL(request.path).href,
type: 1, // Created
},
],
})
log.info("textDocument/didOpen", input)
diagnostics.delete(input.path)
log.info("textDocument/didOpen", request)
diagnostics.delete(request.path)
await connection.sendNotification("textDocument/didOpen", {
textDocument: {
uri: pathToFileURL(input.path).href,
uri: pathToFileURL(request.path).href,
languageId,
version: 0,
text,
},
})
files[input.path] = 0
files[request.path] = 0
return
},
},
get diagnostics() {
return diagnostics
},
async waitForDiagnostics(input: { path: string }) {
async waitForDiagnostics(request: { path: string }) {
const normalizedPath = Filesystem.normalizePath(
path.isAbsolute(input.path) ? input.path : path.resolve(Instance.directory, input.path),
path.isAbsolute(request.path) ? request.path : path.resolve(input.directory, request.path),
)
log.info("waiting for diagnostics", { path: normalizedPath })
let unsub: () => void