use init::TextFn;
use lsp_server::Connection;
use main_loop::Task;
use options::InitOptions;
use std::{collections::HashMap, panic::RefUnwindSafe};
pub mod init;
pub mod main_loop;
pub mod notification_registry;
pub mod options;
pub mod request_registry;
pub(crate) mod task_pool;
pub(crate) type ReqHandler<Db> = fn(&mut Session<Db>, lsp_server::Response);
type ReqQueue<Db> = lsp_server::ReqQueue<String, ReqHandler<Db>>;
pub struct Session<Db: salsa::Database> {
pub init_options: InitOptions,
pub text_fn: TextFn,
pub extensions: HashMap<String, String>,
pub(crate) task_rx: crossbeam_channel::Receiver<Task>,
pub(crate) task_pool: task_pool::TaskPool<Task>,
pub req_queue: ReqQueue<Db>,
pub connection: Connection,
pub db: Db,
}
impl<Db: salsa::Database + Clone> Session<Db> {
pub(crate) fn snapshot(&self) -> DbSnapShot<Db> {
DbSnapShot {
db: self.db.clone(),
}
}
}
pub struct DbSnapShot<Db: salsa::Database + Send> {
db: Db,
}
impl<Db: salsa::Database + Clone + RefUnwindSafe> DbSnapShot<Db> {
pub fn with_db<F, T>(&self, f: F) -> Result<T, salsa::Cancelled>
where
F: FnOnce(&Db) -> T + std::panic::UnwindSafe,
{
salsa::Cancelled::catch(|| f(&self.db))
}
}