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