use dsl_server::Backend;
use tower_lsp::{LspService, Server};
pub fn run() -> anyhow::Result<()> {
#[cfg(target_os = "linux")]
unsafe {
let _ = libc::prctl(1, libc::SIGTERM, 0, 0, 0);
}
let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build()?;
rt.block_on(async move {
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();
let (service, socket) = LspService::new(Backend::new);
let serve = Server::new(stdin, stdout, socket).serve(service);
#[cfg(unix)]
{
use tokio::signal::unix::{SignalKind, signal};
let mut sigterm = signal(SignalKind::terminate()).ok();
let mut sigint = signal(SignalKind::interrupt()).ok();
let mut sighup = signal(SignalKind::hangup()).ok();
tokio::select! {
_ = serve => {}
_ = async {
if let Some(s) = sigterm.as_mut() { s.recv().await; }
else { std::future::pending::<()>().await; }
} => { tracing::info!("SIGTERM received, shutting down"); }
_ = async {
if let Some(s) = sigint.as_mut() { s.recv().await; }
else { std::future::pending::<()>().await; }
} => { tracing::info!("SIGINT received, shutting down"); }
_ = async {
if let Some(s) = sighup.as_mut() { s.recv().await; }
else { std::future::pending::<()>().await; }
} => { tracing::info!("SIGHUP received, shutting down"); }
}
}
#[cfg(not(unix))]
{
serve.await;
}
});
Ok(())
}