Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Make getting keybinding for display more efficient (#27046)

Michael Sloan3ec69a5bc0ed
diff --git a/crates/gpui/src/keymap.rs b/crates/gpui/src/keymap.rs
index 27ca70e3282..49bdb54cfb6 100644
--- a/crates/gpui/src/keymap.rs
+++ b/crates/gpui/src/keymap.rs
@@ -202,10 +202,16 @@ impl Keymap {
/// couple times. The decision as of now is to pick a side and leave it
/// as is, until we have a better way to decide which binding to display
/// that is consistent and not confusing.
- pub fn binding_to_display_from_bindings<'a>(
- bindings: impl IntoIterator<Item = &'a KeyBinding>,
+ pub fn binding_to_display_from_bindings(mut bindings: Vec<KeyBinding>) -> Option<KeyBinding> {
+ bindings.pop()
+ }
+
+ /// Like `bindings_to_display_from_bindings` but takes a `DoubleEndedIterator` and returns a
+ /// reference.
+ pub fn binding_to_display_from_bindings_iterator<'a>(
+ mut bindings: impl DoubleEndedIterator<Item = &'a KeyBinding>,
) -> Option<&'a KeyBinding> {
- return bindings.into_iter().last();
+ bindings.next_back()
}
}
diff --git a/crates/gpui/src/platform/mac/platform.rs b/crates/gpui/src/platform/mac/platform.rs
index edc8fdfea29..4fe8383651c 100644
--- a/crates/gpui/src/platform/mac/platform.rs
+++ b/crates/gpui/src/platform/mac/platform.rs
@@ -290,7 +290,7 @@ impl MacPlatform {
action,
os_action,
} => {
- let keystrokes = crate::Keymap::binding_to_display_from_bindings(
+ let keystrokes = crate::Keymap::binding_to_display_from_bindings_iterator(
keymap.bindings_for_action(action.as_ref()),
)
.map(|binding| binding.keystrokes());
diff --git a/crates/ui/src/components/keybinding.rs b/crates/ui/src/components/keybinding.rs
index 5d1fda8e11e..63b63da45ff 100644
--- a/crates/ui/src/components/keybinding.rs
+++ b/crates/ui/src/components/keybinding.rs
@@ -33,8 +33,7 @@ impl KeyBinding {
return Self::for_action_in(action, &focused, window, cx);
}
let key_binding =
- gpui::Keymap::binding_to_display_from_bindings(&window.bindings_for_action(action))
- .cloned()?;
+ gpui::Keymap::binding_to_display_from_bindings(window.bindings_for_action(action))?;
Some(Self::new(key_binding, cx))
}
@@ -46,9 +45,8 @@ impl KeyBinding {
cx: &App,
) -> Option<Self> {
let key_binding = gpui::Keymap::binding_to_display_from_bindings(
- &window.bindings_for_action_in(action, focus),
- )
- .cloned()?;
+ window.bindings_for_action_in(action, focus),
+ )?;
Some(Self::new(key_binding, cx))
}