base_keymap_setting.rs

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