diagnostic.ts 900 B

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