From 04b5fcfdb28f18f4d5279b43c2bb2b3f9c082313 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 13 Apr 2021 11:27:00 +0300 Subject: Ensure that listing&resolving code actions use the same set of actions --- crates/ide/src/lib.rs | 28 ++++++++++++++++++++++++++ crates/rust-analyzer/src/handlers.rs | 38 ++++++++++++++---------------------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 0615b26d3..bbc0d5eec 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -531,6 +531,34 @@ impl Analysis { self.with_db(|db| diagnostics::diagnostics(db, config, file_id)) } + /// Convenience function to return assists + quick fixes for diagnostics + pub fn assists_with_fixes( + &self, + assist_config: &AssistConfig, + diagnostics_config: &DiagnosticsConfig, + resolve: bool, + frange: FileRange, + ) -> Cancelable> { + let include_fixes = match &assist_config.allowed { + Some(it) => it.iter().any(|&it| it == AssistKind::None || it == AssistKind::QuickFix), + None => true, + }; + + self.with_db(|db| { + let mut res = Assist::get(db, assist_config, resolve, frange); + ssr::add_ssr_assist(db, &mut res, resolve, frange); + + if include_fixes { + res.extend( + diagnostics::diagnostics(db, diagnostics_config, frange.file_id) + .into_iter() + .filter_map(|it| it.fix), + ); + } + res + }) + } + /// Returns the edit required to rename reference at the position to the new /// name. pub fn rename( diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 85c70373a..85cc8602e 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -8,8 +8,8 @@ use std::{ }; use ide::{ - AnnotationConfig, AssistKind, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, - Query, RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, TextEdit, + AnnotationConfig, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, Query, + RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, TextEdit, }; use ide_db::SymbolKind; use itertools::Itertools; @@ -1003,27 +1003,13 @@ pub(crate) fn handle_code_action( let mut res: Vec = Vec::new(); - let include_quick_fixes = match &assists_config.allowed { - Some(v) => v.iter().any(|it| it == &AssistKind::None || it == &AssistKind::QuickFix), - None => true, - }; let code_action_resolve_cap = snap.config.code_action_resolve(); - - let mut assists = Vec::new(); - - // Fixes from native diagnostics. - if include_quick_fixes { - let diagnostics = snap.analysis.diagnostics(&snap.config.diagnostics(), frange.file_id)?; - assists.extend( - diagnostics - .into_iter() - .filter_map(|d| d.fix) - .filter(|fix| fix.target.intersect(frange.range).is_some()), - ) - } - - // Assists proper. - assists.extend(snap.analysis.assists(&assists_config, !code_action_resolve_cap, frange)?); + let assists = snap.analysis.assists_with_fixes( + &assists_config, + &snap.config.diagnostics(), + !code_action_resolve_cap, + frange, + )?; for (index, assist) in assists.into_iter().enumerate() { let resolve_data = if code_action_resolve_cap { Some((index, params.clone())) } else { None }; @@ -1066,7 +1052,13 @@ pub(crate) fn handle_code_action_resolve( .only .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect()); - let assists = snap.analysis.assists(&assists_config, true, frange)?; + let assists = snap.analysis.assists_with_fixes( + &assists_config, + &snap.config.diagnostics(), + true, + frange, + )?; + let (id, index) = split_once(¶ms.id, ':').unwrap(); let index = index.parse::().unwrap(); let assist = &assists[index]; -- cgit v1.2.3