Skip to content

Commit a17ac02

Browse files
authored
refactor: extract LSP diagnostic report formatter (#21964)
1 parent 57f9397 commit a17ac02

4 files changed

Lines changed: 24 additions & 35 deletions

File tree

packages/opencode/src/lsp/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,8 @@ export namespace LSP {
540540
export const outgoingCalls = async (input: LocInput) => runPromise((svc) => svc.outgoingCalls(input))
541541

542542
export namespace Diagnostic {
543+
const MAX_PER_FILE = 20
544+
543545
export function pretty(diagnostic: LSPClient.Diagnostic) {
544546
const severityMap = {
545547
1: "ERROR",
@@ -554,5 +556,14 @@ export namespace LSP {
554556

555557
return `${severity} [${line}:${col}] ${diagnostic.message}`
556558
}
559+
560+
export function report(file: string, issues: LSPClient.Diagnostic[]) {
561+
const errors = issues.filter((item) => item.severity === 1)
562+
if (errors.length === 0) return ""
563+
const limited = errors.slice(0, MAX_PER_FILE)
564+
const more = errors.length - MAX_PER_FILE
565+
const suffix = more > 0 ? `\n... and ${more} more` : ""
566+
return `<diagnostics file="${file}">\n${limited.map(pretty).join("\n")}${suffix}\n</diagnostics>`
567+
}
557568
}
558569
}

packages/opencode/src/tool/apply_patch.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,13 @@ export const ApplyPatchTool = Tool.defineEffect(
258258
})
259259
let output = `Success. Updated the following files:\n${summaryLines.join("\n")}`
260260

261-
// Report LSP errors for changed files
262-
const MAX_DIAGNOSTICS_PER_FILE = 20
263261
for (const change of fileChanges) {
264262
if (change.type === "delete") continue
265263
const target = change.movePath ?? change.filePath
266-
const normalized = AppFileSystem.normalizePath(target)
267-
const issues = diagnostics[normalized] ?? []
268-
const errors = issues.filter((item) => item.severity === 1)
269-
if (errors.length > 0) {
270-
const limited = errors.slice(0, MAX_DIAGNOSTICS_PER_FILE)
271-
const suffix =
272-
errors.length > MAX_DIAGNOSTICS_PER_FILE ? `\n... and ${errors.length - MAX_DIAGNOSTICS_PER_FILE} more` : ""
273-
output += `\n\nLSP errors detected in ${path.relative(Instance.worktree, target).replaceAll("\\", "/")}, please fix:\n<diagnostics file="${target}">\n${limited.map(LSP.Diagnostic.pretty).join("\n")}${suffix}\n</diagnostics>`
274-
}
264+
const block = LSP.Diagnostic.report(target, diagnostics[AppFileSystem.normalizePath(target)] ?? [])
265+
if (!block) continue
266+
const rel = path.relative(Instance.worktree, target).replaceAll("\\", "/")
267+
output += `\n\nLSP errors detected in ${rel}, please fix:\n${block}`
275268
}
276269

277270
return {

packages/opencode/src/tool/edit.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import { Instance } from "../project/instance"
2020
import { Snapshot } from "@/snapshot"
2121
import { assertExternalDirectoryEffect } from "./external-directory"
2222

23-
const MAX_DIAGNOSTICS_PER_FILE = 20
24-
2523
function normalizeLineEndings(text: string): string {
2624
return text.replaceAll("\r\n", "\n")
2725
}
@@ -166,16 +164,8 @@ export const EditTool = Tool.defineEffect(
166164
yield* lsp.touchFile(filePath, true)
167165
const diagnostics = yield* lsp.diagnostics()
168166
const normalizedFilePath = Filesystem.normalizePath(filePath)
169-
const issues = diagnostics[normalizedFilePath] ?? []
170-
const errors = issues.filter((item) => item.severity === 1)
171-
if (errors.length > 0) {
172-
const limited = errors.slice(0, MAX_DIAGNOSTICS_PER_FILE)
173-
const suffix =
174-
errors.length > MAX_DIAGNOSTICS_PER_FILE
175-
? `\n... and ${errors.length - MAX_DIAGNOSTICS_PER_FILE} more`
176-
: ""
177-
output += `\n\nLSP errors detected in this file, please fix:\n<diagnostics file="${filePath}">\n${limited.map(LSP.Diagnostic.pretty).join("\n")}${suffix}\n</diagnostics>`
178-
}
167+
const block = LSP.Diagnostic.report(filePath, diagnostics[normalizedFilePath] ?? [])
168+
if (block) output += `\n\nLSP errors detected in this file, please fix:\n${block}`
179169

180170
return {
181171
metadata: {

packages/opencode/src/tool/write.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { Instance } from "../project/instance"
1515
import { trimDiff } from "./edit"
1616
import { assertExternalDirectoryEffect } from "./external-directory"
1717

18-
const MAX_DIAGNOSTICS_PER_FILE = 20
1918
const MAX_PROJECT_DIAGNOSTICS_FILES = 5
2019

2120
export const WriteTool = Tool.defineEffect(
@@ -72,20 +71,16 @@ export const WriteTool = Tool.defineEffect(
7271
const normalizedFilepath = AppFileSystem.normalizePath(filepath)
7372
let projectDiagnosticsCount = 0
7473
for (const [file, issues] of Object.entries(diagnostics)) {
75-
const errors = issues.filter((item) => item.severity === 1)
76-
if (errors.length === 0) continue
77-
const limited = errors.slice(0, MAX_DIAGNOSTICS_PER_FILE)
78-
const suffix =
79-
errors.length > MAX_DIAGNOSTICS_PER_FILE
80-
? `\n... and ${errors.length - MAX_DIAGNOSTICS_PER_FILE} more`
81-
: ""
82-
if (file === normalizedFilepath) {
83-
output += `\n\nLSP errors detected in this file, please fix:\n<diagnostics file="${filepath}">\n${limited.map(LSP.Diagnostic.pretty).join("\n")}${suffix}\n</diagnostics>`
74+
const current = file === normalizedFilepath
75+
if (!current && projectDiagnosticsCount >= MAX_PROJECT_DIAGNOSTICS_FILES) continue
76+
const block = LSP.Diagnostic.report(current ? filepath : file, issues)
77+
if (!block) continue
78+
if (current) {
79+
output += `\n\nLSP errors detected in this file, please fix:\n${block}`
8480
continue
8581
}
86-
if (projectDiagnosticsCount >= MAX_PROJECT_DIAGNOSTICS_FILES) continue
8782
projectDiagnosticsCount++
88-
output += `\n\nLSP errors detected in other files:\n<diagnostics file="${file}">\n${limited.map(LSP.Diagnostic.pretty).join("\n")}${suffix}\n</diagnostics>`
83+
output += `\n\nLSP errors detected in other files:\n${block}`
8984
}
9085

9186
return {

0 commit comments

Comments
 (0)