Skip to content

Commit 8afb625

Browse files
authored
refactor: extract Diagnostic namespace into lsp/diagnostic.ts + self-reexport (#22983)
1 parent c59df63 commit 8afb625

2 files changed

Lines changed: 30 additions & 27 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as LSPClient from "./client"
2+
3+
const MAX_PER_FILE = 20
4+
5+
export function pretty(diagnostic: LSPClient.Diagnostic) {
6+
const severityMap = {
7+
1: "ERROR",
8+
2: "WARN",
9+
3: "INFO",
10+
4: "HINT",
11+
}
12+
13+
const severity = severityMap[diagnostic.severity || 1]
14+
const line = diagnostic.range.start.line + 1
15+
const col = diagnostic.range.start.character + 1
16+
17+
return `${severity} [${line}:${col}] ${diagnostic.message}`
18+
}
19+
20+
export function report(file: string, issues: LSPClient.Diagnostic[]) {
21+
const errors = issues.filter((item) => item.severity === 1)
22+
if (errors.length === 0) return ""
23+
const limited = errors.slice(0, MAX_PER_FILE)
24+
const more = errors.length - MAX_PER_FILE
25+
const suffix = more > 0 ? `\n... and ${more} more` : ""
26+
return `<diagnostics file="${file}">\n${limited.map(pretty).join("\n")}${suffix}\n</diagnostics>`
27+
}
28+
29+
export * as Diagnostic from "./diagnostic"

packages/opencode/src/lsp/lsp.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -505,30 +505,4 @@ export const layer = Layer.effect(
505505

506506
export const defaultLayer = layer.pipe(Layer.provide(Config.defaultLayer))
507507

508-
export namespace Diagnostic {
509-
const MAX_PER_FILE = 20
510-
511-
export function pretty(diagnostic: LSPClient.Diagnostic) {
512-
const severityMap = {
513-
1: "ERROR",
514-
2: "WARN",
515-
3: "INFO",
516-
4: "HINT",
517-
}
518-
519-
const severity = severityMap[diagnostic.severity || 1]
520-
const line = diagnostic.range.start.line + 1
521-
const col = diagnostic.range.start.character + 1
522-
523-
return `${severity} [${line}:${col}] ${diagnostic.message}`
524-
}
525-
526-
export function report(file: string, issues: LSPClient.Diagnostic[]) {
527-
const errors = issues.filter((item) => item.severity === 1)
528-
if (errors.length === 0) return ""
529-
const limited = errors.slice(0, MAX_PER_FILE)
530-
const more = errors.length - MAX_PER_FILE
531-
const suffix = more > 0 ? `\n... and ${more} more` : ""
532-
return `<diagnostics file="${file}">\n${limited.map(pretty).join("\n")}${suffix}\n</diagnostics>`
533-
}
534-
}
508+
export * as Diagnostic from "./diagnostic"

0 commit comments

Comments
 (0)