fix lsp diagnostic accurancy

This commit is contained in:
Dax Raad
2025-06-30 22:46:42 -04:00
parent fea56d8de6
commit de15e67834
8 changed files with 299 additions and 104 deletions

View File

@@ -10,13 +10,29 @@ export namespace LSP {
const state = App.state(
"lsp",
async () => {
async (app) => {
log.info("initializing")
const clients = new Map<string, LSPClient.Info>()
const skip = new Set<string>()
for (const server of Object.values(LSPServer)) {
for (const extension of server.extensions) {
const [file] = await Ripgrep.files({
cwd: app.path.cwd,
glob: "*" + extension,
})
if (!file) continue
const handle = await server.spawn(App.info())
if (!handle) break
const client = await LSPClient.create(server.id, handle).catch(
() => {},
)
if (!client) break
clients.set(server.id, client)
break
}
}
log.info("initialized")
return {
clients,
skip,
}
},
async (state) => {
@@ -27,49 +43,22 @@ export namespace LSP {
)
export async function init() {
log.info("init")
const app = App.info()
const result = Object.values(LSPServer).map(async (x) => {
for (const extension of x.extensions) {
const [file] = await Ripgrep.files({
cwd: app.path.cwd,
glob: "*" + extension,
})
if (!file) continue
await LSP.touchFile(file, true)
break
}
})
return Promise.all(result)
return state()
}
export async function touchFile(input: string, waitForDiagnostics?: boolean) {
const extension = path.parse(input).ext
const s = await state()
const matches = Object.values(LSPServer).filter((x) =>
x.extensions.includes(extension),
)
for (const match of matches) {
const existing = s.clients.get(match.id)
if (existing) continue
if (s.skip.has(match.id)) continue
s.skip.add(match.id)
const handle = await match.spawn(App.info())
if (!handle) continue
const client = await LSPClient.create(match.id, handle).catch(() => {})
if (!client) {
s.skip.add(match.id)
continue
}
s.clients.set(match.id, client)
}
if (waitForDiagnostics) {
await run(async (client) => {
const wait = client.waitForDiagnostics({ path: input })
await client.notify.open({ path: input })
return wait
})
}
const matches = Object.values(LSPServer)
.filter((x) => x.extensions.includes(extension))
.map((x) => x.id)
await run(async (client) => {
if (!matches.includes(client.serverID)) return
const wait = waitForDiagnostics
? client.waitForDiagnostics({ path: input })
: Promise.resolve()
await client.notify.open({ path: input })
return wait
})
}
export async function diagnostics() {