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