2024-01-06 19:41:35 +00:00
|
|
|
use std::fmt::{Display, Formatter};
|
|
|
|
|
2023-05-17 18:23:09 +00:00
|
|
|
use schemars::JsonSchema;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2024-04-08 23:17:12 +00:00
|
|
|
use settings::{Settings, SettingsSources};
|
2023-05-17 18:23:09 +00:00
|
|
|
|
2024-01-17 22:31:21 +00:00
|
|
|
/// Base key bindings scheme. Base keymaps can be overridden with user keymaps.
|
2024-01-08 18:30:18 +00:00
|
|
|
///
|
|
|
|
/// Default: VSCode
|
2023-05-17 18:23:09 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Default)]
|
|
|
|
pub enum BaseKeymap {
|
|
|
|
#[default]
|
|
|
|
VSCode,
|
|
|
|
JetBrains,
|
|
|
|
SublimeText,
|
|
|
|
Atom,
|
|
|
|
TextMate,
|
2024-11-07 20:22:53 +00:00
|
|
|
Emacs,
|
2024-03-21 00:52:17 +00:00
|
|
|
None,
|
2023-05-17 18:23:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 19:41:35 +00:00
|
|
|
impl Display for BaseKeymap {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
BaseKeymap::VSCode => write!(f, "VSCode"),
|
|
|
|
BaseKeymap::JetBrains => write!(f, "JetBrains"),
|
|
|
|
BaseKeymap::SublimeText => write!(f, "Sublime Text"),
|
|
|
|
BaseKeymap::Atom => write!(f, "Atom"),
|
|
|
|
BaseKeymap::TextMate => write!(f, "TextMate"),
|
2024-11-07 20:22:53 +00:00
|
|
|
BaseKeymap::Emacs => write!(f, "Emacs (beta)"),
|
2024-03-21 00:52:17 +00:00
|
|
|
BaseKeymap::None => write!(f, "None"),
|
2024-01-06 19:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 18:23:09 +00:00
|
|
|
impl BaseKeymap {
|
2024-07-08 19:05:29 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
2024-11-07 20:22:53 +00:00
|
|
|
pub const OPTIONS: [(&'static str, Self); 6] = [
|
2023-05-17 18:23:09 +00:00
|
|
|
("VSCode (Default)", Self::VSCode),
|
|
|
|
("Atom", Self::Atom),
|
|
|
|
("JetBrains", Self::JetBrains),
|
|
|
|
("Sublime Text", Self::SublimeText),
|
2024-11-07 20:22:53 +00:00
|
|
|
("Emacs (beta)", Self::Emacs),
|
2023-05-17 18:23:09 +00:00
|
|
|
("TextMate", Self::TextMate),
|
|
|
|
];
|
|
|
|
|
2024-07-08 19:05:29 +00:00
|
|
|
#[cfg(not(target_os = "macos"))]
|
2024-11-07 20:22:53 +00:00
|
|
|
pub const OPTIONS: [(&'static str, Self); 5] = [
|
2024-07-08 19:05:29 +00:00
|
|
|
("VSCode (Default)", Self::VSCode),
|
|
|
|
("Atom", Self::Atom),
|
|
|
|
("JetBrains", Self::JetBrains),
|
|
|
|
("Sublime Text", Self::SublimeText),
|
2024-11-07 20:22:53 +00:00
|
|
|
("Emacs (beta)", Self::Emacs),
|
2024-07-08 19:05:29 +00:00
|
|
|
];
|
|
|
|
|
2023-05-17 18:23:09 +00:00
|
|
|
pub fn asset_path(&self) -> Option<&'static str> {
|
2024-07-08 19:05:29 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
match self {
|
|
|
|
BaseKeymap::JetBrains => Some("keymaps/macos/jetbrains.json"),
|
|
|
|
BaseKeymap::SublimeText => Some("keymaps/macos/sublime_text.json"),
|
|
|
|
BaseKeymap::Atom => Some("keymaps/macos/atom.json"),
|
|
|
|
BaseKeymap::TextMate => Some("keymaps/macos/textmate.json"),
|
2024-11-07 20:22:53 +00:00
|
|
|
BaseKeymap::Emacs => Some("keymaps/macos/emacs.json"),
|
2024-07-08 19:05:29 +00:00
|
|
|
BaseKeymap::VSCode => None,
|
|
|
|
BaseKeymap::None => None,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
2023-05-17 18:23:09 +00:00
|
|
|
match self {
|
2024-07-08 19:05:29 +00:00
|
|
|
BaseKeymap::JetBrains => Some("keymaps/linux/jetbrains.json"),
|
|
|
|
BaseKeymap::SublimeText => Some("keymaps/linux/sublime_text.json"),
|
|
|
|
BaseKeymap::Atom => Some("keymaps/linux/atom.json"),
|
2024-11-07 20:22:53 +00:00
|
|
|
BaseKeymap::Emacs => Some("keymaps/linux/emacs.json"),
|
2024-07-08 19:05:29 +00:00
|
|
|
BaseKeymap::TextMate => None,
|
2023-05-17 18:23:09 +00:00
|
|
|
BaseKeymap::VSCode => None,
|
2024-03-21 00:52:17 +00:00
|
|
|
BaseKeymap::None => None,
|
2023-05-17 18:23:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn names() -> impl Iterator<Item = &'static str> {
|
|
|
|
Self::OPTIONS.iter().map(|(name, _)| *name)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_names(option: &str) -> BaseKeymap {
|
|
|
|
Self::OPTIONS
|
|
|
|
.iter()
|
|
|
|
.copied()
|
2024-09-06 09:58:39 +00:00
|
|
|
.find_map(|(name, value)| (name == option).then_some(value))
|
2023-05-17 18:23:09 +00:00
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
impl Settings for BaseKeymap {
|
2023-05-17 18:23:09 +00:00
|
|
|
const KEY: Option<&'static str> = Some("base_keymap");
|
|
|
|
|
2024-09-12 18:46:08 +00:00
|
|
|
type FileContent = Option<Self>;
|
2023-05-17 18:23:09 +00:00
|
|
|
|
|
|
|
fn load(
|
2024-04-08 23:17:12 +00:00
|
|
|
sources: SettingsSources<Self::FileContent>,
|
2024-01-03 18:30:52 +00:00
|
|
|
_: &mut gpui::AppContext,
|
2024-04-08 23:17:12 +00:00
|
|
|
) -> anyhow::Result<Self> {
|
2024-09-12 18:46:08 +00:00
|
|
|
if let Some(Some(user_value)) = sources.user.copied() {
|
2024-04-08 23:17:12 +00:00
|
|
|
return Ok(user_value);
|
|
|
|
}
|
2024-10-16 06:32:44 +00:00
|
|
|
if let Some(Some(server_value)) = sources.server.copied() {
|
|
|
|
return Ok(server_value);
|
|
|
|
}
|
2024-09-12 18:46:08 +00:00
|
|
|
sources.default.ok_or_else(Self::missing_default)
|
2023-05-17 18:23:09 +00:00
|
|
|
}
|
|
|
|
}
|