diff options
Diffstat (limited to 'crates/rust-analyzer/src/bin/main.rs')
-rw-r--r-- | crates/rust-analyzer/src/bin/main.rs | 109 |
1 files changed, 79 insertions, 30 deletions
diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs index bf42654a8..288847980 100644 --- a/crates/rust-analyzer/src/bin/main.rs +++ b/crates/rust-analyzer/src/bin/main.rs | |||
@@ -1,58 +1,106 @@ | |||
1 | //! Driver for rust-analyzer. | 1 | //! Driver for rust-analyzer. |
2 | //! | 2 | //! |
3 | //! Based on cli flags, either spawns an LSP server, or runs a batch analysis | 3 | //! Based on cli flags, either spawns an LSP server, or runs a batch analysis |
4 | mod args; | 4 | mod flags; |
5 | mod logger; | 5 | mod logger; |
6 | 6 | ||
7 | use std::{convert::TryFrom, env, fs, path::PathBuf, process}; | 7 | use std::{convert::TryFrom, env, fs, path::Path, process}; |
8 | 8 | ||
9 | use lsp_server::Connection; | 9 | use lsp_server::Connection; |
10 | use project_model::ProjectManifest; | 10 | use project_model::ProjectManifest; |
11 | use rust_analyzer::{cli, config::Config, from_json, Result}; | 11 | use rust_analyzer::{ |
12 | cli::{self, AnalysisStatsCmd, BenchCmd}, | ||
13 | config::Config, | ||
14 | from_json, | ||
15 | lsp_ext::supports_utf8, | ||
16 | Result, | ||
17 | }; | ||
12 | use vfs::AbsPathBuf; | 18 | use vfs::AbsPathBuf; |
13 | 19 | ||
14 | #[cfg(all(feature = "mimalloc"))] | 20 | #[cfg(all(feature = "mimalloc"))] |
15 | #[global_allocator] | 21 | #[global_allocator] |
16 | static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; | 22 | static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; |
17 | 23 | ||
24 | #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))] | ||
25 | #[global_allocator] | ||
26 | static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; | ||
27 | |||
18 | fn main() { | 28 | fn main() { |
19 | if let Err(err) = try_main() { | 29 | if let Err(err) = try_main() { |
30 | log::error!("Unexpected error: {}", err); | ||
20 | eprintln!("{}", err); | 31 | eprintln!("{}", err); |
21 | process::exit(101); | 32 | process::exit(101); |
22 | } | 33 | } |
23 | } | 34 | } |
24 | 35 | ||
25 | fn try_main() -> Result<()> { | 36 | fn try_main() -> Result<()> { |
26 | let args = args::Args::parse()?; | 37 | let flags = flags::RustAnalyzer::from_env()?; |
27 | setup_logging(args.log_file)?; | 38 | |
28 | match args.command { | 39 | #[cfg(debug_assertions)] |
29 | args::Command::RunServer => run_server()?, | 40 | if flags.wait_dbg || env::var("RA_WAIT_DBG").is_ok() { |
30 | args::Command::PrintConfigSchema => { | 41 | #[allow(unused_mut)] |
31 | println!("{:#}", Config::json_schema()); | 42 | let mut d = 4; |
43 | while d == 4 { | ||
44 | d = 4; | ||
32 | } | 45 | } |
33 | args::Command::ProcMacro => proc_macro_srv::cli::run()?, | 46 | } |
34 | 47 | ||
35 | args::Command::Parse { no_dump } => cli::parse(no_dump)?, | 48 | setup_logging(flags.log_file.as_deref(), flags.no_log_buffering)?; |
36 | args::Command::Symbols => cli::symbols()?, | 49 | let verbosity = flags.verbosity(); |
37 | args::Command::Highlight { rainbow } => cli::highlight(rainbow)?, | 50 | |
38 | args::Command::AnalysisStats(cmd) => cmd.run(args.verbosity)?, | 51 | match flags.subcommand { |
39 | args::Command::Bench(cmd) => cmd.run(args.verbosity)?, | 52 | flags::RustAnalyzerCmd::LspServer(cmd) => { |
40 | args::Command::Diagnostics { path, load_output_dirs, with_proc_macro } => { | 53 | if cmd.print_config_schema { |
41 | cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro)? | 54 | println!("{:#}", Config::json_schema()); |
55 | return Ok(()); | ||
56 | } | ||
57 | if cmd.version { | ||
58 | println!("rust-analyzer {}", env!("REV")); | ||
59 | return Ok(()); | ||
60 | } | ||
61 | if cmd.help { | ||
62 | println!("{}", flags::RustAnalyzer::HELP); | ||
63 | return Ok(()); | ||
64 | } | ||
65 | run_server()? | ||
66 | } | ||
67 | flags::RustAnalyzerCmd::ProcMacro(_) => proc_macro_srv::cli::run()?, | ||
68 | flags::RustAnalyzerCmd::Parse(cmd) => cli::parse(cmd.no_dump)?, | ||
69 | flags::RustAnalyzerCmd::Symbols(_) => cli::symbols()?, | ||
70 | flags::RustAnalyzerCmd::Highlight(cmd) => cli::highlight(cmd.rainbow)?, | ||
71 | flags::RustAnalyzerCmd::AnalysisStats(cmd) => AnalysisStatsCmd { | ||
72 | randomize: cmd.randomize, | ||
73 | parallel: cmd.parallel, | ||
74 | memory_usage: cmd.memory_usage, | ||
75 | only: cmd.only, | ||
76 | with_deps: cmd.with_deps, | ||
77 | path: cmd.path, | ||
78 | load_output_dirs: cmd.load_output_dirs, | ||
79 | with_proc_macro: cmd.with_proc_macro, | ||
42 | } | 80 | } |
43 | args::Command::Ssr { rules } => { | 81 | .run(verbosity)?, |
44 | cli::apply_ssr_rules(rules)?; | 82 | flags::RustAnalyzerCmd::AnalysisBench(cmd) => { |
83 | let what = cmd.what(); | ||
84 | BenchCmd { | ||
85 | memory_usage: cmd.memory_usage, | ||
86 | path: cmd.path, | ||
87 | load_output_dirs: cmd.load_output_dirs, | ||
88 | with_proc_macro: cmd.with_proc_macro, | ||
89 | what, | ||
90 | } | ||
91 | .run(verbosity)? | ||
45 | } | 92 | } |
46 | args::Command::StructuredSearch { patterns, debug_snippet } => { | 93 | |
47 | cli::search_for_patterns(patterns, debug_snippet)?; | 94 | flags::RustAnalyzerCmd::Diagnostics(cmd) => { |
95 | cli::diagnostics(&cmd.path, cmd.load_output_dirs, cmd.with_proc_macro)? | ||
48 | } | 96 | } |
49 | args::Command::Version => println!("rust-analyzer {}", env!("REV")), | 97 | flags::RustAnalyzerCmd::Ssr(cmd) => cli::apply_ssr_rules(cmd.rule)?, |
50 | args::Command::Help => {} | 98 | flags::RustAnalyzerCmd::Search(cmd) => cli::search_for_patterns(cmd.pattern, cmd.debug)?, |
51 | } | 99 | } |
52 | Ok(()) | 100 | Ok(()) |
53 | } | 101 | } |
54 | 102 | ||
55 | fn setup_logging(log_file: Option<PathBuf>) -> Result<()> { | 103 | fn setup_logging(log_file: Option<&Path>, no_buffering: bool) -> Result<()> { |
56 | env::set_var("RUST_BACKTRACE", "short"); | 104 | env::set_var("RUST_BACKTRACE", "short"); |
57 | 105 | ||
58 | let log_file = match log_file { | 106 | let log_file = match log_file { |
@@ -65,16 +113,12 @@ fn setup_logging(log_file: Option<PathBuf>) -> Result<()> { | |||
65 | None => None, | 113 | None => None, |
66 | }; | 114 | }; |
67 | let filter = env::var("RA_LOG").ok(); | 115 | let filter = env::var("RA_LOG").ok(); |
68 | logger::Logger::new(log_file, filter.as_deref()).install(); | 116 | logger::Logger::new(log_file, no_buffering, filter.as_deref()).install(); |
69 | 117 | ||
70 | tracing_setup::setup_tracing()?; | 118 | tracing_setup::setup_tracing()?; |
71 | 119 | ||
72 | profile::init(); | 120 | profile::init(); |
73 | 121 | ||
74 | if !cfg!(debug_assertions) { | ||
75 | stdx::set_assert_hook(|loc, args| log::error!("assertion failed at {}: {}", loc, args)); | ||
76 | } | ||
77 | |||
78 | Ok(()) | 122 | Ok(()) |
79 | } | 123 | } |
80 | 124 | ||
@@ -116,6 +160,11 @@ fn run_server() -> Result<()> { | |||
116 | name: String::from("rust-analyzer"), | 160 | name: String::from("rust-analyzer"), |
117 | version: Some(String::from(env!("REV"))), | 161 | version: Some(String::from(env!("REV"))), |
118 | }), | 162 | }), |
163 | offset_encoding: if supports_utf8(&initialize_params.capabilities) { | ||
164 | Some("utf-8".to_string()) | ||
165 | } else { | ||
166 | None | ||
167 | }, | ||
119 | }; | 168 | }; |
120 | 169 | ||
121 | let initialize_result = serde_json::to_value(initialize_result).unwrap(); | 170 | let initialize_result = serde_json::to_value(initialize_result).unwrap(); |