2018-12-21 19:39:59 +00:00
|
|
|
use serde::Deserialize;
|
2018-10-15 21:44:23 +00:00
|
|
|
use flexi_logger::{Duplicate, Logger};
|
2018-09-01 14:40:45 +00:00
|
|
|
use gen_lsp_server::{run_server, stdio_transport};
|
2018-12-21 19:39:59 +00:00
|
|
|
|
2019-03-06 09:34:38 +00:00
|
|
|
use ra_lsp_server::{Result, InitializationOptions};
|
2019-04-02 14:52:04 +00:00
|
|
|
use ra_prof;
|
2018-08-10 12:07:43 +00:00
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
2019-04-14 19:40:04 +00:00
|
|
|
std::env::set_var("RUST_BACKTRACE", "short");
|
2018-12-17 22:22:30 +00:00
|
|
|
let logger = Logger::with_env_or_str("error").duplicate_to_stderr(Duplicate::All);
|
2019-04-14 19:40:04 +00:00
|
|
|
match std::env::var("RA_INTERNAL_MODE") {
|
2018-12-17 22:22:30 +00:00
|
|
|
Ok(ref v) if v == "1" => logger.log_to_file().directory("log").start()?,
|
|
|
|
_ => logger.start()?,
|
|
|
|
};
|
2019-04-14 20:04:08 +00:00
|
|
|
// Filtering syntax
|
|
|
|
// env RA_PROFILE=* // dump everything
|
|
|
|
// env RA_PROFILE=foo|bar|baz // enabled only selected entries
|
|
|
|
// env RA_PROFILE=*@3 // dump everything, up to depth 3
|
|
|
|
let filter = match std::env::var("RA_PROFILE") {
|
|
|
|
Ok(p) => {
|
|
|
|
let mut p = p.as_str();
|
|
|
|
let depth = if let Some(idx) = p.rfind("@") {
|
|
|
|
let depth: usize = p[idx + 1..].parse().expect("invalid profile depth");
|
|
|
|
p = &p[..idx];
|
|
|
|
depth
|
|
|
|
} else {
|
|
|
|
999
|
|
|
|
};
|
|
|
|
let allowed =
|
|
|
|
if p == "*" { Vec::new() } else { p.split(";").map(String::from).collect() };
|
|
|
|
ra_prof::Filter::new(depth, allowed)
|
|
|
|
}
|
|
|
|
Err(_) => ra_prof::Filter::disabled(),
|
2019-04-02 14:52:04 +00:00
|
|
|
};
|
2019-04-14 20:04:08 +00:00
|
|
|
ra_prof::set_filter(filter);
|
2018-12-06 18:03:39 +00:00
|
|
|
log::info!("lifecycle: server started");
|
2018-10-17 23:25:37 +00:00
|
|
|
match ::std::panic::catch_unwind(main_inner) {
|
2018-08-10 14:49:45 +00:00
|
|
|
Ok(res) => {
|
2018-12-06 18:03:39 +00:00
|
|
|
log::info!("lifecycle: terminating process with {:?}", res);
|
2018-08-10 14:49:45 +00:00
|
|
|
res
|
|
|
|
}
|
|
|
|
Err(_) => {
|
2018-12-06 18:03:39 +00:00
|
|
|
log::error!("server panicked");
|
|
|
|
failure::bail!("server panicked")
|
2018-08-10 15:01:59 +00:00
|
|
|
}
|
2018-08-10 14:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 15:43:02 +00:00
|
|
|
|
2018-08-10 14:49:45 +00:00
|
|
|
fn main_inner() -> Result<()> {
|
2018-09-01 14:40:45 +00:00
|
|
|
let (receiver, sender, threads) = stdio_transport();
|
2019-04-14 19:40:04 +00:00
|
|
|
let cwd = std::env::current_dir()?;
|
2019-02-08 11:49:43 +00:00
|
|
|
run_server(ra_lsp_server::server_capabilities(), receiver, sender, |params, r, s| {
|
|
|
|
let root = params.root_uri.and_then(|it| it.to_file_path().ok()).unwrap_or(cwd);
|
2019-03-06 09:34:38 +00:00
|
|
|
|
2019-04-11 06:08:19 +00:00
|
|
|
let workspace_roots = params
|
|
|
|
.workspace_folders
|
|
|
|
.map(|workspaces| {
|
|
|
|
workspaces
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|it| it.uri.to_file_path().ok())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
})
|
|
|
|
.filter(|workspaces| !workspaces.is_empty())
|
|
|
|
.unwrap_or_else(|| vec![root]);
|
|
|
|
|
2019-03-06 09:34:38 +00:00
|
|
|
let opts = params
|
2019-02-08 11:49:43 +00:00
|
|
|
.initialization_options
|
|
|
|
.and_then(|v| InitializationOptions::deserialize(v).ok())
|
2019-03-06 09:34:38 +00:00
|
|
|
.unwrap_or(InitializationOptions::default());
|
|
|
|
|
2019-04-11 06:08:19 +00:00
|
|
|
ra_lsp_server::main_loop(workspace_roots, opts, r, s)
|
2019-02-08 11:49:43 +00:00
|
|
|
})?;
|
2018-12-06 18:03:39 +00:00
|
|
|
log::info!("shutting down IO...");
|
2018-09-01 14:40:45 +00:00
|
|
|
threads.join()?;
|
2018-12-06 18:03:39 +00:00
|
|
|
log::info!("... IO is down");
|
2018-09-01 14:40:45 +00:00
|
|
|
Ok(())
|
2018-08-10 14:49:45 +00:00
|
|
|
}
|