55 lines
1.7 KiB
Rust
55 lines
1.7 KiB
Rust
//! 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();
|
||
|
|
|
||
|
|
let mut manager = ctx.lsp_manager.lock().unwrap();
|
||
|
|
manager.start(&language, &command, &extra_args)?;
|
||
|
|
|
||
|
|
Ok(format!("Connected LSP for '{language}'"))
|
||
|
|
}
|
||
|
|
}
|