From b862576ad7a53188f934a11c46cfd5d3018c3a9e Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Mon, 29 Apr 2024 16:42:46 +0300 Subject: [PATCH] fix: displaying duplicate command suggestions Fixes https://github.com/sxyazi/yazi/issues/955 --- yazi-core/src/which/commands/show.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/yazi-core/src/which/commands/show.rs b/yazi-core/src/which/commands/show.rs index 9a5ae428d..eff69409c 100644 --- a/yazi-core/src/which/commands/show.rs +++ b/yazi-core/src/which/commands/show.rs @@ -1,6 +1,6 @@ -use std::str::FromStr; +use std::{collections::HashMap, str::FromStr}; -use yazi_config::{keymap::{Control, Key}, KEYMAP}; +use yazi_config::{keymap::{Control, ControlCow, Key}, KEYMAP}; use yazi_shared::{event::Cmd, render, Layer}; use crate::which::{Which, WhichSorter}; @@ -45,12 +45,7 @@ impl Which { pub fn show_with(&mut self, key: &Key, layer: Layer) { self.layer = layer; self.times = 1; - self.cands = KEYMAP - .get(layer) - .iter() - .filter(|c| c.on.len() > 1 && &c.on[0] == key) - .map(|c| c.into()) - .collect(); + self.cands = keymap_candidates(layer, key); WhichSorter::default().sort(&mut self.cands); self.visible = true; @@ -58,3 +53,16 @@ impl Which { render!(); } } + +fn keymap_candidates(layer: Layer, key: &Key) -> Vec { + let mut results: HashMap<&Vec, ControlCow> = HashMap::new(); + + let matches = KEYMAP.get(layer).iter().filter(|c| c.on.len() > 1 && &c.on[0] == key); + for c in matches { + if !results.contains_key(&c.on) { + results.insert(&c.on, c.into()); + } + } + + results.into_values().collect() +}