2026-07-20 09:04:57 +07:00
|
|
|
//! Connect to an LSP language server.
|
|
|
|
|
|
|
|
|
|
use crate::tools::{Tool, ToolCtx};
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
|
|
|
|
|
pub struct LspConnect;
|
|
|
|
|
|
|
|
|
|
impl Tool for LspConnect {
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
|
"lsp_connect"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn description(&self) -> &'static str {
|
|
|
|
|
"Connect to an LSP language server for a given language"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parameters(&self) -> Value {
|
|
|
|
|
json!({
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"language": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "Language identifier (e.g. 'rust', 'python')"
|
|
|
|
|
},
|
|
|
|
|
"command": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "Command to start the language server"
|
|
|
|
|
},
|
|
|
|
|
"args": {
|
|
|
|
|
"type": "array",
|
|
|
|
|
"items": {"type": "string"},
|
|
|
|
|
"description": "Arguments for the language server command"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"required": ["language", "command"]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
|
|
|
|
|
let language = crate::tools::arg_str(args, "language")?;
|
|
|
|
|
let command = crate::tools::arg_str(args, "command")?;
|
|
|
|
|
let extra_args: Vec<String> = args
|
|
|
|
|
.get("args")
|
|
|
|
|
.and_then(|v| v.as_array())
|
|
|
|
|
.map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
2026-07-20 10:55:09 +07:00
|
|
|
let mut 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
|
|
|
manager.start(&language, &command, &extra_args)?;
|
|
|
|
|
|
|
|
|
|
Ok(format!("Connected LSP for '{language}'"))
|
|
|
|
|
}
|
|
|
|
|
}
|