2020-02-18 12:11:32 +01:00
|
|
|
//! Driver for rust-analyzer.
|
|
|
|
|
//!
|
|
|
|
|
//! Based on cli flags, either spawns an LSP server, or runs a batch analysis
|
2020-02-17 19:03:03 +01:00
|
|
|
mod args;
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2019-08-30 20:18:57 +03:00
|
|
|
use lsp_server::Connection;
|
2020-04-01 18:46:26 +02:00
|
|
|
use rust_analyzer::{cli, config::Config, from_json, Result};
|
2018-08-10 15:07:43 +03:00
|
|
|
|
2020-02-17 19:03:03 +01:00
|
|
|
use crate::args::HelpPrinted;
|
|
|
|
|
|
2018-08-10 15:07:43 +03:00
|
|
|
fn main() -> Result<()> {
|
2019-08-31 14:47:37 +03:00
|
|
|
setup_logging()?;
|
2020-02-17 19:03:03 +01:00
|
|
|
let args = match args::Args::parse()? {
|
|
|
|
|
Ok(it) => it,
|
|
|
|
|
Err(HelpPrinted) => return Ok(()),
|
|
|
|
|
};
|
|
|
|
|
match args.command {
|
|
|
|
|
args::Command::Parse { no_dump } => cli::parse(no_dump)?,
|
|
|
|
|
args::Command::Symbols => cli::symbols()?,
|
|
|
|
|
args::Command::Highlight { rainbow } => cli::highlight(rainbow)?,
|
2020-03-16 14:51:44 +01:00
|
|
|
args::Command::Stats {
|
|
|
|
|
randomize,
|
|
|
|
|
memory_usage,
|
|
|
|
|
only,
|
|
|
|
|
with_deps,
|
|
|
|
|
path,
|
|
|
|
|
load_output_dirs,
|
2020-04-12 18:25:31 +08:00
|
|
|
with_proc_macro,
|
2020-03-16 14:51:44 +01:00
|
|
|
} => cli::analysis_stats(
|
|
|
|
|
args.verbosity,
|
|
|
|
|
memory_usage,
|
|
|
|
|
path.as_ref(),
|
|
|
|
|
only.as_ref().map(String::as_ref),
|
|
|
|
|
with_deps,
|
|
|
|
|
randomize,
|
|
|
|
|
load_output_dirs,
|
2020-04-12 18:25:31 +08:00
|
|
|
with_proc_macro,
|
2020-03-16 14:51:44 +01:00
|
|
|
)?,
|
2020-02-17 19:03:03 +01:00
|
|
|
|
2020-04-12 18:25:31 +08:00
|
|
|
args::Command::Bench { path, what, load_output_dirs, with_proc_macro } => {
|
|
|
|
|
cli::analysis_bench(
|
|
|
|
|
args.verbosity,
|
|
|
|
|
path.as_ref(),
|
|
|
|
|
what,
|
|
|
|
|
load_output_dirs,
|
|
|
|
|
with_proc_macro,
|
|
|
|
|
)?
|
2020-02-17 19:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-12 18:25:31 +08:00
|
|
|
args::Command::Diagnostics { path, load_output_dirs, with_proc_macro, all } => {
|
|
|
|
|
cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)?
|
2020-04-13 05:44:35 -07:00
|
|
|
}
|
|
|
|
|
|
2020-04-16 21:13:57 +08:00
|
|
|
args::Command::ProcMacro => run_proc_macro_sv()?,
|
2020-02-17 19:03:03 +01:00
|
|
|
args::Command::RunServer => run_server()?,
|
|
|
|
|
args::Command::Version => println!("rust-analyzer {}", env!("REV")),
|
2019-12-09 15:59:04 +01:00
|
|
|
}
|
2019-08-31 14:47:37 +03:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn setup_logging() -> Result<()> {
|
2019-06-15 12:58:17 +03:00
|
|
|
std::env::set_var("RUST_BACKTRACE", "short");
|
2019-11-29 19:35:03 -05:00
|
|
|
env_logger::try_init()?;
|
2020-02-16 18:04:08 +01:00
|
|
|
ra_prof::init();
|
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
|
|
|
|
2020-04-16 21:13:57 +08:00
|
|
|
fn run_proc_macro_sv() -> Result<()> {
|
|
|
|
|
ra_proc_macro_srv::cli::run();
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
2020-02-18 12:33:16 +01:00
|
|
|
let server_capabilities = serde_json::to_value(rust_analyzer::server_capabilities()).unwrap();
|
2019-03-06 11:34:38 +02:00
|
|
|
|
2019-08-30 20:18:57 +03:00
|
|
|
let initialize_params = connection.initialize(server_capabilities)?;
|
2020-02-11 09:46:45 +01:00
|
|
|
let initialize_params =
|
|
|
|
|
from_json::<lsp_types::InitializeParams>("InitializeParams", initialize_params)?;
|
2019-04-11 08:08:19 +02:00
|
|
|
|
2020-01-13 16:20:47 -05:00
|
|
|
if let Some(client_info) = initialize_params.client_info {
|
|
|
|
|
log::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default());
|
|
|
|
|
}
|
|
|
|
|
|
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]);
|
|
|
|
|
|
2020-04-01 18:41:43 +02:00
|
|
|
let config = {
|
|
|
|
|
let mut config = Config::default();
|
|
|
|
|
if let Some(value) = &initialize_params.initialization_options {
|
|
|
|
|
config.update(value);
|
|
|
|
|
}
|
|
|
|
|
if let Some(caps) = &initialize_params.capabilities.text_document {
|
|
|
|
|
config.update_caps(caps);
|
|
|
|
|
}
|
|
|
|
|
config
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
rust_analyzer::main_loop(workspace_roots, config, connection)?;
|
2019-08-22 11:08:22 +03:00
|
|
|
|
2018-12-06 21:03:39 +03:00
|
|
|
log::info!("shutting down IO...");
|
2019-08-30 17:24:11 +03:00
|
|
|
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
|
|
|
}
|