From b3665fccfb0a81752c35e56f6c41043133a949dd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 25 Mar 2020 15:45:52 +0100 Subject: Preserve relative ordering of grouped assists --- crates/rust-analyzer/src/main_loop/handlers.rs | 44 +++++++++++++++----------- 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 1cc2f6571..1033d6de9 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -734,19 +734,29 @@ pub fn handle_code_action( res.push(fix.action.clone()); } - let mut grouped_assists: FxHashMap> = FxHashMap::default(); + let mut grouped_assists: FxHashMap)> = FxHashMap::default(); for assist in world.analysis().assists(FileRange { file_id, range })?.into_iter() { match &assist.group_label { - Some(label) => grouped_assists.entry(label.to_owned()).or_default().push(assist), - None => res.push(create_single_code_action(assist, &world)?.into()), + Some(label) => grouped_assists + .entry(label.to_owned()) + .or_insert_with(|| { + let idx = res.len(); + let dummy = Command::new(String::new(), String::new(), None); + res.push(dummy.into()); + (idx, Vec::new()) + }) + .1 + .push(assist), + None => { + res.push(create_single_code_action(assist, &world)?.into()); + } } } - for (group_label, assists) in grouped_assists { + for (group_label, (idx, assists)) in grouped_assists { if assists.len() == 1 { - res.push( - create_single_code_action(assists.into_iter().next().unwrap(), &world)?.into(), - ); + res[idx] = + create_single_code_action(assists.into_iter().next().unwrap(), &world)?.into(); } else { let title = group_label; @@ -760,17 +770,15 @@ pub fn handle_code_action( command: "rust-analyzer.selectAndApplySourceChange".to_string(), arguments: Some(vec![serde_json::Value::Array(arguments)]), }); - res.push( - CodeAction { - title, - kind: None, - diagnostics: None, - edit: None, - command, - is_preferred: None, - } - .into(), - ); + res[idx] = CodeAction { + title, + kind: None, + diagnostics: None, + edit: None, + command, + is_preferred: None, + } + .into(); } } -- cgit v1.2.3