Files
rust/crates/ra_lsp_server/src/main.rs

79 lines
2.4 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
use flexi_logger::{Duplicate, Logger};
2019-08-30 20:18:57 +03:00
use lsp_server::Connection;
2018-12-21 22:39:59 +03:00
use ra_lsp_server::{show_message, Result, ServerConfig};
2019-04-02 17:52:04 +03:00
use ra_prof;
2018-08-10 15:07:43 +03:00
fn main() -> Result<()> {
2019-08-31 14:47:37 +03:00
setup_logging()?;
run_server()?;
Ok(())
}
fn setup_logging() -> Result<()> {
2019-06-15 12:58:17 +03:00
std::env::set_var("RUST_BACKTRACE", "short");
2019-08-31 14:47:37 +03:00
let logger = Logger::with_env_or_str("error").duplicate_to_stderr(Duplicate::All);
2019-04-14 23:18:58 +03:00
match std::env::var("RA_LOG_DIR") {
Ok(ref v) if v == "1" => logger.log_to_file().directory("log").start()?,
_ => logger.start()?,
};
2019-08-31 14:47:37 +03:00
2019-04-14 23:18:58 +03:00
ra_prof::set_filter(match std::env::var("RA_PROFILE") {
Ok(spec) => ra_prof::Filter::from_spec(&spec),
2019-04-14 23:04:08 +03:00
Err(_) => ra_prof::Filter::disabled(),
2019-04-14 23:18:58 +03:00
});
2019-08-31 14:47:37 +03:00
Ok(())
2018-08-10 17:49:45 +03:00
}
2018-11-08 18:43:02 +03:00
2019-08-31 14:47:37 +03:00
fn run_server() -> Result<()> {
log::info!("lifecycle: server started");
2019-08-30 20:18:57 +03:00
let (connection, io_threads) = Connection::stdio();
let server_capabilities = serde_json::to_value(ra_lsp_server::server_capabilities()).unwrap();
2019-08-30 20:18:57 +03:00
let initialize_params = connection.initialize(server_capabilities)?;
let initialize_params: lsp_types::InitializeParams = serde_json::from_value(initialize_params)?;
2019-08-31 14:47:37 +03:00
let cwd = std::env::current_dir()?;
2019-08-30 20:18:57 +03: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 20:18:57 +03:00
)?;
2018-12-06 21:03:39 +03:00
log::info!("shutting down IO...");
io_threads.join()?;
2018-12-06 21:03:39 +03:00
log::info!("... IO is down");
2018-09-01 17:40:45 +03:00
Ok(())
2018-08-10 17:49:45 +03:00
}