Load workspace and editor key bindings from a JSON file

This commit is contained in:
Max Brunsfeld 2022-04-08 17:32:58 -07:00
parent fd4b81c8fc
commit 3636c9ec25
12 changed files with 326 additions and 293 deletions

View file

@ -10,7 +10,7 @@ use crate::{
AssetCache, AssetSource, ClipboardItem, FontCache, PathPromptOptions, TextLayoutCache,
};
pub use action::*;
use anyhow::{anyhow, Result};
use anyhow::{anyhow, Context, Result};
use collections::btree_map;
use keymap::MatchResult;
use lazy_static::lazy_static;
@ -870,6 +870,7 @@ impl MutableAppContext {
.get(name)
.ok_or_else(|| anyhow!("unknown action {}", name))?;
callback(argument.unwrap_or("{}"))
.with_context(|| format!("invalid data for action {}", name))
}
pub fn add_action<A, V, F>(&mut self, handler: F)

View file

@ -1,5 +1,5 @@
use crate::Action;
use anyhow::anyhow;
use anyhow::{anyhow, Result};
use std::{
any::Any,
collections::{HashMap, HashSet},
@ -168,20 +168,26 @@ impl Keymap {
impl Binding {
pub fn new<A: Action>(keystrokes: &str, action: A, context: Option<&str>) -> Self {
Self::load(keystrokes, Box::new(action), context).unwrap()
}
pub fn load(keystrokes: &str, action: Box<dyn Action>, context: Option<&str>) -> Result<Self> {
let context = if let Some(context) = context {
Some(ContextPredicate::parse(context).unwrap())
Some(ContextPredicate::parse(context)?)
} else {
None
};
Self {
keystrokes: keystrokes
.split_whitespace()
.map(|key| Keystroke::parse(key).unwrap())
.collect(),
action: Box::new(action),
let keystrokes = keystrokes
.split_whitespace()
.map(|key| Keystroke::parse(key))
.collect::<Result<_>>()?;
Ok(Self {
keystrokes,
action,
context,
}
})
}
}