use std::collections::HashMap;
use super::session::{InitializeParams, LspSession};
pub struct LspPool {
sessions: HashMap<String, LspSession>,
}
impl LspPool {
pub fn new() -> Self {
Self {
sessions: HashMap::new(),
}
}
pub async fn get_or_spawn(
&mut self,
language: &str,
cmd: &str,
root_path: Option<&str>,
extra_args: &[String],
) -> Result<&mut LspSession, anyhow::Error> {
let key = match root_path {
Some(root) => format!("{language}:{cmd}:{root}"),
None => format!("{language}:{cmd}"),
};
if !self.sessions.contains_key(&key) {
let mut session = LspSession::spawn_with_args(cmd, extra_args)?;
let init_params = InitializeParams {
root_uri: root_path.map(|s| s.to_string()),
};
session.initialize(init_params).await?;
tracing::info!(key, "LSP session initialized");
self.sessions.insert(key.clone(), session);
}
Ok(self.sessions.get_mut(&key).unwrap())
}
}
impl Default for LspPool {
fn default() -> Self {
Self::new()
}
}