settings.rs

  1mod editable_setting_control;
  2mod json_schema;
  3mod key_equivalents;
  4mod keymap_file;
  5mod settings_file;
  6mod settings_store;
  7
  8use gpui::App;
  9use rust_embed::RustEmbed;
 10use std::{borrow::Cow, fmt, str};
 11use util::asset_str;
 12
 13pub use editable_setting_control::*;
 14pub use json_schema::*;
 15pub use key_equivalents::*;
 16pub use keymap_file::{
 17    KeyBindingValidator, KeyBindingValidatorRegistration, KeymapFile, KeymapFileLoadResult,
 18};
 19pub use settings_file::*;
 20pub use settings_store::{
 21    InvalidSettingsError, LocalSettingsKind, Settings, SettingsLocation, SettingsSources,
 22    SettingsStore, TaskKind, parse_json_with_comments,
 23};
 24
 25#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
 26pub struct WorktreeId(usize);
 27
 28impl From<WorktreeId> for usize {
 29    fn from(value: WorktreeId) -> Self {
 30        value.0
 31    }
 32}
 33
 34impl WorktreeId {
 35    pub fn from_usize(handle_id: usize) -> Self {
 36        Self(handle_id)
 37    }
 38
 39    pub fn from_proto(id: u64) -> Self {
 40        Self(id as usize)
 41    }
 42
 43    pub fn to_proto(&self) -> u64 {
 44        self.0 as u64
 45    }
 46
 47    pub fn to_usize(&self) -> usize {
 48        self.0
 49    }
 50}
 51
 52impl fmt::Display for WorktreeId {
 53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 54        std::fmt::Display::fmt(&self.0, f)
 55    }
 56}
 57
 58#[derive(RustEmbed)]
 59#[folder = "../../assets"]
 60#[include = "settings/*"]
 61#[include = "keymaps/*"]
 62#[exclude = "*.DS_Store"]
 63pub struct SettingsAssets;
 64
 65pub fn init(cx: &mut App) {
 66    let mut settings = SettingsStore::new(cx);
 67    settings
 68        .set_default_settings(&default_settings(), cx)
 69        .unwrap();
 70    cx.set_global(settings);
 71}
 72
 73pub fn default_settings() -> Cow<'static, str> {
 74    asset_str::<SettingsAssets>("settings/default.json")
 75}
 76
 77#[cfg(target_os = "macos")]
 78pub const DEFAULT_KEYMAP_PATH: &str = "keymaps/default-macos.json";
 79
 80#[cfg(not(target_os = "macos"))]
 81pub const DEFAULT_KEYMAP_PATH: &str = "keymaps/default-linux.json";
 82
 83pub fn default_keymap() -> Cow<'static, str> {
 84    asset_str::<SettingsAssets>(DEFAULT_KEYMAP_PATH)
 85}
 86
 87pub const VIM_KEYMAP_PATH: &str = "keymaps/vim.json";
 88
 89pub fn vim_keymap() -> Cow<'static, str> {
 90    asset_str::<SettingsAssets>(VIM_KEYMAP_PATH)
 91}
 92
 93pub fn initial_user_settings_content() -> Cow<'static, str> {
 94    asset_str::<SettingsAssets>("settings/initial_user_settings.json")
 95}
 96
 97pub fn initial_server_settings_content() -> Cow<'static, str> {
 98    asset_str::<SettingsAssets>("settings/initial_server_settings.json")
 99}
100
101pub fn initial_project_settings_content() -> Cow<'static, str> {
102    asset_str::<SettingsAssets>("settings/initial_local_settings.json")
103}
104
105pub fn initial_keymap_content() -> Cow<'static, str> {
106    asset_str::<SettingsAssets>("keymaps/initial.json")
107}
108
109pub fn initial_tasks_content() -> Cow<'static, str> {
110    asset_str::<SettingsAssets>("settings/initial_tasks.json")
111}
112
113pub fn initial_debug_tasks_content() -> Cow<'static, str> {
114    asset_str::<SettingsAssets>("settings/initial_debug_tasks.json")
115}