From a6af35b6ce8b2aa895f0365c917ec1acec7af6eb Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Thu, 29 Nov 2018 01:07:02 +0000 Subject: [PATCH 1/2] Fallback to racer definition * Runs racer find_definition if analysis goto has failed and `racer_completion` config is enabled. This brings the functionality in line with hover. * Removed `goto_def_racer_fallback`. * Ignore clippy large enum lint in `BuildPlan` --- README.md | 2 +- src/actions/requests.rs | 50 +++++++++++++---------------------------- src/build/plan.rs | 1 + src/config.rs | 7 +++--- 4 files changed, 21 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 208393db633..1f25ba9cd95 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ Currently we accept the following options: features * `racer_completion` (`bool`, defaults to `true`) enables code completion using racer (which is, at the moment, our only code completion backend). Also enables - hover tooltips to fall back to racer when save-analysis data is unavailable. + hover tooltips & go-to-definition to fall back to racer when save-analysis data is unavailable. * `clippy_preference` (`String`, defaults to `"opt-in"`) controls eagerness of clippy diagnostics when available. Valid values are _(case-insensitive)_: - `"off"` Disable clippy lints. diff --git a/src/actions/requests.rs b/src/actions/requests.rs index be5b4eb4e57..42da5303273 100644 --- a/src/actions/requests.rs +++ b/src/actions/requests.rs @@ -24,8 +24,6 @@ use url::Url; use crate::actions::hover; use crate::actions::run::collect_run_actions; -use crate::actions::work_pool; -use crate::actions::work_pool::WorkDescription; use crate::build::Edition; use crate::lsp_data; use crate::lsp_data::*; @@ -203,45 +201,27 @@ impl RequestAction for Definition { let span = ctx.convert_pos_to_span(file_path.clone(), params.position); let analysis = ctx.analysis.clone(); - // If configured start racer concurrently and fallback to racer result - let racer_receiver = { - if ctx.config.lock().unwrap().goto_def_racer_fallback { - Some(work_pool::receive_from_thread( - move || { - let cache = ctx.racer_cache(); - let session = ctx.racer_session(&cache); - let location = pos_to_racer_location(params.position); - - racer::find_definition(file_path, location, &session) - .and_then(|rm| location_from_racer_match(&rm)) - }, - WorkDescription("textDocument/definition-racer"), - )) - } else { - None - } - }; - match analysis.goto_def(&span) { Ok(out) => { let result = vec![ls_util::rls_to_location(&out)]; trace!("goto_def (compiler): {:?}", result); Ok(result) } - _ => match racer_receiver { - Some(receiver) => match receiver.recv() { - Ok(Some(r)) => { - trace!("goto_def (Racer): {:?}", r); - Ok(vec![r]) - } - Ok(None) => { - trace!("goto_def (Racer): None"); - Ok(vec![]) - } - _ => Self::fallback_response(), - }, - _ => Self::fallback_response(), - }, + // If analysis failed & `racer_completion` is enabled try racer + _ if ctx.config.lock().unwrap().racer_completion => { + let cache = ctx.racer_cache(); + let session = ctx.racer_session(&cache); + let location = pos_to_racer_location(params.position); + + let r = racer::find_definition(file_path, location, &session) + .and_then(|rm| location_from_racer_match(&rm)) + .map(|l| vec![l]) + .unwrap_or_default(); + + trace!("goto_def (Racer): {:?}", r); + Ok(r) + } + _ => Self::fallback_response(), } } } diff --git a/src/build/plan.rs b/src/build/plan.rs index 77c15270574..63079a61bf9 100644 --- a/src/build/plan.rs +++ b/src/build/plan.rs @@ -53,6 +53,7 @@ crate enum WorkStatus { Execute(JobQueue), } +#[allow(clippy::large_enum_variant)] #[derive(Debug)] crate enum BuildPlan { External(ExternalPlan), diff --git a/src/config.rs b/src/config.rs index d171addc4a1..731d059295b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -127,7 +127,6 @@ pub struct Config { pub unstable_features: bool, pub wait_to_build: Option, pub show_warnings: bool, - pub goto_def_racer_fallback: bool, /// Clear the RUST_LOG env variable before calling rustc/cargo? Default: true pub clear_env_rust_log: bool, /// Build the project only when a file got saved and not on file change. Default: false @@ -140,7 +139,10 @@ pub struct Config { pub no_default_features: bool, pub jobs: Option, pub all_targets: bool, - /// Enable use of racer for `textDocument/completion` requests + /// Enable use of racer for `textDocument/completion` requests. + /// + /// Enabled also enables racer fallbacks for hover & go-to-definition functionality + /// if rustc analysis should fail. pub racer_completion: bool, #[serde(deserialize_with = "deserialize_clippy_preference")] pub clippy_preference: ClippyPreference, @@ -177,7 +179,6 @@ impl Default for Config { unstable_features: false, wait_to_build: None, show_warnings: true, - goto_def_racer_fallback: false, clear_env_rust_log: true, build_on_save: false, use_crate_blacklist: true, From 2d16f791e2cba986df2e1378ec67db0ce913e169 Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Thu, 29 Nov 2018 15:08:32 +0000 Subject: [PATCH 2/2] Make config mutex borrow scope explicit --- src/actions/requests.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/actions/requests.rs b/src/actions/requests.rs index 42da5303273..6cf04314bab 100644 --- a/src/actions/requests.rs +++ b/src/actions/requests.rs @@ -201,14 +201,16 @@ impl RequestAction for Definition { let span = ctx.convert_pos_to_span(file_path.clone(), params.position); let analysis = ctx.analysis.clone(); - match analysis.goto_def(&span) { - Ok(out) => { - let result = vec![ls_util::rls_to_location(&out)]; - trace!("goto_def (compiler): {:?}", result); - Ok(result) - } - // If analysis failed & `racer_completion` is enabled try racer - _ if ctx.config.lock().unwrap().racer_completion => { + if let Ok(out) = analysis.goto_def(&span) { + let result = vec![ls_util::rls_to_location(&out)]; + trace!("goto_def (compiler): {:?}", result); + Ok(result) + } else { + let racer_enabled = { + let config = ctx.config.lock().unwrap(); + config.racer_completion + }; + if racer_enabled { let cache = ctx.racer_cache(); let session = ctx.racer_session(&cache); let location = pos_to_racer_location(params.position); @@ -220,8 +222,9 @@ impl RequestAction for Definition { trace!("goto_def (Racer): {:?}", r); Ok(r) + } else { + Self::fallback_response() } - _ => Self::fallback_response(), } } }