base_keymap_setting.rs

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