settings.rs

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