Skip to content

Commit b3665fc

Browse files
committed
Preserve relative ordering of grouped assists
1 parent 785eb32 commit b3665fc

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -734,19 +734,29 @@ pub fn handle_code_action(
734734
res.push(fix.action.clone());
735735
}
736736

737-
let mut grouped_assists: FxHashMap<String, Vec<Assist>> = FxHashMap::default();
737+
let mut grouped_assists: FxHashMap<String, (usize, Vec<Assist>)> = FxHashMap::default();
738738
for assist in world.analysis().assists(FileRange { file_id, range })?.into_iter() {
739739
match &assist.group_label {
740-
Some(label) => grouped_assists.entry(label.to_owned()).or_default().push(assist),
741-
None => res.push(create_single_code_action(assist, &world)?.into()),
740+
Some(label) => grouped_assists
741+
.entry(label.to_owned())
742+
.or_insert_with(|| {
743+
let idx = res.len();
744+
let dummy = Command::new(String::new(), String::new(), None);
745+
res.push(dummy.into());
746+
(idx, Vec::new())
747+
})
748+
.1
749+
.push(assist),
750+
None => {
751+
res.push(create_single_code_action(assist, &world)?.into());
752+
}
742753
}
743754
}
744755

745-
for (group_label, assists) in grouped_assists {
756+
for (group_label, (idx, assists)) in grouped_assists {
746757
if assists.len() == 1 {
747-
res.push(
748-
create_single_code_action(assists.into_iter().next().unwrap(), &world)?.into(),
749-
);
758+
res[idx] =
759+
create_single_code_action(assists.into_iter().next().unwrap(), &world)?.into();
750760
} else {
751761
let title = group_label;
752762

@@ -760,17 +770,15 @@ pub fn handle_code_action(
760770
command: "rust-analyzer.selectAndApplySourceChange".to_string(),
761771
arguments: Some(vec![serde_json::Value::Array(arguments)]),
762772
});
763-
res.push(
764-
CodeAction {
765-
title,
766-
kind: None,
767-
diagnostics: None,
768-
edit: None,
769-
command,
770-
is_preferred: None,
771-
}
772-
.into(),
773-
);
773+
res[idx] = CodeAction {
774+
title,
775+
kind: None,
776+
diagnostics: None,
777+
edit: None,
778+
command,
779+
is_preferred: None,
780+
}
781+
.into();
774782
}
775783
}
776784

0 commit comments

Comments
 (0)