ollie/tools/lsp/cmd/lsp_diagnostics/main.go

80 lines
1.5 KiB
Go

// lsp_diagnostics — Errors and warnings for a file.
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"ollie/tools/lsp/internal/lsp"
"ollie/tools/lsp/internal/cmdutil"
)
func main() {
cmdutil.RunCustom(func() {
var args struct {
Path string `json:"path"`
}
if err := json.NewDecoder(os.Stdin).Decode(&args); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
file, _ := filepath.Abs(args.Path)
result, err := lsp.ClientRequest(&lsp.BridgeRequest{
Method: "diagnostics",
File: file,
})
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if result == nil || string(result) == "null" || string(result) == "[]" {
fmt.Println("(no diagnostics)")
return
}
var diags []diagnostic
if err := json.Unmarshal(result, &diags); err != nil {
fmt.Println("(no diagnostics)")
return
}
for _, d := range diags {
sev := severity(d.Severity)
line := d.Range.Start.Line + 1
col := d.Range.Start.Character + 1
fmt.Printf("%s:%d:%d: %s: %s\n", file, line, col, sev, d.Message)
}
})
}
type diagnostic struct {
Range struct {
Start struct {
Line int `json:"line"`
Character int `json:"character"`
} `json:"start"`
} `json:"range"`
Severity int `json:"severity"`
Message string `json:"message"`
}
func severity(s int) string {
switch s {
case 1:
return "error"
case 2:
return "warning"
case 3:
return "info"
case 4:
return "hint"
default:
return "diagnostic"
}
}