Preserve relative ordering of grouped assists

This commit is contained in:
Aleksey Kladov
2020-03-25 15:45:52 +01:00
parent 785eb32f49
commit b3665fccfb

View File

@@ -734,19 +734,29 @@ pub fn handle_code_action(
res.push(fix.action.clone()); res.push(fix.action.clone());
} }
let mut grouped_assists: FxHashMap<String, Vec<Assist>> = FxHashMap::default(); let mut grouped_assists: FxHashMap<String, (usize, Vec<Assist>)> = FxHashMap::default();
for assist in world.analysis().assists(FileRange { file_id, range })?.into_iter() { for assist in world.analysis().assists(FileRange { file_id, range })?.into_iter() {
match &assist.group_label { match &assist.group_label {
Some(label) => grouped_assists.entry(label.to_owned()).or_default().push(assist), Some(label) => grouped_assists
None => res.push(create_single_code_action(assist, &world)?.into()), .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 { if assists.len() == 1 {
res.push( res[idx] =
create_single_code_action(assists.into_iter().next().unwrap(), &world)?.into(), create_single_code_action(assists.into_iter().next().unwrap(), &world)?.into();
);
} else { } else {
let title = group_label; let title = group_label;
@@ -760,8 +770,7 @@ pub fn handle_code_action(
command: "rust-analyzer.selectAndApplySourceChange".to_string(), command: "rust-analyzer.selectAndApplySourceChange".to_string(),
arguments: Some(vec![serde_json::Value::Array(arguments)]), arguments: Some(vec![serde_json::Value::Array(arguments)]),
}); });
res.push( res[idx] = CodeAction {
CodeAction {
title, title,
kind: None, kind: None,
diagnostics: None, diagnostics: None,
@@ -769,8 +778,7 @@ pub fn handle_code_action(
command, command,
is_preferred: None, is_preferred: None,
} }
.into(), .into();
);
} }
} }