Add base keymap setting
Format all files Co-Authored-by: Nathan <nathan@zed.dev>
This commit is contained in:
parent
3b31f10c6f
commit
19fc143209
12 changed files with 301 additions and 35 deletions
|
@ -1,4 +1,4 @@
|
|||
use crate::parse_json_with_comments;
|
||||
use crate::{parse_json_with_comments, Settings};
|
||||
use anyhow::{Context, Result};
|
||||
use assets::Assets;
|
||||
use collections::BTreeMap;
|
||||
|
@ -45,6 +45,10 @@ impl KeymapFileContent {
|
|||
for path in ["keymaps/default.json", "keymaps/vim.json"] {
|
||||
Self::load(path, cx).unwrap();
|
||||
}
|
||||
|
||||
if let Some(base_keymap) = cx.global::<Settings>().base_keymap {
|
||||
Self::load(base_keymap.asset_path(), cx).log_err();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(asset_path: &str, cx: &mut MutableAppContext) -> Result<()> {
|
||||
|
|
|
@ -24,6 +24,7 @@ use tree_sitter::Query;
|
|||
use util::ResultExt as _;
|
||||
|
||||
pub use keymap_file::{keymap_file_json_schema, KeymapFileContent};
|
||||
pub use watched_json::watch_files;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Settings {
|
||||
|
@ -54,6 +55,24 @@ pub struct Settings {
|
|||
pub telemetry_defaults: TelemetrySettings,
|
||||
pub telemetry_overrides: TelemetrySettings,
|
||||
pub auto_update: bool,
|
||||
pub base_keymap: Option<BaseKeymap>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
|
||||
pub enum BaseKeymap {
|
||||
JetBrains,
|
||||
Sublime,
|
||||
Atom,
|
||||
}
|
||||
|
||||
impl BaseKeymap {
|
||||
pub fn asset_path(&self) -> &str {
|
||||
match self {
|
||||
BaseKeymap::JetBrains => "keymaps/jetbrains.json",
|
||||
BaseKeymap::Sublime => "keymaps/sublime_text.json",
|
||||
BaseKeymap::Atom => "keymaps/atom.json",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
|
||||
|
@ -326,6 +345,8 @@ pub struct SettingsFileContent {
|
|||
pub telemetry: TelemetrySettings,
|
||||
#[serde(default)]
|
||||
pub auto_update: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub base_keymap: Option<BaseKeymap>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
|
||||
|
@ -396,6 +417,7 @@ impl Settings {
|
|||
telemetry_defaults: defaults.telemetry,
|
||||
telemetry_overrides: Default::default(),
|
||||
auto_update: defaults.auto_update.unwrap(),
|
||||
base_keymap: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -433,6 +455,7 @@ impl Settings {
|
|||
merge(&mut self.vim_mode, data.vim_mode);
|
||||
merge(&mut self.autosave, data.autosave);
|
||||
merge(&mut self.default_dock_anchor, data.default_dock_anchor);
|
||||
merge(&mut self.base_keymap, Some(data.base_keymap));
|
||||
|
||||
// Ensure terminal font is loaded, so we can request it in terminal_element layout
|
||||
if let Some(terminal_font) = &data.terminal.font_family {
|
||||
|
@ -610,6 +633,7 @@ impl Settings {
|
|||
},
|
||||
telemetry_overrides: Default::default(),
|
||||
auto_update: true,
|
||||
base_keymap: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -62,7 +62,18 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub fn watch_settings_file(
|
||||
pub fn watch_files(
|
||||
defaults: Settings,
|
||||
settings_file: WatchedJsonFile<SettingsFileContent>,
|
||||
theme_registry: Arc<ThemeRegistry>,
|
||||
keymap_file: WatchedJsonFile<KeymapFileContent>,
|
||||
cx: &mut MutableAppContext,
|
||||
) {
|
||||
watch_settings_file(defaults, settings_file, theme_registry, cx);
|
||||
watch_keymap_file(keymap_file, cx);
|
||||
}
|
||||
|
||||
pub(crate) fn watch_settings_file(
|
||||
defaults: Settings,
|
||||
mut file: WatchedJsonFile<SettingsFileContent>,
|
||||
theme_registry: Arc<ThemeRegistry>,
|
||||
|
@ -77,13 +88,13 @@ pub fn watch_settings_file(
|
|||
.detach();
|
||||
}
|
||||
|
||||
pub fn keymap_updated(content: KeymapFileContent, cx: &mut MutableAppContext) {
|
||||
fn keymap_updated(content: KeymapFileContent, cx: &mut MutableAppContext) {
|
||||
cx.clear_bindings();
|
||||
KeymapFileContent::load_defaults(cx);
|
||||
content.add_to_cx(cx).log_err();
|
||||
}
|
||||
|
||||
pub fn settings_updated(
|
||||
fn settings_updated(
|
||||
defaults: &Settings,
|
||||
content: SettingsFileContent,
|
||||
theme_registry: &Arc<ThemeRegistry>,
|
||||
|
@ -95,10 +106,20 @@ pub fn settings_updated(
|
|||
cx.refresh_windows();
|
||||
}
|
||||
|
||||
pub fn watch_keymap_file(mut file: WatchedJsonFile<KeymapFileContent>, cx: &mut MutableAppContext) {
|
||||
fn watch_keymap_file(mut file: WatchedJsonFile<KeymapFileContent>, cx: &mut MutableAppContext) {
|
||||
cx.spawn(|mut cx| async move {
|
||||
let mut settings_subscription = None;
|
||||
while let Some(content) = file.0.recv().await {
|
||||
cx.update(|cx| keymap_updated(content, cx));
|
||||
cx.update(|cx| {
|
||||
let old_base_keymap = cx.global::<Settings>().base_keymap;
|
||||
keymap_updated(content.clone(), cx);
|
||||
settings_subscription = Some(cx.observe_global::<Settings, _>(move |cx| {
|
||||
let settings = cx.global::<Settings>();
|
||||
if settings.base_keymap != old_base_keymap {
|
||||
keymap_updated(content.clone(), cx);
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue