base_keymap_setting.rs

  1use std::fmt::{Display, Formatter};
  2
  3use crate::{self as settings, settings_content::BaseKeymapContent};
  4use schemars::JsonSchema;
  5use serde::{Deserialize, Serialize};
  6use settings::Settings;
  7
  8/// Base key bindings scheme. Base keymaps can be overridden with user keymaps.
  9///
 10/// Default: VSCode
 11#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Default)]
 12pub enum BaseKeymap {
 13    #[default]
 14    VSCode,
 15    JetBrains,
 16    SublimeText,
 17    Atom,
 18    TextMate,
 19    Emacs,
 20    Cursor,
 21    None,
 22}
 23
 24impl From<BaseKeymapContent> for BaseKeymap {
 25    fn from(value: BaseKeymapContent) -> Self {
 26        match value {
 27            BaseKeymapContent::VSCode => Self::VSCode,
 28            BaseKeymapContent::JetBrains => Self::JetBrains,
 29            BaseKeymapContent::SublimeText => Self::SublimeText,
 30            BaseKeymapContent::Atom => Self::Atom,
 31            BaseKeymapContent::TextMate => Self::TextMate,
 32            BaseKeymapContent::Emacs => Self::Emacs,
 33            BaseKeymapContent::Cursor => Self::Cursor,
 34            BaseKeymapContent::None => Self::None,
 35        }
 36    }
 37}
 38impl Into<BaseKeymapContent> for BaseKeymap {
 39    fn into(self) -> BaseKeymapContent {
 40        match self {
 41            BaseKeymap::VSCode => BaseKeymapContent::VSCode,
 42            BaseKeymap::JetBrains => BaseKeymapContent::JetBrains,
 43            BaseKeymap::SublimeText => BaseKeymapContent::SublimeText,
 44            BaseKeymap::Atom => BaseKeymapContent::Atom,
 45            BaseKeymap::TextMate => BaseKeymapContent::TextMate,
 46            BaseKeymap::Emacs => BaseKeymapContent::Emacs,
 47            BaseKeymap::Cursor => BaseKeymapContent::Cursor,
 48            BaseKeymap::None => BaseKeymapContent::None,
 49        }
 50    }
 51}
 52
 53impl Display for BaseKeymap {
 54    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 55        match self {
 56            BaseKeymap::VSCode => write!(f, "VS Code"),
 57            BaseKeymap::JetBrains => write!(f, "JetBrains"),
 58            BaseKeymap::SublimeText => write!(f, "Sublime Text"),
 59            BaseKeymap::Atom => write!(f, "Atom"),
 60            BaseKeymap::TextMate => write!(f, "TextMate"),
 61            BaseKeymap::Emacs => write!(f, "Emacs (beta)"),
 62            BaseKeymap::Cursor => write!(f, "Cursor (beta)"),
 63            BaseKeymap::None => write!(f, "None"),
 64        }
 65    }
 66}
 67
 68impl BaseKeymap {
 69    #[cfg(target_os = "macos")]
 70    pub const OPTIONS: [(&'static str, Self); 7] = [
 71        ("VS Code (Default)", Self::VSCode),
 72        ("Atom", Self::Atom),
 73        ("JetBrains", Self::JetBrains),
 74        ("Sublime Text", Self::SublimeText),
 75        ("Emacs (beta)", Self::Emacs),
 76        ("TextMate", Self::TextMate),
 77        ("Cursor", Self::Cursor),
 78    ];
 79
 80    #[cfg(not(target_os = "macos"))]
 81    pub const OPTIONS: [(&'static str, Self); 6] = [
 82        ("VS Code (Default)", Self::VSCode),
 83        ("Atom", Self::Atom),
 84        ("JetBrains", Self::JetBrains),
 85        ("Sublime Text", Self::SublimeText),
 86        ("Emacs (beta)", Self::Emacs),
 87        ("Cursor", Self::Cursor),
 88    ];
 89
 90    pub fn asset_path(&self) -> Option<&'static str> {
 91        #[cfg(target_os = "macos")]
 92        match self {
 93            BaseKeymap::JetBrains => Some("keymaps/macos/jetbrains.json"),
 94            BaseKeymap::SublimeText => Some("keymaps/macos/sublime_text.json"),
 95            BaseKeymap::Atom => Some("keymaps/macos/atom.json"),
 96            BaseKeymap::TextMate => Some("keymaps/macos/textmate.json"),
 97            BaseKeymap::Emacs => Some("keymaps/macos/emacs.json"),
 98            BaseKeymap::Cursor => Some("keymaps/macos/cursor.json"),
 99            BaseKeymap::VSCode => None,
100            BaseKeymap::None => None,
101        }
102
103        #[cfg(not(target_os = "macos"))]
104        match self {
105            BaseKeymap::JetBrains => Some("keymaps/linux/jetbrains.json"),
106            BaseKeymap::SublimeText => Some("keymaps/linux/sublime_text.json"),
107            BaseKeymap::Atom => Some("keymaps/linux/atom.json"),
108            BaseKeymap::Emacs => Some("keymaps/linux/emacs.json"),
109            BaseKeymap::Cursor => Some("keymaps/linux/cursor.json"),
110            BaseKeymap::TextMate => None,
111            BaseKeymap::VSCode => None,
112            BaseKeymap::None => None,
113        }
114    }
115
116    pub fn names() -> impl Iterator<Item = &'static str> {
117        Self::OPTIONS.iter().map(|(name, _)| *name)
118    }
119
120    pub fn from_names(option: &str) -> BaseKeymap {
121        Self::OPTIONS
122            .iter()
123            .copied()
124            .find_map(|(name, value)| (name == option).then_some(value))
125            .unwrap_or_default()
126    }
127}
128
129impl Settings for BaseKeymap {
130    fn from_settings(s: &crate::settings_content::SettingsContent) -> Self {
131        s.base_keymap.unwrap().into()
132    }
133}