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