From 59e1207dac8eb9cc56a72ee685bd4f143683d2bb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Feb 2020 11:56:18 +0100 Subject: Better folder structure --- crates/ra_lsp_server/Cargo.toml | 5 + crates/ra_lsp_server/src/args.rs | 242 ----------------------------------- crates/ra_lsp_server/src/bin/args.rs | 242 +++++++++++++++++++++++++++++++++++ crates/ra_lsp_server/src/bin/main.rs | 96 ++++++++++++++ crates/ra_lsp_server/src/main.rs | 96 -------------- xtask/tests/tidy-tests/docs.rs | 1 - 6 files changed, 343 insertions(+), 339 deletions(-) delete mode 100644 crates/ra_lsp_server/src/args.rs create mode 100644 crates/ra_lsp_server/src/bin/args.rs create mode 100644 crates/ra_lsp_server/src/bin/main.rs delete mode 100644 crates/ra_lsp_server/src/main.rs diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index da523ba8a..151ca3da5 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml @@ -3,10 +3,15 @@ edition = "2018" name = "ra_lsp_server" version = "0.1.0" authors = ["rust-analyzer developers"] +autobins = false [lib] doctest = false +[[bin]] +name = "ra_lsp_server" +path = "./src/bin/main.rs" + [dependencies] anyhow = "1.0" crossbeam-channel = "0.4" diff --git a/crates/ra_lsp_server/src/args.rs b/crates/ra_lsp_server/src/args.rs deleted file mode 100644 index 3890fe13a..000000000 --- a/crates/ra_lsp_server/src/args.rs +++ /dev/null @@ -1,242 +0,0 @@ -//! Command like parsing for rust-analyzer. -//! -//! If run started args, we run the LSP server loop. With a subcommand, we do a -//! one-time batch processing. - -use anyhow::{bail, Result}; -use pico_args::Arguments; -use ra_lsp_server::cli::{BenchWhat, Position, Verbosity}; - -use std::{fmt::Write, path::PathBuf}; - -pub(crate) struct Args { - pub(crate) verbosity: Verbosity, - pub(crate) command: Command, -} - -pub(crate) enum Command { - Parse { - no_dump: bool, - }, - Symbols, - Highlight { - rainbow: bool, - }, - Stats { - randomize: bool, - memory_usage: bool, - only: Option, - with_deps: bool, - path: PathBuf, - }, - Bench { - path: PathBuf, - what: BenchWhat, - }, - RunServer, - Version, -} - -impl Args { - pub(crate) fn parse() -> Result> { - let mut matches = Arguments::from_env(); - - if matches.contains("--version") { - matches.finish().or_else(handle_extra_flags)?; - return Ok(Ok(Args { verbosity: Verbosity::Normal, command: Command::Version })); - } - - let verbosity = match ( - matches.contains(["-vv", "--spammy"]), - matches.contains(["-v", "--verbose"]), - matches.contains(["-q", "--quiet"]), - ) { - (true, _, true) => bail!("Invalid flags: -q conflicts with -vv"), - (true, _, false) => Verbosity::Spammy, - (false, false, false) => Verbosity::Normal, - (false, false, true) => Verbosity::Quiet, - (false, true, false) => Verbosity::Verbose, - (false, true, true) => bail!("Invalid flags: -q conflicts with -v"), - }; - - let subcommand = match matches.subcommand()? { - Some(it) => it, - None => { - matches.finish().or_else(handle_extra_flags)?; - return Ok(Ok(Args { verbosity, command: Command::RunServer })); - } - }; - let command = match subcommand.as_str() { - "parse" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ -ra-cli-parse - -USAGE: - ra_lsp_server parse [FLAGS] - -FLAGS: - -h, --help Prints help inforamtion - --no-dump" - ); - return Ok(Err(HelpPrinted)); - } - - let no_dump = matches.contains("--no-dump"); - matches.finish().or_else(handle_extra_flags)?; - Command::Parse { no_dump } - } - "symbols" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ -ra-cli-symbols - -USAGE: - ra_lsp_server highlight [FLAGS] - -FLAGS: - -h, --help Prints help inforamtion" - ); - return Ok(Err(HelpPrinted)); - } - - matches.finish().or_else(handle_extra_flags)?; - - Command::Symbols - } - "highlight" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ -ra-cli-highlight - -USAGE: - ra_lsp_server highlight [FLAGS] - -FLAGS: - -h, --help Prints help information - -r, --rainbow" - ); - return Ok(Err(HelpPrinted)); - } - - let rainbow = matches.contains(["-r", "--rainbow"]); - matches.finish().or_else(handle_extra_flags)?; - Command::Highlight { rainbow } - } - "analysis-stats" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ -ra-cli-analysis-stats - -USAGE: - ra_lsp_server analysis-stats [FLAGS] [OPTIONS] [PATH] - -FLAGS: - -h, --help Prints help information - --memory-usage - -v, --verbose - -q, --quiet - -OPTIONS: - -o - -ARGS: - " - ); - return Ok(Err(HelpPrinted)); - } - - let randomize = matches.contains("--randomize"); - let memory_usage = matches.contains("--memory-usage"); - let only: Option = matches.opt_value_from_str(["-o", "--only"])?; - let with_deps: bool = matches.contains("--with-deps"); - let path = { - let mut trailing = matches.free()?; - if trailing.len() != 1 { - bail!("Invalid flags"); - } - trailing.pop().unwrap().into() - }; - - Command::Stats { randomize, memory_usage, only, with_deps, path } - } - "analysis-bench" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ -ra_lsp_server-analysis-bench - -USAGE: - ra_lsp_server analysis-bench [FLAGS] [OPTIONS] [PATH] - -FLAGS: - -h, --help Prints help information - -v, --verbose - -OPTIONS: - --complete Compute completions at this location - --highlight Hightlight this file - -ARGS: - Project to analyse" - ); - return Ok(Err(HelpPrinted)); - } - - let path: PathBuf = matches.opt_value_from_str("--path")?.unwrap_or_default(); - let highlight_path: Option = matches.opt_value_from_str("--highlight")?; - let complete_path: Option = matches.opt_value_from_str("--complete")?; - let goto_def_path: Option = matches.opt_value_from_str("--goto-def")?; - let what = match (highlight_path, complete_path, goto_def_path) { - (Some(path), None, None) => BenchWhat::Highlight { path: path.into() }, - (None, Some(position), None) => BenchWhat::Complete(position), - (None, None, Some(position)) => BenchWhat::GotoDef(position), - _ => panic!( - "exactly one of `--highlight`, `--complete` or `--goto-def` must be set" - ), - }; - Command::Bench { path, what } - } - _ => { - eprintln!( - "\ -ra-cli - -USAGE: - ra_lsp_server - -FLAGS: - -h, --help Prints help information - -SUBCOMMANDS: - analysis-bench - analysis-stats - highlight - parse - symbols" - ); - return Ok(Err(HelpPrinted)); - } - }; - Ok(Ok(Args { verbosity, command })) - } -} - -pub(crate) struct HelpPrinted; - -fn handle_extra_flags(e: pico_args::Error) -> Result<()> { - if let pico_args::Error::UnusedArgsLeft(flags) = e { - let mut invalid_flags = String::new(); - for flag in flags { - write!(&mut invalid_flags, "{}, ", flag)?; - } - let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); - bail!("Invalid flags: {}", invalid_flags); - } else { - bail!(e); - } -} diff --git a/crates/ra_lsp_server/src/bin/args.rs b/crates/ra_lsp_server/src/bin/args.rs new file mode 100644 index 000000000..3890fe13a --- /dev/null +++ b/crates/ra_lsp_server/src/bin/args.rs @@ -0,0 +1,242 @@ +//! Command like parsing for rust-analyzer. +//! +//! If run started args, we run the LSP server loop. With a subcommand, we do a +//! one-time batch processing. + +use anyhow::{bail, Result}; +use pico_args::Arguments; +use ra_lsp_server::cli::{BenchWhat, Position, Verbosity}; + +use std::{fmt::Write, path::PathBuf}; + +pub(crate) struct Args { + pub(crate) verbosity: Verbosity, + pub(crate) command: Command, +} + +pub(crate) enum Command { + Parse { + no_dump: bool, + }, + Symbols, + Highlight { + rainbow: bool, + }, + Stats { + randomize: bool, + memory_usage: bool, + only: Option, + with_deps: bool, + path: PathBuf, + }, + Bench { + path: PathBuf, + what: BenchWhat, + }, + RunServer, + Version, +} + +impl Args { + pub(crate) fn parse() -> Result> { + let mut matches = Arguments::from_env(); + + if matches.contains("--version") { + matches.finish().or_else(handle_extra_flags)?; + return Ok(Ok(Args { verbosity: Verbosity::Normal, command: Command::Version })); + } + + let verbosity = match ( + matches.contains(["-vv", "--spammy"]), + matches.contains(["-v", "--verbose"]), + matches.contains(["-q", "--quiet"]), + ) { + (true, _, true) => bail!("Invalid flags: -q conflicts with -vv"), + (true, _, false) => Verbosity::Spammy, + (false, false, false) => Verbosity::Normal, + (false, false, true) => Verbosity::Quiet, + (false, true, false) => Verbosity::Verbose, + (false, true, true) => bail!("Invalid flags: -q conflicts with -v"), + }; + + let subcommand = match matches.subcommand()? { + Some(it) => it, + None => { + matches.finish().or_else(handle_extra_flags)?; + return Ok(Ok(Args { verbosity, command: Command::RunServer })); + } + }; + let command = match subcommand.as_str() { + "parse" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ +ra-cli-parse + +USAGE: + ra_lsp_server parse [FLAGS] + +FLAGS: + -h, --help Prints help inforamtion + --no-dump" + ); + return Ok(Err(HelpPrinted)); + } + + let no_dump = matches.contains("--no-dump"); + matches.finish().or_else(handle_extra_flags)?; + Command::Parse { no_dump } + } + "symbols" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ +ra-cli-symbols + +USAGE: + ra_lsp_server highlight [FLAGS] + +FLAGS: + -h, --help Prints help inforamtion" + ); + return Ok(Err(HelpPrinted)); + } + + matches.finish().or_else(handle_extra_flags)?; + + Command::Symbols + } + "highlight" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ +ra-cli-highlight + +USAGE: + ra_lsp_server highlight [FLAGS] + +FLAGS: + -h, --help Prints help information + -r, --rainbow" + ); + return Ok(Err(HelpPrinted)); + } + + let rainbow = matches.contains(["-r", "--rainbow"]); + matches.finish().or_else(handle_extra_flags)?; + Command::Highlight { rainbow } + } + "analysis-stats" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ +ra-cli-analysis-stats + +USAGE: + ra_lsp_server analysis-stats [FLAGS] [OPTIONS] [PATH] + +FLAGS: + -h, --help Prints help information + --memory-usage + -v, --verbose + -q, --quiet + +OPTIONS: + -o + +ARGS: + " + ); + return Ok(Err(HelpPrinted)); + } + + let randomize = matches.contains("--randomize"); + let memory_usage = matches.contains("--memory-usage"); + let only: Option = matches.opt_value_from_str(["-o", "--only"])?; + let with_deps: bool = matches.contains("--with-deps"); + let path = { + let mut trailing = matches.free()?; + if trailing.len() != 1 { + bail!("Invalid flags"); + } + trailing.pop().unwrap().into() + }; + + Command::Stats { randomize, memory_usage, only, with_deps, path } + } + "analysis-bench" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ +ra_lsp_server-analysis-bench + +USAGE: + ra_lsp_server analysis-bench [FLAGS] [OPTIONS] [PATH] + +FLAGS: + -h, --help Prints help information + -v, --verbose + +OPTIONS: + --complete Compute completions at this location + --highlight Hightlight this file + +ARGS: + Project to analyse" + ); + return Ok(Err(HelpPrinted)); + } + + let path: PathBuf = matches.opt_value_from_str("--path")?.unwrap_or_default(); + let highlight_path: Option = matches.opt_value_from_str("--highlight")?; + let complete_path: Option = matches.opt_value_from_str("--complete")?; + let goto_def_path: Option = matches.opt_value_from_str("--goto-def")?; + let what = match (highlight_path, complete_path, goto_def_path) { + (Some(path), None, None) => BenchWhat::Highlight { path: path.into() }, + (None, Some(position), None) => BenchWhat::Complete(position), + (None, None, Some(position)) => BenchWhat::GotoDef(position), + _ => panic!( + "exactly one of `--highlight`, `--complete` or `--goto-def` must be set" + ), + }; + Command::Bench { path, what } + } + _ => { + eprintln!( + "\ +ra-cli + +USAGE: + ra_lsp_server + +FLAGS: + -h, --help Prints help information + +SUBCOMMANDS: + analysis-bench + analysis-stats + highlight + parse + symbols" + ); + return Ok(Err(HelpPrinted)); + } + }; + Ok(Ok(Args { verbosity, command })) + } +} + +pub(crate) struct HelpPrinted; + +fn handle_extra_flags(e: pico_args::Error) -> Result<()> { + if let pico_args::Error::UnusedArgsLeft(flags) = e { + let mut invalid_flags = String::new(); + for flag in flags { + write!(&mut invalid_flags, "{}, ", flag)?; + } + let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); + bail!("Invalid flags: {}", invalid_flags); + } else { + bail!(e); + } +} diff --git a/crates/ra_lsp_server/src/bin/main.rs b/crates/ra_lsp_server/src/bin/main.rs new file mode 100644 index 000000000..a549e5ff1 --- /dev/null +++ b/crates/ra_lsp_server/src/bin/main.rs @@ -0,0 +1,96 @@ +//! `ra_lsp_server` binary +mod args; + +use lsp_server::Connection; +use ra_lsp_server::{cli, from_json, show_message, Result, ServerConfig}; +use ra_prof; + +use crate::args::HelpPrinted; + +fn main() -> Result<()> { + setup_logging()?; + 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)?, + args::Command::Stats { randomize, memory_usage, only, with_deps, path } => { + cli::analysis_stats( + args.verbosity, + memory_usage, + path.as_ref(), + only.as_ref().map(String::as_ref), + with_deps, + randomize, + )? + } + + args::Command::Bench { path, what } => { + cli::analysis_bench(args.verbosity, path.as_ref(), what)? + } + + args::Command::RunServer => run_server()?, + args::Command::Version => println!("rust-analyzer {}", env!("REV")), + } + Ok(()) +} + +fn setup_logging() -> Result<()> { + std::env::set_var("RUST_BACKTRACE", "short"); + env_logger::try_init()?; + ra_prof::init(); + Ok(()) +} + +fn run_server() -> Result<()> { + log::info!("lifecycle: server started"); + + let (connection, io_threads) = Connection::stdio(); + let server_capabilities = serde_json::to_value(ra_lsp_server::server_capabilities()).unwrap(); + + let initialize_params = connection.initialize(server_capabilities)?; + let initialize_params = + from_json::("InitializeParams", initialize_params)?; + + if let Some(client_info) = initialize_params.client_info { + log::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default()); + } + + let cwd = std::env::current_dir()?; + 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::>() + }) + .filter(|workspaces| !workspaces.is_empty()) + .unwrap_or_else(|| vec![root]); + + let server_config = initialize_params + .initialization_options + .and_then(|v| { + from_json::("config", v) + .map_err(|e| { + log::error!("{}", e); + show_message(lsp_types::MessageType::Error, e.to_string(), &connection.sender); + }) + .ok() + }) + .unwrap_or_default(); + + ra_lsp_server::main_loop( + workspace_roots, + initialize_params.capabilities, + server_config, + connection, + )?; + + log::info!("shutting down IO..."); + io_threads.join()?; + log::info!("... IO is down"); + Ok(()) +} diff --git a/crates/ra_lsp_server/src/main.rs b/crates/ra_lsp_server/src/main.rs deleted file mode 100644 index a549e5ff1..000000000 --- a/crates/ra_lsp_server/src/main.rs +++ /dev/null @@ -1,96 +0,0 @@ -//! `ra_lsp_server` binary -mod args; - -use lsp_server::Connection; -use ra_lsp_server::{cli, from_json, show_message, Result, ServerConfig}; -use ra_prof; - -use crate::args::HelpPrinted; - -fn main() -> Result<()> { - setup_logging()?; - 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)?, - args::Command::Stats { randomize, memory_usage, only, with_deps, path } => { - cli::analysis_stats( - args.verbosity, - memory_usage, - path.as_ref(), - only.as_ref().map(String::as_ref), - with_deps, - randomize, - )? - } - - args::Command::Bench { path, what } => { - cli::analysis_bench(args.verbosity, path.as_ref(), what)? - } - - args::Command::RunServer => run_server()?, - args::Command::Version => println!("rust-analyzer {}", env!("REV")), - } - Ok(()) -} - -fn setup_logging() -> Result<()> { - std::env::set_var("RUST_BACKTRACE", "short"); - env_logger::try_init()?; - ra_prof::init(); - Ok(()) -} - -fn run_server() -> Result<()> { - log::info!("lifecycle: server started"); - - let (connection, io_threads) = Connection::stdio(); - let server_capabilities = serde_json::to_value(ra_lsp_server::server_capabilities()).unwrap(); - - let initialize_params = connection.initialize(server_capabilities)?; - let initialize_params = - from_json::("InitializeParams", initialize_params)?; - - if let Some(client_info) = initialize_params.client_info { - log::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default()); - } - - let cwd = std::env::current_dir()?; - 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::>() - }) - .filter(|workspaces| !workspaces.is_empty()) - .unwrap_or_else(|| vec![root]); - - let server_config = initialize_params - .initialization_options - .and_then(|v| { - from_json::("config", v) - .map_err(|e| { - log::error!("{}", e); - show_message(lsp_types::MessageType::Error, e.to_string(), &connection.sender); - }) - .ok() - }) - .unwrap_or_default(); - - ra_lsp_server::main_loop( - workspace_roots, - initialize_params.capabilities, - server_config, - connection, - )?; - - log::info!("shutting down IO..."); - io_threads.join()?; - log::info!("... IO is down"); - Ok(()) -} diff --git a/xtask/tests/tidy-tests/docs.rs b/xtask/tests/tidy-tests/docs.rs index b2571df25..62c4f8441 100644 --- a/xtask/tests/tidy-tests/docs.rs +++ b/xtask/tests/tidy-tests/docs.rs @@ -77,7 +77,6 @@ fn no_docs_comments() { "ra_hir", "ra_hir_expand", "ra_ide", - "ra_lsp_server", "ra_mbe", "ra_parser", "ra_prof", -- cgit v1.2.3 From 93b969003d0a9448d4207d9d5df9dde63f9444be Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Feb 2020 12:11:32 +0100 Subject: Some docs --- crates/ra_lsp_server/src/bin/main.rs | 4 +++- crates/ra_lsp_server/src/cli.rs | 2 +- crates/ra_lsp_server/src/cli/analysis_bench.rs | 2 +- crates/ra_lsp_server/src/cli/analysis_stats.rs | 3 ++- crates/ra_lsp_server/src/cli/load_cargo.rs | 18 ++++++++++-------- crates/ra_lsp_server/src/lib.rs | 7 +++++-- crates/ra_lsp_server/src/vfs_glob.rs | 2 +- 7 files changed, 23 insertions(+), 15 deletions(-) diff --git a/crates/ra_lsp_server/src/bin/main.rs b/crates/ra_lsp_server/src/bin/main.rs index a549e5ff1..e25d54a0d 100644 --- a/crates/ra_lsp_server/src/bin/main.rs +++ b/crates/ra_lsp_server/src/bin/main.rs @@ -1,4 +1,6 @@ -//! `ra_lsp_server` binary +//! Driver for rust-analyzer. +//! +//! Based on cli flags, either spawns an LSP server, or runs a batch analysis mod args; use lsp_server::Connection; diff --git a/crates/ra_lsp_server/src/cli.rs b/crates/ra_lsp_server/src/cli.rs index 3c7b8e250..c9738d101 100644 --- a/crates/ra_lsp_server/src/cli.rs +++ b/crates/ra_lsp_server/src/cli.rs @@ -1,4 +1,4 @@ -//! FIXME: write short doc here +//! Various batch processing tasks, intended primarily for debugging. mod load_cargo; mod analysis_stats; diff --git a/crates/ra_lsp_server/src/cli/analysis_bench.rs b/crates/ra_lsp_server/src/cli/analysis_bench.rs index e00f81073..91855e592 100644 --- a/crates/ra_lsp_server/src/cli/analysis_bench.rs +++ b/crates/ra_lsp_server/src/cli/analysis_bench.rs @@ -1,4 +1,4 @@ -//! FIXME: write short doc here +//! Benchmark operations like highlighting or goto definition. use std::{ path::{Path, PathBuf}, diff --git a/crates/ra_lsp_server/src/cli/analysis_stats.rs b/crates/ra_lsp_server/src/cli/analysis_stats.rs index c27fabe3c..99ab6e443 100644 --- a/crates/ra_lsp_server/src/cli/analysis_stats.rs +++ b/crates/ra_lsp_server/src/cli/analysis_stats.rs @@ -1,4 +1,5 @@ -//! FIXME: write short doc here +//! Fully type-check project and print various stats, like the number of type +//! errors. use std::{collections::HashSet, fmt::Write, path::Path, time::Instant}; diff --git a/crates/ra_lsp_server/src/cli/load_cargo.rs b/crates/ra_lsp_server/src/cli/load_cargo.rs index bb3e1513b..8cd08ecb6 100644 --- a/crates/ra_lsp_server/src/cli/load_cargo.rs +++ b/crates/ra_lsp_server/src/cli/load_cargo.rs @@ -1,18 +1,18 @@ -//! FIXME: write short doc here +//! Loads a Cargo project into a static instance of analysis, without support +//! for incorporating changes. -use std::{collections::HashSet, path::Path}; +use std::path::Path; +use anyhow::Result; use crossbeam_channel::{unbounded, Receiver}; use ra_db::{CrateGraph, FileId, SourceRootId}; use ra_ide::{AnalysisChange, AnalysisHost, FeatureFlags}; use ra_project_model::{get_rustc_cfg_options, PackageRoot, ProjectWorkspace}; use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch}; -use rustc_hash::FxHashMap; +use rustc_hash::{FxHashMap, FxHashSet}; use crate::vfs_glob::RustPackageFilterBuilder; -use anyhow::Result; - fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId { FileId(f.0) } @@ -20,7 +20,9 @@ fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId { SourceRootId(r.0) } -pub fn load_cargo(root: &Path) -> Result<(AnalysisHost, FxHashMap)> { +pub(crate) fn load_cargo( + root: &Path, +) -> Result<(AnalysisHost, FxHashMap)> { let root = std::env::current_dir()?.join(root); let ws = ProjectWorkspace::discover(root.as_ref(), &Default::default())?; let project_roots = ws.to_roots(); @@ -74,7 +76,7 @@ pub fn load_cargo(root: &Path) -> Result<(AnalysisHost, FxHashMap, crate_graph: CrateGraph, vfs: &mut Vfs, @@ -86,7 +88,7 @@ pub fn load( analysis_change.set_crate_graph(crate_graph); // wait until Vfs has loaded all roots - let mut roots_loaded = HashSet::new(); + let mut roots_loaded = FxHashSet::default(); for task in receiver { vfs.handle_task(task); let mut done = false; diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs index 958c70fe5..0dae30e46 100644 --- a/crates/ra_lsp_server/src/lib.rs +++ b/crates/ra_lsp_server/src/lib.rs @@ -1,10 +1,13 @@ //! Implementation of the LSP for rust-analyzer. //! -//! This crate takes Rust-specific analysis results from ra_ide and -//! translates into LSP types. +//! This crate takes Rust-specific analysis results from ra_ide and translates +//! into LSP types. //! //! It also is the root of all state. `world` module defines the bulk of the //! state, and `main_loop` module defines the rules for modifying it. +//! +//! The `cli` submodule implements some batch-processing analysis, primarily as +//! a debugging aid. #![recursion_limit = "512"] pub mod cli; diff --git a/crates/ra_lsp_server/src/vfs_glob.rs b/crates/ra_lsp_server/src/vfs_glob.rs index 12401d75a..91b33f94e 100644 --- a/crates/ra_lsp_server/src/vfs_glob.rs +++ b/crates/ra_lsp_server/src/vfs_glob.rs @@ -1,4 +1,4 @@ -//! `ra_vfs_glob` crate implements exclusion rules for vfs. +//! Exclusion rules for vfs. //! //! By default, we include only `.rs` files, and skip some know offenders like //! `/target` or `/node_modules` altogether. -- cgit v1.2.3 From b6740060f60fa016ac1c3d420c9ac919d31f6997 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Feb 2020 12:15:12 +0100 Subject: Refactor --- crates/ra_lsp_server/src/cargo_target_spec.rs | 4 +--- crates/ra_lsp_server/src/main_loop/handlers.rs | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/ra_lsp_server/src/cargo_target_spec.rs b/crates/ra_lsp_server/src/cargo_target_spec.rs index 5fd1e7b6b..56d3f0de4 100644 --- a/crates/ra_lsp_server/src/cargo_target_spec.rs +++ b/crates/ra_lsp_server/src/cargo_target_spec.rs @@ -6,11 +6,9 @@ use ra_project_model::{self, ProjectWorkspace, TargetKind}; use crate::{world::WorldSnapshot, Result}; pub(crate) fn runnable_args( - world: &WorldSnapshot, - file_id: FileId, + spec: Option, kind: &RunnableKind, ) -> Result> { - let spec = CargoTargetSpec::for_file(world, file_id)?; let mut res = Vec::new(); match kind { RunnableKind::Test { test_id } => { diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index ae51141cb..833f31d96 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -921,7 +921,8 @@ fn to_lsp_runnable( file_id: FileId, runnable: Runnable, ) -> Result { - let args = runnable_args(world, file_id, &runnable.kind)?; + let spec: Option = CargoTargetSpec::for_file(world, file_id)?; + let args = runnable_args(spec, &runnable.kind)?; let line_index = world.analysis().file_line_index(file_id)?; let label = match &runnable.kind { RunnableKind::Test { test_id } => format!("test {}", test_id), -- cgit v1.2.3 From 42c766b2bdb88afbde051db064f255aa71c75fbd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Feb 2020 12:16:40 +0100 Subject: Reduce visibility --- crates/ra_lsp_server/src/cargo_target_spec.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/ra_lsp_server/src/cargo_target_spec.rs b/crates/ra_lsp_server/src/cargo_target_spec.rs index 56d3f0de4..e08cc30e2 100644 --- a/crates/ra_lsp_server/src/cargo_target_spec.rs +++ b/crates/ra_lsp_server/src/cargo_target_spec.rs @@ -54,14 +54,17 @@ pub(crate) fn runnable_args( Ok(res) } -pub struct CargoTargetSpec { - pub package: String, - pub target: String, - pub target_kind: TargetKind, +pub(crate) struct CargoTargetSpec { + pub(crate) package: String, + pub(crate) target: String, + pub(crate) target_kind: TargetKind, } impl CargoTargetSpec { - pub fn for_file(world: &WorldSnapshot, file_id: FileId) -> Result> { + pub(crate) fn for_file( + world: &WorldSnapshot, + file_id: FileId, + ) -> Result> { let &crate_id = match world.analysis().crate_for(file_id)?.first() { Some(crate_id) => crate_id, None => return Ok(None), @@ -82,7 +85,7 @@ impl CargoTargetSpec { Ok(res) } - pub fn push_to(self, buf: &mut Vec) { + pub(crate) fn push_to(self, buf: &mut Vec) { buf.push("--package".to_string()); buf.push(self.package); match self.target_kind { -- cgit v1.2.3 From 1f142d79ed251db58570a5863b06c8826221f9c9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Feb 2020 12:17:47 +0100 Subject: Refactor --- crates/ra_lsp_server/src/cargo_target_spec.rs | 98 +++++++++++++------------- crates/ra_lsp_server/src/main_loop/handlers.rs | 6 +- 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/crates/ra_lsp_server/src/cargo_target_spec.rs b/crates/ra_lsp_server/src/cargo_target_spec.rs index e08cc30e2..d0a52670a 100644 --- a/crates/ra_lsp_server/src/cargo_target_spec.rs +++ b/crates/ra_lsp_server/src/cargo_target_spec.rs @@ -5,55 +5,6 @@ use ra_project_model::{self, ProjectWorkspace, TargetKind}; use crate::{world::WorldSnapshot, Result}; -pub(crate) fn runnable_args( - spec: Option, - kind: &RunnableKind, -) -> Result> { - let mut res = Vec::new(); - match kind { - RunnableKind::Test { test_id } => { - res.push("test".to_string()); - if let Some(spec) = spec { - spec.push_to(&mut res); - } - res.push("--".to_string()); - res.push(test_id.to_string()); - if let TestId::Path(_) = test_id { - res.push("--exact".to_string()); - } - res.push("--nocapture".to_string()); - } - RunnableKind::TestMod { path } => { - res.push("test".to_string()); - if let Some(spec) = spec { - spec.push_to(&mut res); - } - res.push("--".to_string()); - res.push(path.to_string()); - res.push("--nocapture".to_string()); - } - RunnableKind::Bench { test_id } => { - res.push("bench".to_string()); - if let Some(spec) = spec { - spec.push_to(&mut res); - } - res.push("--".to_string()); - res.push(test_id.to_string()); - if let TestId::Path(_) = test_id { - res.push("--exact".to_string()); - } - res.push("--nocapture".to_string()); - } - RunnableKind::Bin => { - res.push("run".to_string()); - if let Some(spec) = spec { - spec.push_to(&mut res); - } - } - } - Ok(res) -} - pub(crate) struct CargoTargetSpec { pub(crate) package: String, pub(crate) target: String, @@ -61,6 +12,55 @@ pub(crate) struct CargoTargetSpec { } impl CargoTargetSpec { + pub(crate) fn runnable_args( + spec: Option, + kind: &RunnableKind, + ) -> Result> { + let mut res = Vec::new(); + match kind { + RunnableKind::Test { test_id } => { + res.push("test".to_string()); + if let Some(spec) = spec { + spec.push_to(&mut res); + } + res.push("--".to_string()); + res.push(test_id.to_string()); + if let TestId::Path(_) = test_id { + res.push("--exact".to_string()); + } + res.push("--nocapture".to_string()); + } + RunnableKind::TestMod { path } => { + res.push("test".to_string()); + if let Some(spec) = spec { + spec.push_to(&mut res); + } + res.push("--".to_string()); + res.push(path.to_string()); + res.push("--nocapture".to_string()); + } + RunnableKind::Bench { test_id } => { + res.push("bench".to_string()); + if let Some(spec) = spec { + spec.push_to(&mut res); + } + res.push("--".to_string()); + res.push(test_id.to_string()); + if let TestId::Path(_) = test_id { + res.push("--exact".to_string()); + } + res.push("--nocapture".to_string()); + } + RunnableKind::Bin => { + res.push("run".to_string()); + if let Some(spec) = spec { + spec.push_to(&mut res); + } + } + } + Ok(res) + } + pub(crate) fn for_file( world: &WorldSnapshot, file_id: FileId, diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 833f31d96..92f219e28 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -29,7 +29,7 @@ use serde::{Deserialize, Serialize}; use serde_json::to_value; use crate::{ - cargo_target_spec::{runnable_args, CargoTargetSpec}, + cargo_target_spec::CargoTargetSpec, conv::{ to_call_hierarchy_item, to_location, Conv, ConvWith, FoldConvCtx, MapConvWith, TryConvWith, TryConvWithToVec, @@ -921,8 +921,8 @@ fn to_lsp_runnable( file_id: FileId, runnable: Runnable, ) -> Result { - let spec: Option = CargoTargetSpec::for_file(world, file_id)?; - let args = runnable_args(spec, &runnable.kind)?; + let spec = CargoTargetSpec::for_file(world, file_id)?; + let args = CargoTargetSpec::runnable_args(spec, &runnable.kind)?; let line_index = world.analysis().file_line_index(file_id)?; let label = match &runnable.kind { RunnableKind::Test { test_id } => format!("test {}", test_id), -- cgit v1.2.3 From 4d307ff8024c8d2d533bc3ab7aac1d63ca5c5977 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Feb 2020 12:25:26 +0100 Subject: Fully document ra_lsp_server --- crates/ra_lsp_server/src/cargo_target_spec.rs | 6 +++++- crates/ra_lsp_server/src/conv.rs | 3 ++- crates/ra_lsp_server/src/diagnostics.rs | 4 +++- crates/ra_lsp_server/src/main_loop.rs | 4 ++-- crates/ra_lsp_server/src/main_loop/handlers.rs | 5 +++-- crates/ra_lsp_server/src/main_loop/pending_requests.rs | 2 +- crates/ra_lsp_server/src/main_loop/subscriptions.rs | 3 ++- crates/ra_lsp_server/src/world.rs | 4 ++-- 8 files changed, 20 insertions(+), 11 deletions(-) diff --git a/crates/ra_lsp_server/src/cargo_target_spec.rs b/crates/ra_lsp_server/src/cargo_target_spec.rs index d0a52670a..53751aafb 100644 --- a/crates/ra_lsp_server/src/cargo_target_spec.rs +++ b/crates/ra_lsp_server/src/cargo_target_spec.rs @@ -1,10 +1,14 @@ -//! FIXME: write short doc here +//! See `CargoTargetSpec` use ra_ide::{FileId, RunnableKind, TestId}; use ra_project_model::{self, ProjectWorkspace, TargetKind}; use crate::{world::WorldSnapshot, Result}; +/// Abstract representation of Cargo target. +/// +/// We use it to cook up the set of cli args we need to pass to Cargo to +/// build/test/run the target. pub(crate) struct CargoTargetSpec { pub(crate) package: String, pub(crate) target: String, diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 8af74b211..90ef74056 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -1,4 +1,5 @@ -//! Convenience module responsible for translating between rust-analyzer's types and LSP types. +//! Convenience module responsible for translating between rust-analyzer's types +//! and LSP types. use lsp_types::{ self, CreateFile, DiagnosticSeverity, DocumentChangeOperation, DocumentChanges, Documentation, diff --git a/crates/ra_lsp_server/src/diagnostics.rs b/crates/ra_lsp_server/src/diagnostics.rs index ea08bce24..e7924f0a3 100644 --- a/crates/ra_lsp_server/src/diagnostics.rs +++ b/crates/ra_lsp_server/src/diagnostics.rs @@ -1,7 +1,9 @@ //! Book keeping for keeping diagnostics easily in sync with the client. + +use std::{collections::HashMap, sync::Arc}; + use lsp_types::{CodeActionOrCommand, Diagnostic, Range}; use ra_ide::FileId; -use std::{collections::HashMap, sync::Arc}; pub type CheckFixes = Arc>>; diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 944074118..67d8a5f6f 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -1,5 +1,5 @@ -//! The main loop of `ra_lsp_server` responsible for dispatching LSP requests/replies and -//! notifications back to the client. +//! The main loop of `ra_lsp_server` responsible for dispatching LSP +//! requests/replies and notifications back to the client. mod handlers; mod subscriptions; diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 92f219e28..bb7bab372 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -1,5 +1,6 @@ -//! This module is responsible for implementing handlers for Lanuage Server Protocol. -//! The majority of requests are fulfilled by calling into the `ra_ide` crate. +//! This module is responsible for implementing handlers for Language Server +//! Protocol. The majority of requests are fulfilled by calling into the +//! `ra_ide` crate. use std::{ collections::hash_map::Entry, diff --git a/crates/ra_lsp_server/src/main_loop/pending_requests.rs b/crates/ra_lsp_server/src/main_loop/pending_requests.rs index 2d2213464..73b33e419 100644 --- a/crates/ra_lsp_server/src/main_loop/pending_requests.rs +++ b/crates/ra_lsp_server/src/main_loop/pending_requests.rs @@ -1,4 +1,4 @@ -//! Datastructures that keep track of inflight requests. +//! Data structures that keep track of inflight requests. use std::time::{Duration, Instant}; diff --git a/crates/ra_lsp_server/src/main_loop/subscriptions.rs b/crates/ra_lsp_server/src/main_loop/subscriptions.rs index b0bae90f5..bee6437cf 100644 --- a/crates/ra_lsp_server/src/main_loop/subscriptions.rs +++ b/crates/ra_lsp_server/src/main_loop/subscriptions.rs @@ -1,4 +1,5 @@ -//! Keeps track of file subscriptions. +//! Keeps track of file subscriptions -- the set of currently opened files for +//! which we want to publish diagnostics, syntax highlighting, etc. use ra_ide::FileId; use rustc_hash::FxHashSet; diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index 71c95d4af..96efab844 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -1,5 +1,5 @@ -//! The context or environment in which the language server functions. -//! In our server implementation this is know as the `WorldState`. +//! The context or environment in which the language server functions. In our +//! server implementation this is know as the `WorldState`. //! //! Each tick provides an immutable snapshot of the state as `WorldSnapshot`. -- cgit v1.2.3