settings.rs

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