Move keymap_file module into settings crate

Co-authored-by: Keith Simmons <keith@zed.dev
This commit is contained in:
Max Brunsfeld 2022-04-11 15:22:18 -07:00
parent 9a4b8e3d8c
commit e0096ec1eb
5 changed files with 5 additions and 2 deletions

View file

@ -1,43 +0,0 @@
use anyhow::{Context, Result};
use collections::BTreeMap;
use gpui::{keymap::Binding, MutableAppContext};
use serde::Deserialize;
use serde_json::value::RawValue;
#[derive(Deserialize)]
struct ActionWithData<'a>(#[serde(borrow)] &'a str, #[serde(borrow)] &'a RawValue);
type ActionSetsByContext<'a> = BTreeMap<&'a str, ActionsByKeystroke<'a>>;
type ActionsByKeystroke<'a> = BTreeMap<&'a str, &'a RawValue>;
pub fn load_keymap(cx: &mut MutableAppContext, content: &str) -> Result<()> {
let actions: ActionSetsByContext = serde_json::from_str(content)?;
for (context, actions) in actions {
let context = if context.is_empty() {
None
} else {
Some(context)
};
cx.add_bindings(
actions
.into_iter()
.map(|(keystroke, action)| {
let action = action.get();
let action = if action.starts_with('[') {
let ActionWithData(name, data) = serde_json::from_str(action)?;
cx.deserialize_action(name, Some(data.get()))
} else {
let name = serde_json::from_str(action)?;
cx.deserialize_action(name, None)
}
.with_context(|| {
format!(
"invalid binding value for keystroke {keystroke}, context {context:?}"
)
})?;
Binding::load(keystroke, action, context)
})
.collect::<Result<Vec<_>>>()?,
)
}
Ok(())
}

View file

@ -1,5 +1,4 @@
pub mod assets;
mod keymap_file;
pub mod languages;
pub mod menus;
pub mod settings_file;
@ -105,7 +104,7 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) {
workspace::lsp_status::init(cx);
keymap_file::load_keymap(
settings::keymap_file::load_keymap(
cx,
std::str::from_utf8(Assets::get("keymaps/default.json").unwrap().data.as_ref()).unwrap(),
)