2026-07-20 09:04:57 +07:00
|
|
|
//! Get diagnostics from LSP.
|
|
|
|
|
|
|
|
|
|
use crate::tools::{Tool, ToolCtx};
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
|
|
|
|
|
pub struct LspDiagnostics;
|
|
|
|
|
|
|
|
|
|
impl Tool for LspDiagnostics {
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
|
"lsp_diagnostics"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn description(&self) -> &'static str {
|
|
|
|
|
"Get diagnostics (errors, warnings) from the LSP for a file"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parameters(&self) -> Value {
|
|
|
|
|
json!({
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"language": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "Language identifier"
|
|
|
|
|
},
|
|
|
|
|
"path": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "File path to get diagnostics for"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"required": ["language", "path"]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
|
|
|
|
|
let language = crate::tools::arg_str(args, "language")?;
|
|
|
|
|
let path = crate::tools::arg_str(args, "path")?;
|
|
|
|
|
|
2026-07-20 10:55:09 +07:00
|
|
|
let manager = match ctx.lsp_manager.lock() {
|
|
|
|
|
Ok(g) => g,
|
|
|
|
|
Err(poisoned) => {
|
|
|
|
|
tracing::error!("LSP manager mutex poisoned, recovering");
|
|
|
|
|
poisoned.into_inner()
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-07-20 09:04:57 +07:00
|
|
|
if let Some(client) = manager.get_client(&language) {
|
|
|
|
|
let result = client.send_request("textDocument/diagnostic", &json!({
|
|
|
|
|
"textDocument": { "uri": format!("file://{}", path) }
|
|
|
|
|
}))?;
|
|
|
|
|
Ok(serde_json::to_string_pretty(&result)?)
|
|
|
|
|
} else {
|
|
|
|
|
anyhow::bail!("no LSP client connected for '{language}'")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|