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

@ -0,0 +1,38 @@
use anyhow::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)
}?;
Binding::load(keystroke, action, context)
})
.collect::<Result<Vec<_>>>()?,
)
}
Ok(())
}

View file

@ -145,8 +145,8 @@ fn main() {
build_workspace: &build_workspace,
});
journal::init(app_state.clone(), cx);
zed::init(&app_state, cx);
theme_selector::init(cx);
zed::init(&app_state, cx);
cx.set_menus(menus::menus(&app_state.clone()));

View file

@ -1,10 +1,12 @@
pub mod assets;
mod keymap_file;
pub mod languages;
pub mod menus;
pub mod settings_file;
#[cfg(any(test, feature = "test-support"))]
pub mod test;
use assets::Assets;
use breadcrumbs::Breadcrumbs;
use chat_panel::ChatPanel;
pub use client;
@ -14,7 +16,6 @@ pub use editor;
use gpui::{
actions,
geometry::vector::vec2f,
keymap::Binding,
platform::{WindowBounds, WindowOptions},
ModelHandle, ViewContext,
};
@ -104,11 +105,11 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) {
workspace::lsp_status::init(cx);
cx.add_bindings(vec![
Binding::new("cmd-=", IncreaseBufferFontSize, None),
Binding::new("cmd--", DecreaseBufferFontSize, None),
Binding::new("cmd-,", OpenSettings, None),
])
keymap_file::load_keymap(
cx,
std::str::from_utf8(Assets::get("keymaps/default.json").unwrap().data.as_ref()).unwrap(),
)
.unwrap();
}
pub fn build_workspace(
@ -208,9 +209,8 @@ fn quit(_: &Quit, cx: &mut gpui::MutableAppContext) {
#[cfg(test)]
mod tests {
use crate::assets::Assets;
use super::*;
use crate::assets::Assets;
use editor::{DisplayPoint, Editor};
use gpui::{AssetSource, MutableAppContext, TestAppContext, ViewHandle};
use project::{Fs, ProjectPath};