settings.rs

  1mod base_keymap_setting;
  2mod content_into_gpui;
  3mod editable_setting_control;
  4mod editorconfig_store;
  5mod keymap_file;
  6mod settings_file;
  7mod settings_store;
  8mod vscode_import;
  9
 10pub use settings_macros::RegisterSetting;
 11
 12pub mod settings_content {
 13    pub use ::settings_content::*;
 14}
 15
 16pub mod fallible_options {
 17    pub use ::settings_content::{FallibleOption, parse_json};
 18}
 19
 20#[doc(hidden)]
 21pub mod private {
 22    pub use crate::settings_store::{RegisteredSetting, SettingValue};
 23    pub use inventory;
 24}
 25
 26use gpui::{App, Global};
 27use release_channel::ReleaseChannel;
 28use rust_embed::RustEmbed;
 29use std::env;
 30use std::{borrow::Cow, fmt, str};
 31use util::asset_str;
 32
 33pub use ::settings_content::*;
 34pub use base_keymap_setting::*;
 35pub use content_into_gpui::IntoGpui;
 36pub use editable_setting_control::*;
 37pub use editorconfig_store::{
 38    Editorconfig, EditorconfigEvent, EditorconfigProperties, EditorconfigStore,
 39};
 40pub use keymap_file::{
 41    KeyBindingValidator, KeyBindingValidatorRegistration, KeybindSource, KeybindUpdateOperation,
 42    KeybindUpdateTarget, KeymapFile, KeymapFileLoadResult,
 43};
 44pub use settings_file::*;
 45pub use settings_json::*;
 46pub use settings_store::{
 47    InvalidSettingsError, LSP_SETTINGS_SCHEMA_URL_PREFIX, LocalSettingsKind, LocalSettingsPath,
 48    MigrationStatus, Settings, SettingsFile, SettingsJsonSchemaParams, SettingsKey,
 49    SettingsLocation, SettingsParseResult, SettingsStore,
 50};
 51
 52pub use vscode_import::{VsCodeSettings, VsCodeSettingsSource};
 53
 54pub use keymap_file::ActionSequence;
 55
 56#[derive(Clone, Debug, PartialEq)]
 57pub struct ActiveSettingsProfileName(pub String);
 58
 59impl Global for ActiveSettingsProfileName {}
 60
 61pub trait UserSettingsContentExt {
 62    fn for_profile(&self, cx: &App) -> Option<&SettingsContent>;
 63    fn for_release_channel(&self) -> Option<&SettingsContent>;
 64    fn for_os(&self) -> Option<&SettingsContent>;
 65}
 66
 67impl UserSettingsContentExt for UserSettingsContent {
 68    fn for_profile(&self, cx: &App) -> Option<&SettingsContent> {
 69        let Some(active_profile) = cx.try_global::<ActiveSettingsProfileName>() else {
 70            return None;
 71        };
 72        self.profiles.get(&active_profile.0)
 73    }
 74
 75    fn for_release_channel(&self) -> Option<&SettingsContent> {
 76        match *release_channel::RELEASE_CHANNEL {
 77            ReleaseChannel::Dev => self.dev.as_deref(),
 78            ReleaseChannel::Nightly => self.nightly.as_deref(),
 79            ReleaseChannel::Preview => self.preview.as_deref(),
 80            ReleaseChannel::Stable => self.stable.as_deref(),
 81        }
 82    }
 83
 84    fn for_os(&self) -> Option<&SettingsContent> {
 85        match env::consts::OS {
 86            "macos" => self.macos.as_deref(),
 87            "linux" => self.linux.as_deref(),
 88            "windows" => self.windows.as_deref(),
 89            _ => None,
 90        }
 91    }
 92}
 93
 94#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord, serde::Serialize)]
 95pub struct WorktreeId(usize);
 96
 97impl From<WorktreeId> for usize {
 98    fn from(value: WorktreeId) -> Self {
 99        value.0
100    }
101}
102
103impl WorktreeId {
104    pub fn from_usize(handle_id: usize) -> Self {
105        Self(handle_id)
106    }
107
108    pub fn from_proto(id: u64) -> Self {
109        Self(id as usize)
110    }
111
112    pub fn to_proto(self) -> u64 {
113        self.0 as u64
114    }
115
116    pub fn to_usize(self) -> usize {
117        self.0
118    }
119}
120
121impl fmt::Display for WorktreeId {
122    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123        std::fmt::Display::fmt(&self.0, f)
124    }
125}
126
127#[derive(RustEmbed)]
128#[folder = "../../assets"]
129#[include = "settings/*"]
130#[include = "keymaps/*"]
131#[exclude = "*.DS_Store"]
132pub struct SettingsAssets;
133
134pub fn init(cx: &mut App) {
135    let settings = SettingsStore::new(cx, &default_settings());
136    cx.set_global(settings);
137    SettingsStore::observe_active_settings_profile_name(cx).detach();
138}
139
140pub fn default_settings() -> Cow<'static, str> {
141    asset_str::<SettingsAssets>("settings/default.json")
142}
143
144#[cfg(target_os = "macos")]
145pub const DEFAULT_KEYMAP_PATH: &str = "keymaps/default-macos.json";
146
147#[cfg(target_os = "windows")]
148pub const DEFAULT_KEYMAP_PATH: &str = "keymaps/default-windows.json";
149
150#[cfg(not(any(target_os = "macos", target_os = "windows")))]
151pub const DEFAULT_KEYMAP_PATH: &str = "keymaps/default-linux.json";
152
153pub fn default_keymap() -> Cow<'static, str> {
154    asset_str::<SettingsAssets>(DEFAULT_KEYMAP_PATH)
155}
156
157pub const VIM_KEYMAP_PATH: &str = "keymaps/vim.json";
158
159pub fn vim_keymap() -> Cow<'static, str> {
160    asset_str::<SettingsAssets>(VIM_KEYMAP_PATH)
161}
162
163pub fn initial_user_settings_content() -> Cow<'static, str> {
164    asset_str::<SettingsAssets>("settings/initial_user_settings.json")
165}
166
167pub fn initial_server_settings_content() -> Cow<'static, str> {
168    asset_str::<SettingsAssets>("settings/initial_server_settings.json")
169}
170
171pub fn initial_project_settings_content() -> Cow<'static, str> {
172    asset_str::<SettingsAssets>("settings/initial_local_settings.json")
173}
174
175pub fn initial_keymap_content() -> Cow<'static, str> {
176    asset_str::<SettingsAssets>("keymaps/initial.json")
177}
178
179pub fn initial_tasks_content() -> Cow<'static, str> {
180    asset_str::<SettingsAssets>("settings/initial_tasks.json")
181}
182
183pub fn initial_debug_tasks_content() -> Cow<'static, str> {
184    asset_str::<SettingsAssets>("settings/initial_debug_tasks.json")
185}
186
187pub fn initial_local_debug_tasks_content() -> Cow<'static, str> {
188    asset_str::<SettingsAssets>("settings/initial_local_debug_tasks.json")
189}