base_keymap_setting.rs

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