Skip to content

armgit / herdr

Public Imported from GitHub
0

the runtime your coding agents live on

fix: preserve literal plus keybindings (#4983)

Eric Yue4f8961180c54
diff --git a/src/client/shell/tests/keybindings_settings.rs b/src/client/shell/tests/keybindings_settings.rs
index a00e79c0a..fd3301db2 100644
--- a/src/client/shell/tests/keybindings_settings.rs
+++ b/src/client/shell/tests/keybindings_settings.rs
@@ -1,4 +1,66 @@
use super::*;
+use crate::input::TerminalKey;
+
+#[test]
+fn plus_commands_survive_snapshots_and_reload_for_both_keybinding_sources() {
+ for source in [
+ ClientShellKeybindingSource::Local,
+ ClientShellKeybindingSource::Endpoint,
+ ] {
+ let mut state = ClientShellState::new(
+ ClientShellConfig::from_config(&Config::default()).with_keybinding_source(source),
+ );
+ for (revision, key, modifiers) in [
+ (1, "prefix+plus", KeyModifiers::empty()),
+ (2, "prefix+ctrl+plus", KeyModifiers::CONTROL),
+ ] {
+ let config: Config = toml::from_str(&format!(
+ "[[keys.command]]\nkey = {key:?}\ntype = \"shell\"\ncommand = \"echo plus\"\n"
+ ))
+ .unwrap();
+ assert!(config.collect_diagnostics().is_empty());
+ let (_api_tx, api_rx) = tokio::sync::mpsc::unbounded_channel();
+ let app = crate::app::App::new(
+ &config,
+ crate::app::AppPolicy::TEST,
+ None,
+ api_rx,
+ crate::api::EventHub::default(),
+ );
+ let mut projection = snapshot();
+ projection.revision = revision;
+ projection.server_keybindings_toml = config.local_keybindings_profile_toml().ok();
+ projection.commands = app.client_shell_command_manifest();
+ assert_eq!(projection.commands.len(), 1);
+ let command_id = projection.commands[0].command_id.clone();
+ state.set_snapshot(Box::new(projection));
+ state.set_pane_surface(surface());
+ assert_eq!(state.config.keybinds.keybinds.custom_commands.len(), 1);
+
+ for plus in [
+ TerminalKey::new(KeyCode::Char('+'), modifiers),
+ TerminalKey::new(KeyCode::Char('+'), modifiers | KeyModifiers::SHIFT),
+ TerminalKey::new(KeyCode::Char('='), modifiers | KeyModifiers::SHIFT)
+ .with_shifted_codepoint('+' as u32),
+ ] {
+ let outcome = state.handle_raw_events(vec![
+ RawInputEvent::Key(TerminalKey::new(KeyCode::Char('b'), KeyModifiers::CONTROL)),
+ RawInputEvent::Key(plus),
+ ]);
+ let [ClientShellAction::Endpoint { request, .. }] = &outcome.actions[..] else {
+ panic!(
+ "expected endpoint command invocation: {:?}",
+ outcome.actions
+ );
+ };
+ let crate::api::schema::Method::CommandInvoke(params) = &request.method else {
+ panic!("expected command.invoke");
+ };
+ assert_eq!(params.command_id, command_id);
+ }
+ }
+ }
+}
#[test]
fn shell_new_controls_use_the_same_client_action_routes_as_keybinds() {
diff --git a/src/config.rs b/src/config.rs
index 8550097d7..925423d95 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -200,6 +200,21 @@ pub(crate) fn keybindings_from_profile_toml(profile: &str) -> Result<LiveKeybind
mod tests {
use super::*;
+ #[test]
+ fn local_keybindings_profile_preserves_plus_prefixes() {
+ let config: Config = toml::from_str(
+ "[keys]\nprefix = [\"ctrl+plus\", \"alt+plus\"]\nnew_tab = \"prefix+plus\"\n",
+ )
+ .unwrap();
+ let profile = config.local_keybindings_profile_toml().unwrap();
+ let restored = keybindings_from_profile_toml(&profile).unwrap();
+ assert_eq!(restored.prefix, config.prefix_keys());
+ assert_eq!(
+ restored.keybinds.new_tab.bindings,
+ config.keybinds().new_tab.bindings
+ );
+ }
+
#[test]
fn local_keybindings_profile_includes_defaults_and_excludes_commands() {
let config: Config = toml::from_str(
diff --git a/src/config/keybinds.rs b/src/config/keybinds.rs
index 903f85ca3..9d2792a08 100644
--- a/src/config/keybinds.rs
+++ b/src/config/keybinds.rs
@@ -1279,6 +1279,15 @@ fn parse_modifier_combo(s: &str) -> Option<KeyModifiers> {
}
pub(crate) fn parse_key_combo(s: &str) -> Option<KeyCombo> {
+ let s = s.trim();
+ // Labels are also used in endpoint profiles and command manifests. A literal
+ // plus is the key after the final separator, not an empty key token.
+ if s == "+" {
+ return Some((KeyCode::Char('+'), KeyModifiers::empty()));
+ }
+ if let Some(modifiers) = s.strip_suffix("++") {
+ return Some((KeyCode::Char('+'), parse_modifier_combo(modifiers)?));
+ }
let parts: Vec<&str> = s.split('+').collect();
let mut modifiers = KeyModifiers::empty();
let mut key_str: Option<&str> = None;
@@ -1849,6 +1858,73 @@ close_tab = "X"
);
}
+ #[test]
+ fn supported_key_labels_round_trip_through_the_combo_parser() {
+ let keys = [
+ "a",
+ "A",
+ "ğ",
+ "Ğ",
+ "space",
+ "enter",
+ "escape",
+ "tab",
+ "backspace",
+ "left",
+ "right",
+ "up",
+ "down",
+ "f1",
+ "f12",
+ "minus",
+ "comma",
+ "period",
+ "slash",
+ "backslash",
+ "quote",
+ "double_quote",
+ "semicolon",
+ "colon",
+ "percent",
+ "ampersand",
+ "backtick",
+ "plus",
+ "(",
+ ")",
+ "{",
+ "}",
+ "?",
+ ];
+ let modifiers = ["ctrl", "alt", "shift", "super", "hyper"];
+ for mask in 0..(1 << modifiers.len()) {
+ for key in keys {
+ let mut parts: Vec<_> = modifiers
+ .iter()
+ .enumerate()
+ .filter_map(|(index, modifier)| (mask & (1 << index) != 0).then_some(*modifier))
+ .collect();
+ parts.push(key);
+ let input = parts.join("+");
+ let combo = parse_key_combo(&input).expect("supported key combo");
+ let label = format_key_combo(combo);
+ assert_eq!(parse_key_combo(&label), Some(combo), "{input} -> {label}");
+ }
+ }
+ }
+
+ #[test]
+ fn literal_plus_does_not_accept_missing_or_extra_key_tokens() {
+ for invalid in ["", "ctrl+", "++", "ctrl+++", "ctrl++x", "x++", "unknown++"] {
+ assert_eq!(parse_key_combo(invalid), None, "{invalid}");
+ }
+ assert!(parse_binding_string("prefix+++").is_none());
+ assert_eq!(parse_key_combo(" + "), parse_key_combo("plus"));
+ assert_eq!(
+ parse_key_combo("ctrl + alt ++"),
+ parse_key_combo("ctrl+alt+plus")
+ );
+ }
+
#[test]
fn shifted_punctuation_matches_enhanced_input() {
let help = ActionKeybinds::prefix("?");