rust-analyzer/crates/ra_lsp_server/src/main.rs

89 lines
2.5 KiB
Rust
Raw Normal View History

2019-11-22 07:33:08 +00:00
//! `ra_lsp_server` binary
2019-08-30 17:18:57 +00:00
use lsp_server::Connection;
use ra_lsp_server::{show_message, Result, ServerConfig};
2019-04-02 14:52:04 +00:00
use ra_prof;
2018-08-10 12:07:43 +00:00
fn main() -> Result<()> {
2019-08-31 11:47:37 +00:00
setup_logging()?;
2019-12-09 14:59:04 +00:00
match Args::parse()? {
Args::Version => println!("rust-analyzer {}", env!("REV")),
Args::Run => run_server()?,
}
2019-08-31 11:47:37 +00:00
Ok(())
}
fn setup_logging() -> Result<()> {
2019-06-15 09:58:17 +00:00
std::env::set_var("RUST_BACKTRACE", "short");
2019-08-31 11:47:37 +00:00
2019-11-30 00:35:03 +00:00
env_logger::try_init()?;
2019-08-31 11:47:37 +00:00
2019-04-14 20:18:58 +00:00
ra_prof::set_filter(match std::env::var("RA_PROFILE") {
Ok(spec) => ra_prof::Filter::from_spec(&spec),
2019-04-14 20:04:08 +00:00
Err(_) => ra_prof::Filter::disabled(),
2019-04-14 20:18:58 +00:00
});
2019-08-31 11:47:37 +00:00
Ok(())
2018-08-10 14:49:45 +00:00
}
2018-11-08 15:43:02 +00:00
2019-12-09 14:59:04 +00:00
enum Args {
Version,
Run,
}
impl Args {
fn parse() -> Result<Args> {
let res =
if std::env::args().any(|it| it == "--version") { Args::Version } else { Args::Run };
Ok(res)
}
}
2019-08-31 11:47:37 +00:00
fn run_server() -> Result<()> {
log::info!("lifecycle: server started");
2019-08-30 17:18:57 +00:00
let (connection, io_threads) = Connection::stdio();
let server_capabilities = serde_json::to_value(ra_lsp_server::server_capabilities()).unwrap();
2019-08-30 17:18:57 +00:00
let initialize_params = connection.initialize(server_capabilities)?;
let initialize_params: lsp_types::InitializeParams = serde_json::from_value(initialize_params)?;
2019-08-31 11:47:37 +00:00
let cwd = std::env::current_dir()?;
2019-08-30 17:18:57 +00:00
let root = initialize_params.root_uri.and_then(|it| it.to_file_path().ok()).unwrap_or(cwd);
let workspace_roots = initialize_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]);
let server_config: ServerConfig = initialize_params
.initialization_options
.and_then(|v| {
serde_json::from_value(v)
.map_err(|e| {
log::error!("failed to deserialize config: {}", e);
show_message(
lsp_types::MessageType::Error,
format!("failed to deserialize config: {}", e),
&connection.sender,
);
})
.ok()
})
.unwrap_or_default();
ra_lsp_server::main_loop(
workspace_roots,
initialize_params.capabilities,
server_config,
connection,
2019-08-30 17:18:57 +00:00
)?;
2018-12-06 18:03:39 +00:00
log::info!("shutting down IO...");
io_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
}