From 3ced546033973a63cb2ef1644ca099740fdfb1c2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 23 Dec 2020 13:16:24 +0300 Subject: Make code more understandable Avoid mutation of snapshot's config -- that's spooky action at a distance. Instead, copy it over to a local variable. This points out a minor architecture problem, which we won't fix right away. Various `ide`-level config structs, like `AssistConfig`, are geared towards one-shot use when calling a specific methods. On the other hand, the large `Config` struct in `rust-analyzer` is a long-term config store. The fact that `Config` stores `AssistConfig` is accidental -- a better design would probably be to just store `ConfigData` inside `Config` and create various `Config`s on the fly out of it. --- crates/rust-analyzer/src/handlers.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index a51a9293f..83b3a343c 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -8,8 +8,9 @@ use std::{ }; use ide::{ - CompletionResolveCapability, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, - NavigationTarget, Query, RangeInfo, Runnable, RunnableKind, SearchScope, SymbolKind, TextEdit, + AssistConfig, CompletionResolveCapability, FileId, FilePosition, FileRange, HoverAction, + HoverGotoTypeData, NavigationTarget, Query, RangeInfo, Runnable, RunnableKind, SearchScope, + SymbolKind, TextEdit, }; use itertools::Itertools; use lsp_server::ErrorCode; @@ -882,11 +883,14 @@ pub(crate) fn handle_code_action( let range = from_proto::text_range(&line_index, params.range); let frange = FileRange { file_id, range }; - snap.config.assist.allowed = params - .clone() - .context - .only - .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect()); + let assists_config = AssistConfig { + allowed: params + .clone() + .context + .only + .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect()), + ..snap.config.assist + }; let mut res: Vec = Vec::new(); @@ -894,12 +898,12 @@ pub(crate) fn handle_code_action( if snap.config.client_caps.code_action_resolve { for (index, assist) in - snap.analysis.unresolved_assists(&snap.config.assist, frange)?.into_iter().enumerate() + snap.analysis.unresolved_assists(&assists_config, frange)?.into_iter().enumerate() { res.push(to_proto::unresolved_code_action(&snap, params.clone(), assist, index)?); } } else { - for assist in snap.analysis.resolved_assists(&snap.config.assist, frange)?.into_iter() { + for assist in snap.analysis.resolved_assists(&assists_config, frange)?.into_iter() { res.push(to_proto::resolved_code_action(&snap, assist)?); } } -- cgit v1.2.3