From 797cd34c7c996251816226dac369fece1587a416 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Apr 2020 17:00:37 +0200 Subject: Reduce feature flags --- crates/rust-analyzer/src/config.rs | 37 +++++++++++++++++++++++++- crates/rust-analyzer/src/main_loop.rs | 33 +++++------------------ crates/rust-analyzer/src/main_loop/handlers.rs | 16 +++-------- crates/rust-analyzer/src/world.rs | 14 +--------- 4 files changed, 48 insertions(+), 52 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index fb7895ce0..94627d35d 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -9,24 +9,35 @@ use rustc_hash::FxHashMap; +use crate::feature_flags::FeatureFlags; use lsp_types::TextDocumentClientCapabilities; use ra_flycheck::FlycheckConfig; -use ra_ide::InlayHintsConfig; +use ra_ide::{CompletionConfig, InlayHintsConfig}; use ra_project_model::CargoFeatures; use serde::{Deserialize, Deserializer}; #[derive(Debug, Clone)] pub struct Config { pub publish_decorations: bool, + pub publish_diagnostics: bool, + pub notifications: NotificationsConfig, pub supports_location_link: bool, pub line_folding_only: bool, pub inlay_hints: InlayHintsConfig, + pub completion: CompletionConfig, + pub call_info_full: bool, pub rustfmt: RustfmtConfig, pub check: Option, pub vscode_lldb: bool, pub proc_macro_srv: Option, } +#[derive(Debug, Clone)] +pub struct NotificationsConfig { + pub workspace_loaded: bool, + pub cargo_toml_not_found: bool, +} + #[derive(Debug, Clone)] pub enum RustfmtConfig { Rustfmt { @@ -49,8 +60,14 @@ pub(crate) fn get_config( config: &ServerConfig, text_document_caps: Option<&TextDocumentClientCapabilities>, ) -> Config { + let feature_flags = get_feature_flags(config); Config { publish_decorations: config.publish_decorations, + publish_diagnostics: feature_flags.get("lsp.diagnostics"), + notifications: NotificationsConfig { + workspace_loaded: feature_flags.get("notifications.workspace-loaded"), + cargo_toml_not_found: feature_flags.get("notifications.cargo-toml-not-found"), + }, supports_location_link: text_document_caps .and_then(|it| it.definition) .and_then(|it| it.link_support) @@ -65,6 +82,13 @@ pub(crate) fn get_config( chaining_hints: config.inlay_hints_chaining, max_length: config.inlay_hints_max_length, }, + completion: CompletionConfig { + enable_postfix_completions: feature_flags.get("completion.enable-postfix"), + add_call_parenthesis: feature_flags.get("completion.insertion.add-call-parenthesis"), + add_call_argument_snippets: feature_flags + .get("completion.insertion.add-argument-snippets"), + }, + call_info_full: feature_flags.get("call-info.full"), check: if config.cargo_watch_enable { Some(FlycheckConfig::CargoCommand { command: config.cargo_watch_command.clone(), @@ -80,6 +104,17 @@ pub(crate) fn get_config( } } +fn get_feature_flags(config: &ServerConfig) -> FeatureFlags { + let mut ff = FeatureFlags::default(); + for (flag, &value) in &config.feature_flags { + if ff.set(flag.as_str(), value).is_err() { + log::error!("unknown feature flag: {:?}", flag); + } + } + log::info!("feature_flags: {:#?}", ff); + ff +} + /// Client provided initialization options #[derive(Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "camelCase", default)] diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index de40e2ac2..8f89fb7ea 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -33,7 +33,6 @@ use threadpool::ThreadPool; use crate::{ config::get_config, diagnostics::DiagnosticTask, - feature_flags::FeatureFlags, main_loop::{ pending_requests::{PendingRequest, PendingRequests}, subscriptions::Subscriptions, @@ -66,22 +65,6 @@ impl fmt::Display for LspError { impl Error for LspError {} -fn get_feature_flags(config: &ServerConfig, connection: &Connection) -> FeatureFlags { - let mut ff = FeatureFlags::default(); - for (flag, &value) in &config.feature_flags { - if ff.set(flag.as_str(), value).is_err() { - log::error!("unknown feature flag: {:?}", flag); - show_message( - req::MessageType::Error, - format!("unknown feature flag: {:?}", flag), - &connection.sender, - ); - } - } - log::info!("feature_flags: {:#?}", ff); - ff -} - pub fn main_loop( ws_roots: Vec, client_caps: ClientCapabilities, @@ -112,8 +95,8 @@ pub fn main_loop( let text_document_caps = client_caps.text_document.as_ref(); let mut loop_state = LoopState::default(); let mut world_state = { - let feature_flags = get_feature_flags(&config, &connection); - + // TODO: refactor + let new_config = get_config(&config, text_document_caps); // FIXME: support dynamic workspace loading. let workspaces = { let mut loaded_workspaces = Vec::new(); @@ -131,7 +114,7 @@ pub fn main_loop( if let Some(ra_project_model::CargoTomlNotFoundError { .. }) = e.downcast_ref() { - if !feature_flags.get("notifications.cargo-toml-not-found") { + if !new_config.notifications.cargo_toml_not_found { continue; } } @@ -180,8 +163,7 @@ pub fn main_loop( config.lru_capacity, &globs, Watch(!config.use_client_watching), - get_config(&config, text_document_caps), - feature_flags, + new_config, ) }; @@ -406,7 +388,6 @@ fn loop_turn( world_state.update_configuration( new_config.lru_capacity, get_config(&new_config, text_document_caps), - get_feature_flags(&new_config, connection), ); } (None, Some(Err(e))) => { @@ -441,8 +422,8 @@ fn loop_turn( }); } - let show_progress = !loop_state.workspace_loaded - && world_state.feature_flags.get("notifications.workspace-loaded"); + let show_progress = + !loop_state.workspace_loaded && world_state.config.notifications.workspace_loaded; if !loop_state.workspace_loaded && loop_state.roots_scanned == loop_state.roots_total @@ -930,7 +911,7 @@ fn update_file_notifications_on_threadpool( subscriptions: Vec, ) { log::trace!("updating notifications for {:?}", subscriptions); - let publish_diagnostics = world.feature_flags.get("lsp.diagnostics"); + let publish_diagnostics = world.config.publish_diagnostics; pool.execute(move || { for file_id in subscriptions { if publish_diagnostics { diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index bb99b38a8..d0f64f007 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -19,8 +19,8 @@ use lsp_types::{ TextEdit, WorkspaceEdit, }; use ra_ide::{ - Assist, AssistId, CompletionConfig, FileId, FilePosition, FileRange, Query, RangeInfo, - Runnable, RunnableKind, SearchScope, + Assist, AssistId, FileId, FilePosition, FileRange, Query, RangeInfo, Runnable, RunnableKind, + SearchScope, }; use ra_prof::profile; use ra_syntax::{AstNode, SyntaxKind, TextRange, TextUnit}; @@ -426,15 +426,7 @@ pub fn handle_completion( return Ok(None); } - let config = CompletionConfig { - enable_postfix_completions: world.feature_flags.get("completion.enable-postfix"), - add_call_parenthesis: world.feature_flags.get("completion.insertion.add-call-parenthesis"), - add_call_argument_snippets: world - .feature_flags - .get("completion.insertion.add-argument-snippets"), - }; - - let items = match world.analysis().completions(position, &config)? { + let items = match world.analysis().completions(position, &world.config.completion)? { None => return Ok(None), Some(items) => items, }; @@ -471,7 +463,7 @@ pub fn handle_signature_help( let _p = profile("handle_signature_help"); let position = params.try_conv_with(&world)?; if let Some(call_info) = world.analysis().call_info(position)? { - let concise = !world.feature_flags.get("call-info.full"); + let concise = !world.config.call_info_full; let mut active_parameter = call_info.active_parameter.map(|it| it as i64); if concise && call_info.signature.has_self_param { active_parameter = active_parameter.map(|it| it.saturating_sub(1)); diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs index 124de4d8e..df1b7ceeb 100644 --- a/crates/rust-analyzer/src/world.rs +++ b/crates/rust-analyzer/src/world.rs @@ -23,7 +23,6 @@ use stdx::format_to; use crate::{ config::Config, diagnostics::{CheckFixes, DiagnosticCollection}, - feature_flags::FeatureFlags, main_loop::pending_requests::{CompletedRequest, LatestRequests}, vfs_glob::{Glob, RustPackageFilterBuilder}, LspError, Result, @@ -59,7 +58,6 @@ fn create_flycheck(workspaces: &[ProjectWorkspace], config: &Config) -> Option, pub roots: Vec, pub workspaces: Arc>, pub analysis_host: AnalysisHost, @@ -73,7 +71,6 @@ pub struct WorldState { /// An immutable snapshot of the world's state at a point in time. pub struct WorldSnapshot { pub config: Config, - pub feature_flags: Arc, pub workspaces: Arc>, pub analysis: Analysis, pub latest_requests: Arc>, @@ -89,7 +86,6 @@ impl WorldState { exclude_globs: &[Glob], watch: Watch, config: Config, - feature_flags: FeatureFlags, ) -> WorldState { let mut change = AnalysisChange::new(); @@ -197,7 +193,6 @@ impl WorldState { analysis_host.apply_change(change); WorldState { config: config, - feature_flags: Arc::new(feature_flags), roots: folder_roots, workspaces: Arc::new(workspaces), analysis_host, @@ -209,13 +204,7 @@ impl WorldState { } } - pub fn update_configuration( - &mut self, - lru_capacity: Option, - config: Config, - feature_flags: FeatureFlags, - ) { - self.feature_flags = Arc::new(feature_flags); + pub fn update_configuration(&mut self, lru_capacity: Option, config: Config) { self.analysis_host.update_lru_capacity(lru_capacity); self.flycheck = create_flycheck(&self.workspaces, &config); self.config = config; @@ -275,7 +264,6 @@ impl WorldState { pub fn snapshot(&self) -> WorldSnapshot { WorldSnapshot { config: self.config.clone(), - feature_flags: Arc::clone(&self.feature_flags), workspaces: Arc::clone(&self.workspaces), analysis: self.analysis_host.analysis(), vfs: Arc::clone(&self.vfs), -- cgit v1.2.3