base_keymap_setting.rs

 1use std::fmt::{Display, Formatter};
 2
 3use schemars::JsonSchema;
 4use serde::{Deserialize, Serialize};
 5use settings::{Settings, SettingsSources};
 6
 7/// Base key bindings scheme. Base keymaps can be overridden with user keymaps.
 8///
 9/// Default: VSCode
10#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Default)]
11pub enum BaseKeymap {
12    #[default]
13    VSCode,
14    JetBrains,
15    SublimeText,
16    Atom,
17    TextMate,
18    None,
19}
20
21impl Display for BaseKeymap {
22    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23        match self {
24            BaseKeymap::VSCode => write!(f, "VSCode"),
25            BaseKeymap::JetBrains => write!(f, "JetBrains"),
26            BaseKeymap::SublimeText => write!(f, "Sublime Text"),
27            BaseKeymap::Atom => write!(f, "Atom"),
28            BaseKeymap::TextMate => write!(f, "TextMate"),
29            BaseKeymap::None => write!(f, "None"),
30        }
31    }
32}
33
34impl BaseKeymap {
35    pub const OPTIONS: [(&'static str, Self); 5] = [
36        ("VSCode (Default)", Self::VSCode),
37        ("Atom", Self::Atom),
38        ("JetBrains", Self::JetBrains),
39        ("Sublime Text", Self::SublimeText),
40        ("TextMate", Self::TextMate),
41    ];
42
43    pub fn asset_path(&self) -> Option<&'static str> {
44        match self {
45            BaseKeymap::JetBrains => Some("keymaps/jetbrains.json"),
46            BaseKeymap::SublimeText => Some("keymaps/sublime_text.json"),
47            BaseKeymap::Atom => Some("keymaps/atom.json"),
48            BaseKeymap::TextMate => Some("keymaps/textmate.json"),
49            BaseKeymap::VSCode => None,
50            BaseKeymap::None => None,
51        }
52    }
53
54    pub fn names() -> impl Iterator<Item = &'static str> {
55        Self::OPTIONS.iter().map(|(name, _)| *name)
56    }
57
58    pub fn from_names(option: &str) -> BaseKeymap {
59        Self::OPTIONS
60            .iter()
61            .copied()
62            .find_map(|(name, value)| (name == option).then(|| value))
63            .unwrap_or_default()
64    }
65}
66
67impl Settings for BaseKeymap {
68    const KEY: Option<&'static str> = Some("base_keymap");
69
70    type FileContent = Option<Self>;
71
72    fn load(
73        sources: SettingsSources<Self::FileContent>,
74        _: &mut gpui::AppContext,
75    ) -> anyhow::Result<Self> {
76        if let Some(Some(user_value)) = sources.user.copied() {
77            return Ok(user_value);
78        }
79        sources.default.ok_or_else(Self::missing_default)
80    }
81}