use std::collections::BTreeMap;
use std::path::Path;
use std::sync::Mutex;
use gpui_kit::{App, SharedString};
use toml_edit::DocumentMut;
use crate::command::Registry;
pub type Keys = Vec<(String, Vec<String>)>;
#[derive(Default)]
struct Keymap {
file: BTreeMap<String, BTreeMap<String, Vec<String>>>,
defaults: Vec<(String, Keys)>,
notices: Vec<String>,
}
static KEYMAP: Mutex<Option<Keymap>> = Mutex::new(None);
fn with_keymap<R>(f: impl FnOnce(&mut Keymap) -> R) -> R {
let mut guard = KEYMAP.lock().unwrap_or_else(|e| e.into_inner());
f(guard.get_or_insert_with(Keymap::default))
}
pub fn load_keybindings(path: &Path) {
let Ok(text) = std::fs::read_to_string(path) else { return };
let (file, notices) = parse_keybindings(&text);
with_keymap(|k| {
k.file = file;
k.notices.extend(notices);
});
}
fn parse_keybindings(text: &str) -> (BTreeMap<String, BTreeMap<String, Vec<String>>>, Vec<String>) {
let mut notices = Vec::new();
let mut file = BTreeMap::new();
let doc: DocumentMut = match text.parse() {
Ok(d) => d,
Err(e) => return (file, vec![format!("keybindings.toml: {e}")]),
};
for (section, item) in doc.iter() {
let mut out = BTreeMap::new();
match item.as_table_like() {
Some(table) => flatten(section, "", table, &mut out, &mut notices),
None => notices.push(format!("keybindings.toml: {section} must be a section ([{section}])")),
}
file.insert(section.to_string(), out);
}
(file, notices)
}
fn flatten(
section: &str,
prefix: &str,
table: &dyn toml_edit::TableLike,
out: &mut BTreeMap<String, Vec<String>>,
notices: &mut Vec<String>,
) {
for (key, item) in table.iter() {
let name = if prefix.is_empty() { key.to_string() } else { format!("{prefix}.{key}") };
if let Some(text) = item.as_str() {
out.insert(name, if text.is_empty() { Vec::new() } else { vec![text.to_string()] });
} else if let Some(list) = item.as_array() {
let keys: Vec<String> =
list.iter().filter_map(|v| v.as_str()).filter(|t| !t.is_empty()).map(str::to_string).collect();
if keys.len() != list.iter().filter(|v| v.as_str() != Some("")).count() {
notices.push(format!("keybindings.toml: [{section}] {name}: every key must be text in quotes"));
} else {
out.insert(name, keys);
}
} else if let Some(inner) = item.as_table_like() {
flatten(section, &name, inner, out, notices);
} else {
notices.push(format!("keybindings.toml: [{section}] {name} must be a key in quotes, or a list of keys"));
}
}
}
pub fn resolve(section: &str, defaults: &[(&str, &[&str])], valid: impl Fn(&str) -> bool) -> Keys {
with_keymap(|k| {
let defaults: Keys = defaults
.iter()
.map(|(name, keys)| (name.to_string(), keys.iter().map(|s| s.to_string()).collect()))
.collect();
let empty = BTreeMap::new();
let file = k.file.get(section).unwrap_or(&empty);
for name in file.keys() {
if !defaults.iter().any(|(n, _)| n == name) {
k.notices.push(format!("keybindings.toml: [{section}] has no command {name:?}"));
}
}
let mut out = Keys::new();
for (name, default) in &defaults {
let keys = match file.get(name) {
Some(keys) => match keys.iter().find(|key| !valid(key)) {
Some(bad) => {
k.notices.push(format!(
"keybindings.toml: [{section}] {name}: {bad:?} is not a key; the default is kept"
));
default.clone()
}
None => keys.clone(),
},
None => default.clone(),
};
out.push((name.clone(), keys));
}
let mut seen: BTreeMap<&str, &str> = BTreeMap::new();
for (name, keys) in &out {
for key in keys {
if let Some(other) = seen.insert(key, name)
&& other != name
{
k.notices
.push(format!("keybindings.toml: [{section}] {key:?} is bound to both {other} and {name}"));
}
}
}
k.defaults.retain(|(s, _)| s != section);
k.defaults.push((section.to_string(), defaults));
out
})
}
pub fn write_default_keybindings(path: &Path, app: &str) -> std::io::Result<()> {
if path.exists() {
return Ok(());
}
let text = with_keymap(|k| default_keybindings(app, &k.defaults));
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir)?;
}
std::fs::write(path, text)
}
fn default_keybindings(app: &str, sections: &[(String, Keys)]) -> String {
let mut text = format!(
"# {app} key bindings: command = \"key\", or a list of keys; \"\" removes a binding.\n# Keys are written as ctrl-shift-o, alt-g, space, left, f3, +. Read when the app starts.\n# Delete a line to go back to its default.\n",
);
for (section, keys) in sections {
text.push_str(&format!("\n[{section}]\n"));
let bare = |name: &str| {
name.split('.')
.all(|part| !part.is_empty() && part.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-'))
&& !keys.iter().any(|(other, _)| other.starts_with(&format!("{name}.")))
};
for (name, bound) in keys {
let name = if bare(name) { name.clone() } else { format!("{name:?}") };
let value = match bound.as_slice() {
[one] => format!("{one:?}"),
many => format!("[{}]", many.iter().map(|k| format!("{k:?}")).collect::<Vec<_>>().join(", ")),
};
text.push_str(&format!("{name} = {value}\n"));
}
}
text
}
pub fn take_notices() -> Vec<String> {
with_keymap(|k| std::mem::take(&mut k.notices))
}
pub fn parse_key(text: &str) -> Option<(bool, bool, bool, String)> {
let (mut cmd, mut alt, mut shift) = (false, false, false);
let mut rest = text.trim();
loop {
let Some((head, tail)) = rest.split_once('-').filter(|(_, tail)| !tail.is_empty()) else { break };
match head {
"ctrl" | "cmd" | "secondary" | "super" => cmd = true,
"alt" => alt = true,
"shift" => shift = true,
_ => return None,
}
rest = tail;
}
(!rest.is_empty() && !rest.contains(' ')).then(|| (cmd, alt, shift, rest.to_lowercase()))
}
pub fn bind_commands(registry: &Registry, cx: &mut App) -> Vec<String> {
let commands = registry.all();
let defaults: Vec<(String, Vec<String>)> =
commands.iter().map(|c| (c.id.to_string(), c.key.iter().map(|k| k.to_string()).collect())).collect();
let borrowed: Vec<(&str, Vec<&str>)> =
defaults.iter().map(|(id, keys)| (id.as_str(), keys.iter().map(String::as_str).collect())).collect();
let slices: Vec<(&str, &[&str])> = borrowed.iter().map(|(id, keys)| (*id, keys.as_slice())).collect();
let probe =
|key: &str| gpui_kit::KeyBinding::load(key, Box::new(gpui_kit::NoAction), None, false, None, cx).is_ok();
for ((id, keys), mut command) in resolve("commands", &slices, probe).into_iter().zip(commands) {
let key = keys.first().map(|k| SharedString::from(k.clone()));
if command.id.as_ref() == id && command.key != key {
command.key = key;
registry.add(command);
}
}
registry.bind_keys(None, cx)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn key_bindings_override_defaults_by_command() {
let (file, notices) = parse_keybindings(
"[plot]\nplay_pause = \"p\"\nzoom_in = [\"+\", \"=\"]\npan_back = \"\"\n[commands]\nfile.open = \"ctrl-shift-o\"\n\"view.dark\" = \"f9\"\nbad = 3\n",
);
assert_eq!(notices.len(), 1, "{notices:?}");
assert_eq!(file["plot"]["play_pause"], vec!["p"]);
assert_eq!(file["plot"]["zoom_in"], vec!["+", "="]);
assert!(file["plot"]["pan_back"].is_empty(), "an empty key removes the binding");
assert_eq!(file["commands"]["file.open"], vec!["ctrl-shift-o"], "a dotted key is the command's id");
assert_eq!(file["commands"]["view.dark"], vec!["f9"]);
}
#[test]
fn the_default_file_reads_back_as_the_defaults() {
let sections = vec![(
"commands".to_string(),
vec![
("file.open".to_string(), vec!["ctrl-o".to_string()]),
("a.b".to_string(), vec![]),
("a.b.c".to_string(), vec!["f2".to_string(), "f3".to_string()]),
],
)];
let text = default_keybindings("test-app", §ions);
let (file, notices) = parse_keybindings(&text);
assert!(notices.is_empty(), "{notices:?}\n{text}");
assert_eq!(file["commands"]["file.open"], vec!["ctrl-o"]);
assert!(file["commands"]["a.b"].is_empty());
assert_eq!(file["commands"]["a.b.c"], vec!["f2", "f3"]);
}
#[test]
fn keys_parse_for_matching_by_hand() {
assert_eq!(parse_key("alt-g"), Some((false, true, false, "g".into())));
assert_eq!(parse_key("ctrl-shift-z"), Some((true, false, true, "z".into())));
assert_eq!(parse_key("shift-space"), Some((false, false, true, "space".into())));
assert_eq!(parse_key("-"), Some((false, false, false, "-".into())));
assert_eq!(parse_key("ctrl--"), Some((true, false, false, "-".into())));
assert_eq!(parse_key("hyper-g"), None);
assert_eq!(parse_key(""), None);
}
}