Skip to content

Commit a18cbda

Browse files
committed
fix(keyboard): warn when user tries to rebind protected keys
Added validation to reject attempts to rebind non-rebindable keys (ctrl+c, ctrl+d, ctrl+m) in keybindings.json. Users now get a warning message listing the protected key and are told which keys cannot be rebound. Silently filters out these protected bindings instead of letting them override hardcoded behavior.
1 parent a42c3d6 commit a18cbda

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

src-rust/crates/core/src/keybindings.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use indexmap::IndexMap;
44
use serde::{Deserialize, Serialize};
55
use std::path::Path;
6+
use tracing::warn;
67

78
/// All keybinding contexts
89
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
@@ -324,9 +325,31 @@ pub struct UserBinding {
324325

325326
impl UserKeybindings {
326327
pub fn from_json_str(content: &str) -> Self {
327-
serde_json::from_str(content)
328+
let mut kb = serde_json::from_str(content)
328329
.or_else(|_| Self::from_block_config(content))
329-
.unwrap_or_default()
330+
.unwrap_or_default();
331+
332+
// Warn about and filter out non-rebindable keys
333+
let original_len = kb.bindings.len();
334+
kb.bindings.retain(|binding| {
335+
let normalized = binding.chord.to_lowercase();
336+
if NON_REBINDABLE.iter().any(|protected| normalized == *protected) {
337+
warn!("Cannot rebind protected key '{}' in keybindings.json", binding.chord);
338+
return false;
339+
}
340+
true
341+
});
342+
343+
if kb.bindings.len() < original_len {
344+
let filtered_count = original_len - kb.bindings.len();
345+
warn!(
346+
"Filtered out {} protected keybinding(s). Protected keys: {}",
347+
filtered_count,
348+
NON_REBINDABLE.join(", ")
349+
);
350+
}
351+
352+
kb
330353
}
331354

332355
pub fn load(config_dir: &Path) -> Self {

0 commit comments

Comments
 (0)