settings_store.rs

   1use anyhow::{Context as _, Result};
   2use collections::{BTreeMap, HashMap, btree_map, hash_map};
   3use ec4rs::{ConfigParser, PropertiesSource, Section};
   4use fs::Fs;
   5use futures::{
   6    FutureExt, StreamExt,
   7    channel::{mpsc, oneshot},
   8    future::LocalBoxFuture,
   9};
  10use gpui::{App, AsyncApp, BorrowAppContext, Global, SharedString, Task, UpdateGlobal};
  11
  12use paths::{EDITORCONFIG_NAME, local_settings_file_relative_path, task_file_name};
  13use schemars::JsonSchema;
  14use serde::{Deserialize, Serialize, de::DeserializeOwned};
  15use serde_json::{Value, json};
  16use smallvec::SmallVec;
  17use std::{
  18    any::{Any, TypeId, type_name},
  19    env,
  20    fmt::Debug,
  21    ops::Range,
  22    path::{Path, PathBuf},
  23    str::{self, FromStr},
  24    sync::Arc,
  25};
  26use util::{
  27    ResultExt as _, merge_non_null_json_value_into,
  28    schemars::{DefaultDenyUnknownFields, add_new_subschema},
  29};
  30
  31pub type EditorconfigProperties = ec4rs::Properties;
  32
  33use crate::{
  34    ActiveSettingsProfileName, ParameterizedJsonSchema, SettingsJsonSchemaParams, SettingsUiEntry,
  35    VsCodeSettings, WorktreeId, parse_json_with_comments, replace_value_in_json_text,
  36    settings_ui::SettingsUi, update_value_in_json_text,
  37};
  38
  39/// A value that can be defined as a user setting.
  40///
  41/// Settings can be loaded from a combination of multiple JSON files.
  42pub trait Settings: SettingsUi + 'static + Send + Sync {
  43    /// The name of a key within the JSON file from which this setting should
  44    /// be deserialized. If this is `None`, then the setting will be deserialized
  45    /// from the root object.
  46    const KEY: Option<&'static str>;
  47
  48    const FALLBACK_KEY: Option<&'static str> = None;
  49
  50    /// The name of the keys in the [`FileContent`](Self::FileContent) that should
  51    /// always be written to a settings file, even if their value matches the default
  52    /// value.
  53    ///
  54    /// This is useful for tagged [`FileContent`](Self::FileContent)s where the tag
  55    /// is a "version" field that should always be persisted, even if the current
  56    /// user settings match the current version of the settings.
  57    const PRESERVED_KEYS: Option<&'static [&'static str]> = None;
  58
  59    /// The type that is stored in an individual JSON file.
  60    type FileContent: Clone + Default + Serialize + DeserializeOwned + JsonSchema;
  61
  62    /// The logic for combining together values from one or more JSON files into the
  63    /// final value for this setting.
  64    ///
  65    /// # Warning
  66    /// `Self::FileContent` deserialized field names should match with `Self` deserialized field names
  67    /// otherwise the field won't be deserialized properly and you will get the error:
  68    /// "A default setting must be added to the `default.json` file"
  69    fn load(sources: SettingsSources<Self::FileContent>, cx: &mut App) -> Result<Self>
  70    where
  71        Self: Sized;
  72
  73    fn missing_default() -> anyhow::Error {
  74        anyhow::anyhow!("missing default")
  75    }
  76
  77    /// Use [the helpers in the vscode_import module](crate::vscode_import) to apply known
  78    /// equivalent settings from a vscode config to our config
  79    fn import_from_vscode(vscode: &VsCodeSettings, current: &mut Self::FileContent);
  80
  81    #[track_caller]
  82    fn register(cx: &mut App)
  83    where
  84        Self: Sized,
  85    {
  86        SettingsStore::update_global(cx, |store, cx| {
  87            store.register_setting::<Self>(cx);
  88        });
  89    }
  90
  91    #[track_caller]
  92    fn get<'a>(path: Option<SettingsLocation>, cx: &'a App) -> &'a Self
  93    where
  94        Self: Sized,
  95    {
  96        cx.global::<SettingsStore>().get(path)
  97    }
  98
  99    #[track_caller]
 100    fn get_global(cx: &App) -> &Self
 101    where
 102        Self: Sized,
 103    {
 104        cx.global::<SettingsStore>().get(None)
 105    }
 106
 107    #[track_caller]
 108    fn try_get(cx: &App) -> Option<&Self>
 109    where
 110        Self: Sized,
 111    {
 112        if cx.has_global::<SettingsStore>() {
 113            cx.global::<SettingsStore>().try_get(None)
 114        } else {
 115            None
 116        }
 117    }
 118
 119    #[track_caller]
 120    fn try_read_global<R>(cx: &AsyncApp, f: impl FnOnce(&Self) -> R) -> Option<R>
 121    where
 122        Self: Sized,
 123    {
 124        cx.try_read_global(|s: &SettingsStore, _| f(s.get(None)))
 125    }
 126
 127    #[track_caller]
 128    fn override_global(settings: Self, cx: &mut App)
 129    where
 130        Self: Sized,
 131    {
 132        cx.global_mut::<SettingsStore>().override_global(settings)
 133    }
 134}
 135
 136#[derive(Clone, Copy, Debug)]
 137pub struct SettingsSources<'a, T> {
 138    /// The default Zed settings.
 139    pub default: &'a T,
 140    /// Global settings (loaded before user settings).
 141    pub global: Option<&'a T>,
 142    /// Settings provided by extensions.
 143    pub extensions: Option<&'a T>,
 144    /// The user settings.
 145    pub user: Option<&'a T>,
 146    /// The user settings for the current release channel.
 147    pub release_channel: Option<&'a T>,
 148    /// The user settings for the current operating system.
 149    pub operating_system: Option<&'a T>,
 150    /// The settings associated with an enabled settings profile
 151    pub profile: Option<&'a T>,
 152    /// The server's settings.
 153    pub server: Option<&'a T>,
 154    /// The project settings, ordered from least specific to most specific.
 155    pub project: &'a [&'a T],
 156}
 157
 158impl<'a, T: Serialize> SettingsSources<'a, T> {
 159    /// Returns an iterator over the default settings as well as all settings customizations.
 160    pub fn defaults_and_customizations(&self) -> impl Iterator<Item = &T> {
 161        [self.default].into_iter().chain(self.customizations())
 162    }
 163
 164    /// Returns an iterator over all of the settings customizations.
 165    pub fn customizations(&self) -> impl Iterator<Item = &T> {
 166        self.global
 167            .into_iter()
 168            .chain(self.extensions)
 169            .chain(self.user)
 170            .chain(self.release_channel)
 171            .chain(self.operating_system)
 172            .chain(self.profile)
 173            .chain(self.server)
 174            .chain(self.project.iter().copied())
 175    }
 176
 177    /// Returns the settings after performing a JSON merge of the provided customizations.
 178    ///
 179    /// Customizations later in the iterator win out over the earlier ones.
 180    pub fn json_merge_with<O: DeserializeOwned>(
 181        customizations: impl Iterator<Item = &'a T>,
 182    ) -> Result<O> {
 183        let mut merged = Value::Null;
 184        for value in customizations {
 185            merge_non_null_json_value_into(serde_json::to_value(value).unwrap(), &mut merged);
 186        }
 187        Ok(serde_json::from_value(merged)?)
 188    }
 189
 190    /// Returns the settings after performing a JSON merge of the customizations into the
 191    /// default settings.
 192    ///
 193    /// More-specific customizations win out over the less-specific ones.
 194    pub fn json_merge<O: DeserializeOwned>(&'a self) -> Result<O> {
 195        Self::json_merge_with(self.defaults_and_customizations())
 196    }
 197}
 198
 199#[derive(Clone, Copy, Debug)]
 200pub struct SettingsLocation<'a> {
 201    pub worktree_id: WorktreeId,
 202    pub path: &'a Path,
 203}
 204
 205/// A set of strongly-typed setting values defined via multiple config files.
 206pub struct SettingsStore {
 207    setting_values: HashMap<TypeId, Box<dyn AnySettingValue>>,
 208    raw_default_settings: Value,
 209    raw_global_settings: Option<Value>,
 210    raw_user_settings: Value,
 211    raw_server_settings: Option<Value>,
 212    raw_extension_settings: Value,
 213    raw_local_settings: BTreeMap<(WorktreeId, Arc<Path>), Value>,
 214    raw_editorconfig_settings: BTreeMap<(WorktreeId, Arc<Path>), (String, Option<Editorconfig>)>,
 215    tab_size_callback: Option<(
 216        TypeId,
 217        Box<dyn Fn(&dyn Any) -> Option<usize> + Send + Sync + 'static>,
 218    )>,
 219    _setting_file_updates: Task<()>,
 220    setting_file_updates_tx:
 221        mpsc::UnboundedSender<Box<dyn FnOnce(AsyncApp) -> LocalBoxFuture<'static, Result<()>>>>,
 222}
 223
 224#[derive(Clone)]
 225pub struct Editorconfig {
 226    pub is_root: bool,
 227    pub sections: SmallVec<[Section; 5]>,
 228}
 229
 230impl FromStr for Editorconfig {
 231    type Err = anyhow::Error;
 232
 233    fn from_str(contents: &str) -> Result<Self, Self::Err> {
 234        let parser = ConfigParser::new_buffered(contents.as_bytes())
 235            .context("creating editorconfig parser")?;
 236        let is_root = parser.is_root;
 237        let sections = parser
 238            .collect::<Result<SmallVec<_>, _>>()
 239            .context("parsing editorconfig sections")?;
 240        Ok(Self { is_root, sections })
 241    }
 242}
 243
 244#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
 245pub enum LocalSettingsKind {
 246    Settings,
 247    Tasks,
 248    Editorconfig,
 249    Debug,
 250}
 251
 252impl Global for SettingsStore {}
 253
 254#[derive(Debug)]
 255struct SettingValue<T> {
 256    global_value: Option<T>,
 257    local_values: Vec<(WorktreeId, Arc<Path>, T)>,
 258}
 259
 260trait AnySettingValue: 'static + Send + Sync {
 261    fn key(&self) -> Option<&'static str>;
 262    fn setting_type_name(&self) -> &'static str;
 263    fn deserialize_setting(&self, json: &Value) -> Result<DeserializedSetting> {
 264        self.deserialize_setting_with_key(json).1
 265    }
 266    fn deserialize_setting_with_key(
 267        &self,
 268        json: &Value,
 269    ) -> (Option<&'static str>, Result<DeserializedSetting>);
 270    fn load_setting(
 271        &self,
 272        sources: SettingsSources<DeserializedSetting>,
 273        cx: &mut App,
 274    ) -> Result<Box<dyn Any>>;
 275    fn value_for_path(&self, path: Option<SettingsLocation>) -> &dyn Any;
 276    fn all_local_values(&self) -> Vec<(WorktreeId, Arc<Path>, &dyn Any)>;
 277    fn set_global_value(&mut self, value: Box<dyn Any>);
 278    fn set_local_value(&mut self, root_id: WorktreeId, path: Arc<Path>, value: Box<dyn Any>);
 279    fn json_schema(&self, generator: &mut schemars::SchemaGenerator) -> schemars::Schema;
 280    fn edits_for_update(
 281        &self,
 282        raw_settings: &serde_json::Value,
 283        tab_size: usize,
 284        vscode_settings: &VsCodeSettings,
 285        text: &mut String,
 286        edits: &mut Vec<(Range<usize>, String)>,
 287    );
 288    fn settings_ui_item(&self) -> SettingsUiEntry;
 289}
 290
 291struct DeserializedSetting(Box<dyn Any>);
 292
 293impl SettingsStore {
 294    pub fn new(cx: &App) -> Self {
 295        let (setting_file_updates_tx, mut setting_file_updates_rx) = mpsc::unbounded();
 296        Self {
 297            setting_values: Default::default(),
 298            raw_default_settings: json!({}),
 299            raw_global_settings: None,
 300            raw_user_settings: json!({}),
 301            raw_server_settings: None,
 302            raw_extension_settings: json!({}),
 303            raw_local_settings: Default::default(),
 304            raw_editorconfig_settings: BTreeMap::default(),
 305            tab_size_callback: Default::default(),
 306            setting_file_updates_tx,
 307            _setting_file_updates: cx.spawn(async move |cx| {
 308                while let Some(setting_file_update) = setting_file_updates_rx.next().await {
 309                    (setting_file_update)(cx.clone()).await.log_err();
 310                }
 311            }),
 312        }
 313    }
 314
 315    pub fn observe_active_settings_profile_name(cx: &mut App) -> gpui::Subscription {
 316        cx.observe_global::<ActiveSettingsProfileName>(|cx| {
 317            Self::update_global(cx, |store, cx| {
 318                store.recompute_values(None, cx).log_err();
 319            });
 320        })
 321    }
 322
 323    pub fn update<C, R>(cx: &mut C, f: impl FnOnce(&mut Self, &mut C) -> R) -> R
 324    where
 325        C: BorrowAppContext,
 326    {
 327        cx.update_global(f)
 328    }
 329
 330    /// Add a new type of setting to the store.
 331    pub fn register_setting<T: Settings>(&mut self, cx: &mut App) {
 332        let setting_type_id = TypeId::of::<T>();
 333        let entry = self.setting_values.entry(setting_type_id);
 334
 335        if matches!(entry, hash_map::Entry::Occupied(_)) {
 336            return;
 337        }
 338
 339        let setting_value = entry.or_insert(Box::new(SettingValue::<T> {
 340            global_value: None,
 341            local_values: Vec::new(),
 342        }));
 343
 344        if let Some(default_settings) = setting_value
 345            .deserialize_setting(&self.raw_default_settings)
 346            .log_err()
 347        {
 348            let user_value = setting_value
 349                .deserialize_setting(&self.raw_user_settings)
 350                .log_err();
 351
 352            let mut release_channel_value = None;
 353            if let Some(release_settings) = &self
 354                .raw_user_settings
 355                .get(release_channel::RELEASE_CHANNEL.dev_name())
 356            {
 357                release_channel_value = setting_value
 358                    .deserialize_setting(release_settings)
 359                    .log_err();
 360            }
 361
 362            let mut os_settings_value = None;
 363            if let Some(os_settings) = &self.raw_user_settings.get(env::consts::OS) {
 364                os_settings_value = setting_value.deserialize_setting(os_settings).log_err();
 365            }
 366
 367            let mut profile_value = None;
 368            if let Some(active_profile) = cx.try_global::<ActiveSettingsProfileName>()
 369                && let Some(profiles) = self.raw_user_settings.get("profiles")
 370                && let Some(profile_settings) = profiles.get(&active_profile.0)
 371            {
 372                profile_value = setting_value
 373                    .deserialize_setting(profile_settings)
 374                    .log_err();
 375            }
 376
 377            let server_value = self
 378                .raw_server_settings
 379                .as_ref()
 380                .and_then(|server_setting| {
 381                    setting_value.deserialize_setting(server_setting).log_err()
 382                });
 383
 384            let extension_value = setting_value
 385                .deserialize_setting(&self.raw_extension_settings)
 386                .log_err();
 387
 388            if let Some(setting) = setting_value
 389                .load_setting(
 390                    SettingsSources {
 391                        default: &default_settings,
 392                        global: None,
 393                        extensions: extension_value.as_ref(),
 394                        user: user_value.as_ref(),
 395                        release_channel: release_channel_value.as_ref(),
 396                        operating_system: os_settings_value.as_ref(),
 397                        profile: profile_value.as_ref(),
 398                        server: server_value.as_ref(),
 399                        project: &[],
 400                    },
 401                    cx,
 402                )
 403                .context("A default setting must be added to the `default.json` file")
 404                .log_err()
 405            {
 406                setting_value.set_global_value(setting);
 407            }
 408        }
 409    }
 410
 411    /// Get the value of a setting.
 412    ///
 413    /// Panics if the given setting type has not been registered, or if there is no
 414    /// value for this setting.
 415    pub fn get<T: Settings>(&self, path: Option<SettingsLocation>) -> &T {
 416        self.setting_values
 417            .get(&TypeId::of::<T>())
 418            .unwrap_or_else(|| panic!("unregistered setting type {}", type_name::<T>()))
 419            .value_for_path(path)
 420            .downcast_ref::<T>()
 421            .expect("no default value for setting type")
 422    }
 423
 424    /// Get the value of a setting.
 425    ///
 426    /// Does not panic
 427    pub fn try_get<T: Settings>(&self, path: Option<SettingsLocation>) -> Option<&T> {
 428        self.setting_values
 429            .get(&TypeId::of::<T>())
 430            .map(|value| value.value_for_path(path))
 431            .and_then(|value| value.downcast_ref::<T>())
 432    }
 433
 434    /// Get all values from project specific settings
 435    pub fn get_all_locals<T: Settings>(&self) -> Vec<(WorktreeId, Arc<Path>, &T)> {
 436        self.setting_values
 437            .get(&TypeId::of::<T>())
 438            .unwrap_or_else(|| panic!("unregistered setting type {}", type_name::<T>()))
 439            .all_local_values()
 440            .into_iter()
 441            .map(|(id, path, any)| {
 442                (
 443                    id,
 444                    path,
 445                    any.downcast_ref::<T>()
 446                        .expect("wrong value type for setting"),
 447                )
 448            })
 449            .collect()
 450    }
 451
 452    /// Override the global value for a setting.
 453    ///
 454    /// The given value will be overwritten if the user settings file changes.
 455    pub fn override_global<T: Settings>(&mut self, value: T) {
 456        self.setting_values
 457            .get_mut(&TypeId::of::<T>())
 458            .unwrap_or_else(|| panic!("unregistered setting type {}", type_name::<T>()))
 459            .set_global_value(Box::new(value))
 460    }
 461
 462    /// Get the user's settings as a raw JSON value.
 463    ///
 464    /// For user-facing functionality use the typed setting interface.
 465    /// (e.g. ProjectSettings::get_global(cx))
 466    pub fn raw_user_settings(&self) -> &Value {
 467        &self.raw_user_settings
 468    }
 469
 470    /// Get the configured settings profile names.
 471    pub fn configured_settings_profiles(&self) -> impl Iterator<Item = &str> {
 472        self.raw_user_settings
 473            .get("profiles")
 474            .and_then(|v| v.as_object())
 475            .into_iter()
 476            .flat_map(|obj| obj.keys())
 477            .map(|s| s.as_str())
 478    }
 479
 480    /// Access the raw JSON value of the global settings.
 481    pub fn raw_global_settings(&self) -> Option<&Value> {
 482        self.raw_global_settings.as_ref()
 483    }
 484
 485    /// Access the raw JSON value of the default settings.
 486    pub fn raw_default_settings(&self) -> &Value {
 487        &self.raw_default_settings
 488    }
 489
 490    #[cfg(any(test, feature = "test-support"))]
 491    pub fn test(cx: &mut App) -> Self {
 492        let mut this = Self::new(cx);
 493        this.set_default_settings(&crate::test_settings(), cx)
 494            .unwrap();
 495        this.set_user_settings("{}", cx).unwrap();
 496        this
 497    }
 498
 499    /// Updates the value of a setting in the user's global configuration.
 500    ///
 501    /// This is only for tests. Normally, settings are only loaded from
 502    /// JSON files.
 503    #[cfg(any(test, feature = "test-support"))]
 504    pub fn update_user_settings<T: Settings>(
 505        &mut self,
 506        cx: &mut App,
 507        update: impl FnOnce(&mut T::FileContent),
 508    ) {
 509        let old_text = serde_json::to_string(&self.raw_user_settings).unwrap();
 510        let new_text = self.new_text_for_update::<T>(old_text, update);
 511        self.set_user_settings(&new_text, cx).unwrap();
 512    }
 513
 514    pub async fn load_settings(fs: &Arc<dyn Fs>) -> Result<String> {
 515        match fs.load(paths::settings_file()).await {
 516            result @ Ok(_) => result,
 517            Err(err) => {
 518                if let Some(e) = err.downcast_ref::<std::io::Error>()
 519                    && e.kind() == std::io::ErrorKind::NotFound
 520                {
 521                    return Ok(crate::initial_user_settings_content().to_string());
 522                }
 523                Err(err)
 524            }
 525        }
 526    }
 527
 528    pub async fn load_global_settings(fs: &Arc<dyn Fs>) -> Result<String> {
 529        match fs.load(paths::global_settings_file()).await {
 530            result @ Ok(_) => result,
 531            Err(err) => {
 532                if let Some(e) = err.downcast_ref::<std::io::Error>()
 533                    && e.kind() == std::io::ErrorKind::NotFound
 534                {
 535                    return Ok("{}".to_string());
 536                }
 537                Err(err)
 538            }
 539        }
 540    }
 541
 542    fn update_settings_file_inner(
 543        &self,
 544        fs: Arc<dyn Fs>,
 545        update: impl 'static + Send + FnOnce(String, AsyncApp) -> Result<String>,
 546    ) -> oneshot::Receiver<Result<()>> {
 547        let (tx, rx) = oneshot::channel::<Result<()>>();
 548        self.setting_file_updates_tx
 549            .unbounded_send(Box::new(move |cx: AsyncApp| {
 550                async move {
 551                    let res = async move {
 552                        let old_text = Self::load_settings(&fs).await?;
 553                        let new_text = update(old_text, cx)?;
 554                        let settings_path = paths::settings_file().as_path();
 555                        if fs.is_file(settings_path).await {
 556                            let resolved_path =
 557                                fs.canonicalize(settings_path).await.with_context(|| {
 558                                    format!(
 559                                        "Failed to canonicalize settings path {:?}",
 560                                        settings_path
 561                                    )
 562                                })?;
 563
 564                            fs.atomic_write(resolved_path.clone(), new_text)
 565                                .await
 566                                .with_context(|| {
 567                                    format!("Failed to write settings to file {:?}", resolved_path)
 568                                })?;
 569                        } else {
 570                            fs.atomic_write(settings_path.to_path_buf(), new_text)
 571                                .await
 572                                .with_context(|| {
 573                                    format!("Failed to write settings to file {:?}", settings_path)
 574                                })?;
 575                        }
 576                        anyhow::Ok(())
 577                    }
 578                    .await;
 579
 580                    let new_res = match &res {
 581                        Ok(_) => anyhow::Ok(()),
 582                        Err(e) => Err(anyhow::anyhow!("Failed to write settings to file {:?}", e)),
 583                    };
 584
 585                    _ = tx.send(new_res);
 586                    res
 587                }
 588                .boxed_local()
 589            }))
 590            .map_err(|err| anyhow::format_err!("Failed to update settings file: {}", err))
 591            .log_with_level(log::Level::Warn);
 592        return rx;
 593    }
 594
 595    pub fn update_settings_file_at_path(
 596        &self,
 597        fs: Arc<dyn Fs>,
 598        path: &[&str],
 599        new_value: serde_json::Value,
 600    ) -> oneshot::Receiver<Result<()>> {
 601        let key_path = path
 602            .into_iter()
 603            .cloned()
 604            .map(SharedString::new)
 605            .collect::<Vec<_>>();
 606        let update = move |mut old_text: String, cx: AsyncApp| {
 607            cx.read_global(|store: &SettingsStore, _cx| {
 608                // todo(settings_ui) use `update_value_in_json_text` for merging new and old objects with comment preservation, needs old value though...
 609                let (range, replacement) = replace_value_in_json_text(
 610                    &old_text,
 611                    key_path.as_slice(),
 612                    store.json_tab_size(),
 613                    Some(&new_value),
 614                    None,
 615                );
 616                old_text.replace_range(range, &replacement);
 617                old_text
 618            })
 619        };
 620        self.update_settings_file_inner(fs, update)
 621    }
 622
 623    pub fn update_settings_file<T: Settings>(
 624        &self,
 625        fs: Arc<dyn Fs>,
 626        update: impl 'static + Send + FnOnce(&mut T::FileContent, &App),
 627    ) {
 628        _ = self.update_settings_file_inner(fs, move |old_text: String, cx: AsyncApp| {
 629            cx.read_global(|store: &SettingsStore, cx| {
 630                store.new_text_for_update::<T>(old_text, |content| update(content, cx))
 631            })
 632        });
 633    }
 634
 635    pub fn import_vscode_settings(
 636        &self,
 637        fs: Arc<dyn Fs>,
 638        vscode_settings: VsCodeSettings,
 639    ) -> oneshot::Receiver<Result<()>> {
 640        self.update_settings_file_inner(fs, move |old_text: String, cx: AsyncApp| {
 641            cx.read_global(|store: &SettingsStore, _cx| {
 642                store.get_vscode_edits(old_text, &vscode_settings)
 643            })
 644        })
 645    }
 646
 647    pub fn settings_ui_items(&self) -> impl IntoIterator<Item = SettingsUiEntry> {
 648        self.setting_values
 649            .values()
 650            .map(|item| item.settings_ui_item())
 651    }
 652}
 653
 654impl SettingsStore {
 655    /// Updates the value of a setting in a JSON file, returning the new text
 656    /// for that JSON file.
 657    pub fn new_text_for_update<T: Settings>(
 658        &self,
 659        old_text: String,
 660        update: impl FnOnce(&mut T::FileContent),
 661    ) -> String {
 662        let edits = self.edits_for_update::<T>(&old_text, update);
 663        let mut new_text = old_text;
 664        for (range, replacement) in edits.into_iter() {
 665            new_text.replace_range(range, &replacement);
 666        }
 667        new_text
 668    }
 669
 670    pub fn get_vscode_edits(&self, mut old_text: String, vscode: &VsCodeSettings) -> String {
 671        let mut new_text = old_text.clone();
 672        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
 673        let raw_settings = parse_json_with_comments::<Value>(&old_text).unwrap_or_default();
 674        let tab_size = self.json_tab_size();
 675        for v in self.setting_values.values() {
 676            v.edits_for_update(&raw_settings, tab_size, vscode, &mut old_text, &mut edits);
 677        }
 678        for (range, replacement) in edits.into_iter() {
 679            new_text.replace_range(range, &replacement);
 680        }
 681        new_text
 682    }
 683
 684    /// Updates the value of a setting in a JSON file, returning a list
 685    /// of edits to apply to the JSON file.
 686    pub fn edits_for_update<T: Settings>(
 687        &self,
 688        text: &str,
 689        update: impl FnOnce(&mut T::FileContent),
 690    ) -> Vec<(Range<usize>, String)> {
 691        let setting_type_id = TypeId::of::<T>();
 692
 693        let preserved_keys = T::PRESERVED_KEYS.unwrap_or_default();
 694
 695        let setting = self
 696            .setting_values
 697            .get(&setting_type_id)
 698            .unwrap_or_else(|| panic!("unregistered setting type {}", type_name::<T>()));
 699        let raw_settings = parse_json_with_comments::<Value>(text).unwrap_or_default();
 700        let (key, deserialized_setting) = setting.deserialize_setting_with_key(&raw_settings);
 701        let old_content = match deserialized_setting {
 702            Ok(content) => content.0.downcast::<T::FileContent>().unwrap(),
 703            Err(_) => Box::<<T as Settings>::FileContent>::default(),
 704        };
 705        let mut new_content = old_content.clone();
 706        update(&mut new_content);
 707
 708        let old_value = serde_json::to_value(&old_content).unwrap();
 709        let new_value = serde_json::to_value(new_content).unwrap();
 710
 711        let mut key_path = Vec::new();
 712        if let Some(key) = key {
 713            key_path.push(key);
 714        }
 715
 716        let mut edits = Vec::new();
 717        let tab_size = self.json_tab_size();
 718        let mut text = text.to_string();
 719        update_value_in_json_text(
 720            &mut text,
 721            &mut key_path,
 722            tab_size,
 723            &old_value,
 724            &new_value,
 725            preserved_keys,
 726            &mut edits,
 727        );
 728        edits
 729    }
 730
 731    /// Configure the tab sized when updating JSON files.
 732    pub fn set_json_tab_size_callback<T: Settings>(
 733        &mut self,
 734        get_tab_size: fn(&T) -> Option<usize>,
 735    ) {
 736        self.tab_size_callback = Some((
 737            TypeId::of::<T>(),
 738            Box::new(move |value| get_tab_size(value.downcast_ref::<T>().unwrap())),
 739        ));
 740    }
 741
 742    pub fn json_tab_size(&self) -> usize {
 743        const DEFAULT_JSON_TAB_SIZE: usize = 2;
 744
 745        if let Some((setting_type_id, callback)) = &self.tab_size_callback {
 746            let setting_value = self.setting_values.get(setting_type_id).unwrap();
 747            let value = setting_value.value_for_path(None);
 748            if let Some(value) = callback(value) {
 749                return value;
 750            }
 751        }
 752
 753        DEFAULT_JSON_TAB_SIZE
 754    }
 755
 756    /// Sets the default settings via a JSON string.
 757    ///
 758    /// The string should contain a JSON object with a default value for every setting.
 759    pub fn set_default_settings(
 760        &mut self,
 761        default_settings_content: &str,
 762        cx: &mut App,
 763    ) -> Result<()> {
 764        let settings: Value = parse_json_with_comments(default_settings_content)?;
 765        anyhow::ensure!(settings.is_object(), "settings must be an object");
 766        self.raw_default_settings = settings;
 767        self.recompute_values(None, cx)?;
 768        Ok(())
 769    }
 770
 771    /// Sets the user settings via a JSON string.
 772    pub fn set_user_settings(
 773        &mut self,
 774        user_settings_content: &str,
 775        cx: &mut App,
 776    ) -> Result<Value> {
 777        let settings: Value = if user_settings_content.is_empty() {
 778            parse_json_with_comments("{}")?
 779        } else {
 780            parse_json_with_comments(user_settings_content)?
 781        };
 782
 783        anyhow::ensure!(settings.is_object(), "settings must be an object");
 784        self.raw_user_settings = settings.clone();
 785        self.recompute_values(None, cx)?;
 786        Ok(settings)
 787    }
 788
 789    /// Sets the global settings via a JSON string.
 790    pub fn set_global_settings(
 791        &mut self,
 792        global_settings_content: &str,
 793        cx: &mut App,
 794    ) -> Result<Value> {
 795        let settings: Value = if global_settings_content.is_empty() {
 796            parse_json_with_comments("{}")?
 797        } else {
 798            parse_json_with_comments(global_settings_content)?
 799        };
 800
 801        anyhow::ensure!(settings.is_object(), "settings must be an object");
 802        self.raw_global_settings = Some(settings.clone());
 803        self.recompute_values(None, cx)?;
 804        Ok(settings)
 805    }
 806
 807    pub fn set_server_settings(
 808        &mut self,
 809        server_settings_content: &str,
 810        cx: &mut App,
 811    ) -> Result<()> {
 812        let settings: Option<Value> = if server_settings_content.is_empty() {
 813            None
 814        } else {
 815            parse_json_with_comments(server_settings_content)?
 816        };
 817
 818        anyhow::ensure!(
 819            settings
 820                .as_ref()
 821                .map(|value| value.is_object())
 822                .unwrap_or(true),
 823            "settings must be an object"
 824        );
 825        self.raw_server_settings = settings;
 826        self.recompute_values(None, cx)?;
 827        Ok(())
 828    }
 829
 830    /// Add or remove a set of local settings via a JSON string.
 831    pub fn set_local_settings(
 832        &mut self,
 833        root_id: WorktreeId,
 834        directory_path: Arc<Path>,
 835        kind: LocalSettingsKind,
 836        settings_content: Option<&str>,
 837        cx: &mut App,
 838    ) -> std::result::Result<(), InvalidSettingsError> {
 839        let mut zed_settings_changed = false;
 840        match (
 841            kind,
 842            settings_content
 843                .map(|content| content.trim())
 844                .filter(|content| !content.is_empty()),
 845        ) {
 846            (LocalSettingsKind::Tasks, _) => {
 847                return Err(InvalidSettingsError::Tasks {
 848                    message: "Attempted to submit tasks into the settings store".to_string(),
 849                    path: directory_path.join(task_file_name()),
 850                });
 851            }
 852            (LocalSettingsKind::Debug, _) => {
 853                return Err(InvalidSettingsError::Debug {
 854                    message: "Attempted to submit debugger config into the settings store"
 855                        .to_string(),
 856                    path: directory_path.join(task_file_name()),
 857                });
 858            }
 859            (LocalSettingsKind::Settings, None) => {
 860                zed_settings_changed = self
 861                    .raw_local_settings
 862                    .remove(&(root_id, directory_path.clone()))
 863                    .is_some()
 864            }
 865            (LocalSettingsKind::Editorconfig, None) => {
 866                self.raw_editorconfig_settings
 867                    .remove(&(root_id, directory_path.clone()));
 868            }
 869            (LocalSettingsKind::Settings, Some(settings_contents)) => {
 870                let new_settings =
 871                    parse_json_with_comments::<Value>(settings_contents).map_err(|e| {
 872                        InvalidSettingsError::LocalSettings {
 873                            path: directory_path.join(local_settings_file_relative_path()),
 874                            message: e.to_string(),
 875                        }
 876                    })?;
 877                match self
 878                    .raw_local_settings
 879                    .entry((root_id, directory_path.clone()))
 880                {
 881                    btree_map::Entry::Vacant(v) => {
 882                        v.insert(new_settings);
 883                        zed_settings_changed = true;
 884                    }
 885                    btree_map::Entry::Occupied(mut o) => {
 886                        if o.get() != &new_settings {
 887                            o.insert(new_settings);
 888                            zed_settings_changed = true;
 889                        }
 890                    }
 891                }
 892            }
 893            (LocalSettingsKind::Editorconfig, Some(editorconfig_contents)) => {
 894                match self
 895                    .raw_editorconfig_settings
 896                    .entry((root_id, directory_path.clone()))
 897                {
 898                    btree_map::Entry::Vacant(v) => match editorconfig_contents.parse() {
 899                        Ok(new_contents) => {
 900                            v.insert((editorconfig_contents.to_owned(), Some(new_contents)));
 901                        }
 902                        Err(e) => {
 903                            v.insert((editorconfig_contents.to_owned(), None));
 904                            return Err(InvalidSettingsError::Editorconfig {
 905                                message: e.to_string(),
 906                                path: directory_path.join(EDITORCONFIG_NAME),
 907                            });
 908                        }
 909                    },
 910                    btree_map::Entry::Occupied(mut o) => {
 911                        if o.get().0 != editorconfig_contents {
 912                            match editorconfig_contents.parse() {
 913                                Ok(new_contents) => {
 914                                    o.insert((
 915                                        editorconfig_contents.to_owned(),
 916                                        Some(new_contents),
 917                                    ));
 918                                }
 919                                Err(e) => {
 920                                    o.insert((editorconfig_contents.to_owned(), None));
 921                                    return Err(InvalidSettingsError::Editorconfig {
 922                                        message: e.to_string(),
 923                                        path: directory_path.join(EDITORCONFIG_NAME),
 924                                    });
 925                                }
 926                            }
 927                        }
 928                    }
 929                }
 930            }
 931        };
 932
 933        if zed_settings_changed {
 934            self.recompute_values(Some((root_id, &directory_path)), cx)?;
 935        }
 936        Ok(())
 937    }
 938
 939    pub fn set_extension_settings<T: Serialize>(&mut self, content: T, cx: &mut App) -> Result<()> {
 940        let settings: Value = serde_json::to_value(content)?;
 941        anyhow::ensure!(settings.is_object(), "settings must be an object");
 942        self.raw_extension_settings = settings;
 943        self.recompute_values(None, cx)?;
 944        Ok(())
 945    }
 946
 947    /// Add or remove a set of local settings via a JSON string.
 948    pub fn clear_local_settings(&mut self, root_id: WorktreeId, cx: &mut App) -> Result<()> {
 949        self.raw_local_settings
 950            .retain(|(worktree_id, _), _| worktree_id != &root_id);
 951        self.recompute_values(Some((root_id, "".as_ref())), cx)?;
 952        Ok(())
 953    }
 954
 955    pub fn local_settings(
 956        &self,
 957        root_id: WorktreeId,
 958    ) -> impl '_ + Iterator<Item = (Arc<Path>, String)> {
 959        self.raw_local_settings
 960            .range(
 961                (root_id, Path::new("").into())
 962                    ..(
 963                        WorktreeId::from_usize(root_id.to_usize() + 1),
 964                        Path::new("").into(),
 965                    ),
 966            )
 967            .map(|((_, path), content)| (path.clone(), serde_json::to_string(content).unwrap()))
 968    }
 969
 970    pub fn local_editorconfig_settings(
 971        &self,
 972        root_id: WorktreeId,
 973    ) -> impl '_ + Iterator<Item = (Arc<Path>, String, Option<Editorconfig>)> {
 974        self.raw_editorconfig_settings
 975            .range(
 976                (root_id, Path::new("").into())
 977                    ..(
 978                        WorktreeId::from_usize(root_id.to_usize() + 1),
 979                        Path::new("").into(),
 980                    ),
 981            )
 982            .map(|((_, path), (content, parsed_content))| {
 983                (path.clone(), content.clone(), parsed_content.clone())
 984            })
 985    }
 986
 987    pub fn json_schema(&self, schema_params: &SettingsJsonSchemaParams, cx: &App) -> Value {
 988        let mut generator = schemars::generate::SchemaSettings::draft2019_09()
 989            .with_transform(DefaultDenyUnknownFields)
 990            .into_generator();
 991        let mut combined_schema = json!({
 992            "type": "object",
 993            "properties": {}
 994        });
 995
 996        // Merge together settings schemas, similarly to json schema's "allOf". This merging is
 997        // recursive, though at time of writing this recursive nature isn't used very much. An
 998        // example of it is the schema for `jupyter` having contribution from both `EditorSettings`
 999        // and `JupyterSettings`.
1000        //
1001        // This logic could be removed in favor of "allOf", but then there isn't the opportunity to
1002        // validate and fully control the merge.
1003        for setting_value in self.setting_values.values() {
1004            let mut setting_schema = setting_value.json_schema(&mut generator);
1005
1006            if let Some(key) = setting_value.key() {
1007                if let Some(properties) = combined_schema.get_mut("properties")
1008                    && let Some(properties_obj) = properties.as_object_mut()
1009                {
1010                    if let Some(target) = properties_obj.get_mut(key) {
1011                        merge_schema(target, setting_schema.to_value());
1012                    } else {
1013                        properties_obj.insert(key.to_string(), setting_schema.to_value());
1014                    }
1015                }
1016            } else {
1017                setting_schema.remove("description");
1018                setting_schema.remove("additionalProperties");
1019                merge_schema(&mut combined_schema, setting_schema.to_value());
1020            }
1021        }
1022
1023        fn merge_schema(target: &mut serde_json::Value, source: serde_json::Value) {
1024            let (Some(target_obj), serde_json::Value::Object(source_obj)) =
1025                (target.as_object_mut(), source)
1026            else {
1027                return;
1028            };
1029
1030            for (source_key, source_value) in source_obj {
1031                match source_key.as_str() {
1032                    "properties" => {
1033                        let serde_json::Value::Object(source_properties) = source_value else {
1034                            log::error!(
1035                                "bug: expected object for `{}` json schema field, but got: {}",
1036                                source_key,
1037                                source_value
1038                            );
1039                            continue;
1040                        };
1041                        let target_properties =
1042                            target_obj.entry(source_key.clone()).or_insert(json!({}));
1043                        let Some(target_properties) = target_properties.as_object_mut() else {
1044                            log::error!(
1045                                "bug: expected object for `{}` json schema field, but got: {}",
1046                                source_key,
1047                                target_properties
1048                            );
1049                            continue;
1050                        };
1051                        for (key, value) in source_properties {
1052                            if let Some(existing) = target_properties.get_mut(&key) {
1053                                merge_schema(existing, value);
1054                            } else {
1055                                target_properties.insert(key, value);
1056                            }
1057                        }
1058                    }
1059                    "allOf" | "anyOf" | "oneOf" => {
1060                        let serde_json::Value::Array(source_array) = source_value else {
1061                            log::error!(
1062                                "bug: expected array for `{}` json schema field, but got: {}",
1063                                source_key,
1064                                source_value,
1065                            );
1066                            continue;
1067                        };
1068                        let target_array =
1069                            target_obj.entry(source_key.clone()).or_insert(json!([]));
1070                        let Some(target_array) = target_array.as_array_mut() else {
1071                            log::error!(
1072                                "bug: expected array for `{}` json schema field, but got: {}",
1073                                source_key,
1074                                target_array,
1075                            );
1076                            continue;
1077                        };
1078                        target_array.extend(source_array);
1079                    }
1080                    "type"
1081                    | "$ref"
1082                    | "enum"
1083                    | "minimum"
1084                    | "maximum"
1085                    | "pattern"
1086                    | "description"
1087                    | "additionalProperties" => {
1088                        if let Some(old_value) =
1089                            target_obj.insert(source_key.clone(), source_value.clone())
1090                            && old_value != source_value
1091                        {
1092                            log::error!(
1093                                "bug: while merging JSON schemas, \
1094                                    mismatch `\"{}\": {}` (before was `{}`)",
1095                                source_key,
1096                                old_value,
1097                                source_value
1098                            );
1099                        }
1100                    }
1101                    _ => {
1102                        log::error!(
1103                            "bug: while merging settings JSON schemas, \
1104                            encountered unexpected `\"{}\": {}`",
1105                            source_key,
1106                            source_value
1107                        );
1108                    }
1109                }
1110            }
1111        }
1112
1113        // add schemas which are determined at runtime
1114        for parameterized_json_schema in inventory::iter::<ParameterizedJsonSchema>() {
1115            (parameterized_json_schema.add_and_get_ref)(&mut generator, schema_params, cx);
1116        }
1117
1118        // add merged settings schema to the definitions
1119        const ZED_SETTINGS: &str = "ZedSettings";
1120        let zed_settings_ref = add_new_subschema(&mut generator, ZED_SETTINGS, combined_schema);
1121
1122        // add `ZedSettingsOverride` which is the same as `ZedSettings` except that unknown
1123        // fields are rejected. This is used for release stage settings and profiles.
1124        let mut zed_settings_override = zed_settings_ref.clone();
1125        zed_settings_override.insert("unevaluatedProperties".to_string(), false.into());
1126        let zed_settings_override_ref = add_new_subschema(
1127            &mut generator,
1128            "ZedSettingsOverride",
1129            zed_settings_override.to_value(),
1130        );
1131
1132        // Remove `"additionalProperties": false` added by `DefaultDenyUnknownFields` so that
1133        // unknown fields can be handled by the root schema and `ZedSettingsOverride`.
1134        let mut definitions = generator.take_definitions(true);
1135        definitions
1136            .get_mut(ZED_SETTINGS)
1137            .unwrap()
1138            .as_object_mut()
1139            .unwrap()
1140            .remove("additionalProperties");
1141
1142        let meta_schema = generator
1143            .settings()
1144            .meta_schema
1145            .as_ref()
1146            .expect("meta_schema should be present in schemars settings")
1147            .to_string();
1148
1149        json!({
1150            "$schema": meta_schema,
1151            "title": "Zed Settings",
1152            "unevaluatedProperties": false,
1153            // ZedSettings + settings overrides for each release stage / OS / profiles
1154            "allOf": [
1155                zed_settings_ref,
1156                {
1157                    "properties": {
1158                        "dev": zed_settings_override_ref,
1159                        "nightly": zed_settings_override_ref,
1160                        "stable": zed_settings_override_ref,
1161                        "preview": zed_settings_override_ref,
1162                        "linux": zed_settings_override_ref,
1163                        "macos": zed_settings_override_ref,
1164                        "windows": zed_settings_override_ref,
1165                        "profiles": {
1166                            "type": "object",
1167                            "description": "Configures any number of settings profiles.",
1168                            "additionalProperties": zed_settings_override_ref
1169                        }
1170                    }
1171                }
1172            ],
1173            "$defs": definitions,
1174        })
1175    }
1176
1177    fn recompute_values(
1178        &mut self,
1179        changed_local_path: Option<(WorktreeId, &Path)>,
1180        cx: &mut App,
1181    ) -> std::result::Result<(), InvalidSettingsError> {
1182        // Reload the global and local values for every setting.
1183        let mut project_settings_stack = Vec::<DeserializedSetting>::new();
1184        let mut paths_stack = Vec::<Option<(WorktreeId, &Path)>>::new();
1185        for setting_value in self.setting_values.values_mut() {
1186            let default_settings = setting_value
1187                .deserialize_setting(&self.raw_default_settings)
1188                .map_err(|e| InvalidSettingsError::DefaultSettings {
1189                    message: e.to_string(),
1190                })?;
1191
1192            let global_settings = self
1193                .raw_global_settings
1194                .as_ref()
1195                .and_then(|setting| setting_value.deserialize_setting(setting).log_err());
1196
1197            let extension_settings = setting_value
1198                .deserialize_setting(&self.raw_extension_settings)
1199                .log_err();
1200
1201            let user_settings = match setting_value.deserialize_setting(&self.raw_user_settings) {
1202                Ok(settings) => Some(settings),
1203                Err(error) => {
1204                    return Err(InvalidSettingsError::UserSettings {
1205                        message: error.to_string(),
1206                    });
1207                }
1208            };
1209
1210            let server_settings = self
1211                .raw_server_settings
1212                .as_ref()
1213                .and_then(|setting| setting_value.deserialize_setting(setting).log_err());
1214
1215            let mut release_channel_settings = None;
1216            if let Some(release_settings) = &self
1217                .raw_user_settings
1218                .get(release_channel::RELEASE_CHANNEL.dev_name())
1219                && let Some(release_settings) = setting_value
1220                    .deserialize_setting(release_settings)
1221                    .log_err()
1222            {
1223                release_channel_settings = Some(release_settings);
1224            }
1225
1226            let mut os_settings = None;
1227            if let Some(settings) = &self.raw_user_settings.get(env::consts::OS)
1228                && let Some(settings) = setting_value.deserialize_setting(settings).log_err()
1229            {
1230                os_settings = Some(settings);
1231            }
1232
1233            let mut profile_settings = None;
1234            if let Some(active_profile) = cx.try_global::<ActiveSettingsProfileName>()
1235                && let Some(profiles) = self.raw_user_settings.get("profiles")
1236                && let Some(profile_json) = profiles.get(&active_profile.0)
1237            {
1238                profile_settings = setting_value.deserialize_setting(profile_json).log_err();
1239            }
1240
1241            // If the global settings file changed, reload the global value for the field.
1242            if changed_local_path.is_none()
1243                && let Some(value) = setting_value
1244                    .load_setting(
1245                        SettingsSources {
1246                            default: &default_settings,
1247                            global: global_settings.as_ref(),
1248                            extensions: extension_settings.as_ref(),
1249                            user: user_settings.as_ref(),
1250                            release_channel: release_channel_settings.as_ref(),
1251                            operating_system: os_settings.as_ref(),
1252                            profile: profile_settings.as_ref(),
1253                            server: server_settings.as_ref(),
1254                            project: &[],
1255                        },
1256                        cx,
1257                    )
1258                    .log_err()
1259            {
1260                setting_value.set_global_value(value);
1261            }
1262
1263            // Reload the local values for the setting.
1264            paths_stack.clear();
1265            project_settings_stack.clear();
1266            for ((root_id, directory_path), local_settings) in &self.raw_local_settings {
1267                // Build a stack of all of the local values for that setting.
1268                while let Some(prev_entry) = paths_stack.last() {
1269                    if let Some((prev_root_id, prev_path)) = prev_entry
1270                        && (root_id != prev_root_id || !directory_path.starts_with(prev_path))
1271                    {
1272                        paths_stack.pop();
1273                        project_settings_stack.pop();
1274                        continue;
1275                    }
1276                    break;
1277                }
1278
1279                match setting_value.deserialize_setting(local_settings) {
1280                    Ok(local_settings) => {
1281                        paths_stack.push(Some((*root_id, directory_path.as_ref())));
1282                        project_settings_stack.push(local_settings);
1283
1284                        // If a local settings file changed, then avoid recomputing local
1285                        // settings for any path outside of that directory.
1286                        if changed_local_path.is_some_and(
1287                            |(changed_root_id, changed_local_path)| {
1288                                *root_id != changed_root_id
1289                                    || !directory_path.starts_with(changed_local_path)
1290                            },
1291                        ) {
1292                            continue;
1293                        }
1294
1295                        if let Some(value) = setting_value
1296                            .load_setting(
1297                                SettingsSources {
1298                                    default: &default_settings,
1299                                    global: global_settings.as_ref(),
1300                                    extensions: extension_settings.as_ref(),
1301                                    user: user_settings.as_ref(),
1302                                    release_channel: release_channel_settings.as_ref(),
1303                                    operating_system: os_settings.as_ref(),
1304                                    profile: profile_settings.as_ref(),
1305                                    server: server_settings.as_ref(),
1306                                    project: &project_settings_stack.iter().collect::<Vec<_>>(),
1307                                },
1308                                cx,
1309                            )
1310                            .log_err()
1311                        {
1312                            setting_value.set_local_value(*root_id, directory_path.clone(), value);
1313                        }
1314                    }
1315                    Err(error) => {
1316                        return Err(InvalidSettingsError::LocalSettings {
1317                            path: directory_path.join(local_settings_file_relative_path()),
1318                            message: error.to_string(),
1319                        });
1320                    }
1321                }
1322            }
1323        }
1324        Ok(())
1325    }
1326
1327    pub fn editorconfig_properties(
1328        &self,
1329        for_worktree: WorktreeId,
1330        for_path: &Path,
1331    ) -> Option<EditorconfigProperties> {
1332        let mut properties = EditorconfigProperties::new();
1333
1334        for (directory_with_config, _, parsed_editorconfig) in
1335            self.local_editorconfig_settings(for_worktree)
1336        {
1337            if !for_path.starts_with(&directory_with_config) {
1338                properties.use_fallbacks();
1339                return Some(properties);
1340            }
1341            let parsed_editorconfig = parsed_editorconfig?;
1342            if parsed_editorconfig.is_root {
1343                properties = EditorconfigProperties::new();
1344            }
1345            for section in parsed_editorconfig.sections {
1346                section.apply_to(&mut properties, for_path).log_err()?;
1347            }
1348        }
1349
1350        properties.use_fallbacks();
1351        Some(properties)
1352    }
1353}
1354
1355#[derive(Debug, Clone, PartialEq)]
1356pub enum InvalidSettingsError {
1357    LocalSettings { path: PathBuf, message: String },
1358    UserSettings { message: String },
1359    ServerSettings { message: String },
1360    DefaultSettings { message: String },
1361    Editorconfig { path: PathBuf, message: String },
1362    Tasks { path: PathBuf, message: String },
1363    Debug { path: PathBuf, message: String },
1364}
1365
1366impl std::fmt::Display for InvalidSettingsError {
1367    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1368        match self {
1369            InvalidSettingsError::LocalSettings { message, .. }
1370            | InvalidSettingsError::UserSettings { message }
1371            | InvalidSettingsError::ServerSettings { message }
1372            | InvalidSettingsError::DefaultSettings { message }
1373            | InvalidSettingsError::Tasks { message, .. }
1374            | InvalidSettingsError::Editorconfig { message, .. }
1375            | InvalidSettingsError::Debug { message, .. } => {
1376                write!(f, "{message}")
1377            }
1378        }
1379    }
1380}
1381impl std::error::Error for InvalidSettingsError {}
1382
1383impl Debug for SettingsStore {
1384    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1385        f.debug_struct("SettingsStore")
1386            .field(
1387                "types",
1388                &self
1389                    .setting_values
1390                    .values()
1391                    .map(|value| value.setting_type_name())
1392                    .collect::<Vec<_>>(),
1393            )
1394            .field("default_settings", &self.raw_default_settings)
1395            .field("user_settings", &self.raw_user_settings)
1396            .field("local_settings", &self.raw_local_settings)
1397            .finish_non_exhaustive()
1398    }
1399}
1400
1401impl<T: Settings> AnySettingValue for SettingValue<T> {
1402    fn key(&self) -> Option<&'static str> {
1403        T::KEY
1404    }
1405
1406    fn setting_type_name(&self) -> &'static str {
1407        type_name::<T>()
1408    }
1409
1410    fn load_setting(
1411        &self,
1412        values: SettingsSources<DeserializedSetting>,
1413        cx: &mut App,
1414    ) -> Result<Box<dyn Any>> {
1415        Ok(Box::new(T::load(
1416            SettingsSources {
1417                default: values.default.0.downcast_ref::<T::FileContent>().unwrap(),
1418                global: values
1419                    .global
1420                    .map(|value| value.0.downcast_ref::<T::FileContent>().unwrap()),
1421                extensions: values
1422                    .extensions
1423                    .map(|value| value.0.downcast_ref::<T::FileContent>().unwrap()),
1424                user: values
1425                    .user
1426                    .map(|value| value.0.downcast_ref::<T::FileContent>().unwrap()),
1427                release_channel: values
1428                    .release_channel
1429                    .map(|value| value.0.downcast_ref::<T::FileContent>().unwrap()),
1430                operating_system: values
1431                    .operating_system
1432                    .map(|value| value.0.downcast_ref::<T::FileContent>().unwrap()),
1433                profile: values
1434                    .profile
1435                    .map(|value| value.0.downcast_ref::<T::FileContent>().unwrap()),
1436                server: values
1437                    .server
1438                    .map(|value| value.0.downcast_ref::<T::FileContent>().unwrap()),
1439                project: values
1440                    .project
1441                    .iter()
1442                    .map(|value| value.0.downcast_ref().unwrap())
1443                    .collect::<SmallVec<[_; 3]>>()
1444                    .as_slice(),
1445            },
1446            cx,
1447        )?))
1448    }
1449
1450    fn deserialize_setting_with_key(
1451        &self,
1452        mut json: &Value,
1453    ) -> (Option<&'static str>, Result<DeserializedSetting>) {
1454        let mut key = None;
1455        if let Some(k) = T::KEY {
1456            if let Some(value) = json.get(k) {
1457                json = value;
1458                key = Some(k);
1459            } else if let Some((k, value)) = T::FALLBACK_KEY.and_then(|k| Some((k, json.get(k)?))) {
1460                json = value;
1461                key = Some(k);
1462            } else {
1463                let value = T::FileContent::default();
1464                return (T::KEY, Ok(DeserializedSetting(Box::new(value))));
1465            }
1466        }
1467        let value = T::FileContent::deserialize(json)
1468            .map(|value| DeserializedSetting(Box::new(value)))
1469            .map_err(anyhow::Error::from);
1470        (key, value)
1471    }
1472
1473    fn all_local_values(&self) -> Vec<(WorktreeId, Arc<Path>, &dyn Any)> {
1474        self.local_values
1475            .iter()
1476            .map(|(id, path, value)| (*id, path.clone(), value as _))
1477            .collect()
1478    }
1479
1480    fn value_for_path(&self, path: Option<SettingsLocation>) -> &dyn Any {
1481        if let Some(SettingsLocation { worktree_id, path }) = path {
1482            for (settings_root_id, settings_path, value) in self.local_values.iter().rev() {
1483                if worktree_id == *settings_root_id && path.starts_with(settings_path) {
1484                    return value;
1485                }
1486            }
1487        }
1488        self.global_value
1489            .as_ref()
1490            .unwrap_or_else(|| panic!("no default value for setting {}", self.setting_type_name()))
1491    }
1492
1493    fn set_global_value(&mut self, value: Box<dyn Any>) {
1494        self.global_value = Some(*value.downcast().unwrap());
1495    }
1496
1497    fn set_local_value(&mut self, root_id: WorktreeId, path: Arc<Path>, value: Box<dyn Any>) {
1498        let value = *value.downcast().unwrap();
1499        match self
1500            .local_values
1501            .binary_search_by_key(&(root_id, &path), |e| (e.0, &e.1))
1502        {
1503            Ok(ix) => self.local_values[ix].2 = value,
1504            Err(ix) => self.local_values.insert(ix, (root_id, path, value)),
1505        }
1506    }
1507
1508    fn json_schema(&self, generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
1509        T::FileContent::json_schema(generator)
1510    }
1511
1512    fn edits_for_update(
1513        &self,
1514        raw_settings: &serde_json::Value,
1515        tab_size: usize,
1516        vscode_settings: &VsCodeSettings,
1517        text: &mut String,
1518        edits: &mut Vec<(Range<usize>, String)>,
1519    ) {
1520        let (key, deserialized_setting) = self.deserialize_setting_with_key(raw_settings);
1521        let old_content = match deserialized_setting {
1522            Ok(content) => content.0.downcast::<T::FileContent>().unwrap(),
1523            Err(_) => Box::<<T as Settings>::FileContent>::default(),
1524        };
1525        let mut new_content = old_content.clone();
1526        T::import_from_vscode(vscode_settings, &mut new_content);
1527
1528        let old_value = serde_json::to_value(&old_content).unwrap();
1529        let new_value = serde_json::to_value(new_content).unwrap();
1530
1531        let mut key_path = Vec::new();
1532        if let Some(key) = key {
1533            key_path.push(key);
1534        }
1535
1536        update_value_in_json_text(
1537            text,
1538            &mut key_path,
1539            tab_size,
1540            &old_value,
1541            &new_value,
1542            T::PRESERVED_KEYS.unwrap_or_default(),
1543            edits,
1544        );
1545    }
1546
1547    fn settings_ui_item(&self) -> SettingsUiEntry {
1548        <T as SettingsUi>::settings_ui_entry()
1549    }
1550}
1551
1552#[cfg(test)]
1553mod tests {
1554    use crate::VsCodeSettingsSource;
1555
1556    use super::*;
1557    // This is so the SettingsUi macro can still work properly
1558    use crate as settings;
1559    use serde_derive::Deserialize;
1560    use settings_ui_macros::SettingsUi;
1561    use unindent::Unindent;
1562
1563    #[gpui::test]
1564    fn test_settings_store_basic(cx: &mut App) {
1565        let mut store = SettingsStore::new(cx);
1566        store.register_setting::<UserSettings>(cx);
1567        store.register_setting::<TurboSetting>(cx);
1568        store.register_setting::<MultiKeySettings>(cx);
1569        store
1570            .set_default_settings(
1571                r#"{
1572                    "turbo": false,
1573                    "user": {
1574                        "name": "John Doe",
1575                        "age": 30,
1576                        "staff": false
1577                    }
1578                }"#,
1579                cx,
1580            )
1581            .unwrap();
1582
1583        assert_eq!(store.get::<TurboSetting>(None), &TurboSetting(false));
1584        assert_eq!(
1585            store.get::<UserSettings>(None),
1586            &UserSettings {
1587                name: "John Doe".to_string(),
1588                age: 30,
1589                staff: false,
1590            }
1591        );
1592        assert_eq!(
1593            store.get::<MultiKeySettings>(None),
1594            &MultiKeySettings {
1595                key1: String::new(),
1596                key2: String::new(),
1597            }
1598        );
1599
1600        store
1601            .set_user_settings(
1602                r#"{
1603                    "turbo": true,
1604                    "user": { "age": 31 },
1605                    "key1": "a"
1606                }"#,
1607                cx,
1608            )
1609            .unwrap();
1610
1611        assert_eq!(store.get::<TurboSetting>(None), &TurboSetting(true));
1612        assert_eq!(
1613            store.get::<UserSettings>(None),
1614            &UserSettings {
1615                name: "John Doe".to_string(),
1616                age: 31,
1617                staff: false
1618            }
1619        );
1620
1621        store
1622            .set_local_settings(
1623                WorktreeId::from_usize(1),
1624                Path::new("/root1").into(),
1625                LocalSettingsKind::Settings,
1626                Some(r#"{ "user": { "staff": true } }"#),
1627                cx,
1628            )
1629            .unwrap();
1630        store
1631            .set_local_settings(
1632                WorktreeId::from_usize(1),
1633                Path::new("/root1/subdir").into(),
1634                LocalSettingsKind::Settings,
1635                Some(r#"{ "user": { "name": "Jane Doe" } }"#),
1636                cx,
1637            )
1638            .unwrap();
1639
1640        store
1641            .set_local_settings(
1642                WorktreeId::from_usize(1),
1643                Path::new("/root2").into(),
1644                LocalSettingsKind::Settings,
1645                Some(r#"{ "user": { "age": 42 }, "key2": "b" }"#),
1646                cx,
1647            )
1648            .unwrap();
1649
1650        assert_eq!(
1651            store.get::<UserSettings>(Some(SettingsLocation {
1652                worktree_id: WorktreeId::from_usize(1),
1653                path: Path::new("/root1/something"),
1654            })),
1655            &UserSettings {
1656                name: "John Doe".to_string(),
1657                age: 31,
1658                staff: true
1659            }
1660        );
1661        assert_eq!(
1662            store.get::<UserSettings>(Some(SettingsLocation {
1663                worktree_id: WorktreeId::from_usize(1),
1664                path: Path::new("/root1/subdir/something")
1665            })),
1666            &UserSettings {
1667                name: "Jane Doe".to_string(),
1668                age: 31,
1669                staff: true
1670            }
1671        );
1672        assert_eq!(
1673            store.get::<UserSettings>(Some(SettingsLocation {
1674                worktree_id: WorktreeId::from_usize(1),
1675                path: Path::new("/root2/something")
1676            })),
1677            &UserSettings {
1678                name: "John Doe".to_string(),
1679                age: 42,
1680                staff: false
1681            }
1682        );
1683        assert_eq!(
1684            store.get::<MultiKeySettings>(Some(SettingsLocation {
1685                worktree_id: WorktreeId::from_usize(1),
1686                path: Path::new("/root2/something")
1687            })),
1688            &MultiKeySettings {
1689                key1: "a".to_string(),
1690                key2: "b".to_string(),
1691            }
1692        );
1693    }
1694
1695    #[gpui::test]
1696    fn test_setting_store_assign_json_before_register(cx: &mut App) {
1697        let mut store = SettingsStore::new(cx);
1698        store
1699            .set_default_settings(
1700                r#"{
1701                    "turbo": true,
1702                    "user": {
1703                        "name": "John Doe",
1704                        "age": 30,
1705                        "staff": false
1706                    },
1707                    "key1": "x"
1708                }"#,
1709                cx,
1710            )
1711            .unwrap();
1712        store
1713            .set_user_settings(r#"{ "turbo": false }"#, cx)
1714            .unwrap();
1715        store.register_setting::<UserSettings>(cx);
1716        store.register_setting::<TurboSetting>(cx);
1717
1718        assert_eq!(store.get::<TurboSetting>(None), &TurboSetting(false));
1719        assert_eq!(
1720            store.get::<UserSettings>(None),
1721            &UserSettings {
1722                name: "John Doe".to_string(),
1723                age: 30,
1724                staff: false,
1725            }
1726        );
1727
1728        store.register_setting::<MultiKeySettings>(cx);
1729        assert_eq!(
1730            store.get::<MultiKeySettings>(None),
1731            &MultiKeySettings {
1732                key1: "x".into(),
1733                key2: String::new(),
1734            }
1735        );
1736    }
1737
1738    fn check_settings_update<T: Settings>(
1739        store: &mut SettingsStore,
1740        old_json: String,
1741        update: fn(&mut T::FileContent),
1742        expected_new_json: String,
1743        cx: &mut App,
1744    ) {
1745        store.set_user_settings(&old_json, cx).ok();
1746        let edits = store.edits_for_update::<T>(&old_json, update);
1747        let mut new_json = old_json;
1748        for (range, replacement) in edits.into_iter() {
1749            new_json.replace_range(range, &replacement);
1750        }
1751        pretty_assertions::assert_eq!(new_json, expected_new_json);
1752    }
1753
1754    #[gpui::test]
1755    fn test_setting_store_update(cx: &mut App) {
1756        let mut store = SettingsStore::new(cx);
1757        store.register_setting::<MultiKeySettings>(cx);
1758        store.register_setting::<UserSettings>(cx);
1759        store.register_setting::<LanguageSettings>(cx);
1760
1761        // entries added and updated
1762        check_settings_update::<LanguageSettings>(
1763            &mut store,
1764            r#"{
1765                "languages": {
1766                    "JSON": {
1767                        "language_setting_1": true
1768                    }
1769                }
1770            }"#
1771            .unindent(),
1772            |settings| {
1773                settings
1774                    .languages
1775                    .get_mut("JSON")
1776                    .unwrap()
1777                    .language_setting_1 = Some(false);
1778                settings.languages.insert(
1779                    "Rust".into(),
1780                    LanguageSettingEntry {
1781                        language_setting_2: Some(true),
1782                        ..Default::default()
1783                    },
1784                );
1785            },
1786            r#"{
1787                "languages": {
1788                    "Rust": {
1789                        "language_setting_2": true
1790                    },
1791                    "JSON": {
1792                        "language_setting_1": false
1793                    }
1794                }
1795            }"#
1796            .unindent(),
1797            cx,
1798        );
1799
1800        // entries removed
1801        check_settings_update::<LanguageSettings>(
1802            &mut store,
1803            r#"{
1804                "languages": {
1805                    "Rust": {
1806                        "language_setting_2": true
1807                    },
1808                    "JSON": {
1809                        "language_setting_1": false
1810                    }
1811                }
1812            }"#
1813            .unindent(),
1814            |settings| {
1815                settings.languages.remove("JSON").unwrap();
1816            },
1817            r#"{
1818                "languages": {
1819                    "Rust": {
1820                        "language_setting_2": true
1821                    }
1822                }
1823            }"#
1824            .unindent(),
1825            cx,
1826        );
1827
1828        check_settings_update::<LanguageSettings>(
1829            &mut store,
1830            r#"{
1831                "languages": {
1832                    "Rust": {
1833                        "language_setting_2": true
1834                    },
1835                    "JSON": {
1836                        "language_setting_1": false
1837                    }
1838                }
1839            }"#
1840            .unindent(),
1841            |settings| {
1842                settings.languages.remove("Rust").unwrap();
1843            },
1844            r#"{
1845                "languages": {
1846                    "JSON": {
1847                        "language_setting_1": false
1848                    }
1849                }
1850            }"#
1851            .unindent(),
1852            cx,
1853        );
1854
1855        // weird formatting
1856        check_settings_update::<UserSettings>(
1857            &mut store,
1858            r#"{
1859                "user":   { "age": 36, "name": "Max", "staff": true }
1860                }"#
1861            .unindent(),
1862            |settings| settings.age = Some(37),
1863            r#"{
1864                "user":   { "age": 37, "name": "Max", "staff": true }
1865                }"#
1866            .unindent(),
1867            cx,
1868        );
1869
1870        // single-line formatting, other keys
1871        check_settings_update::<MultiKeySettings>(
1872            &mut store,
1873            r#"{ "one": 1, "two": 2 }"#.unindent(),
1874            |settings| settings.key1 = Some("x".into()),
1875            r#"{ "key1": "x", "one": 1, "two": 2 }"#.unindent(),
1876            cx,
1877        );
1878
1879        // empty object
1880        check_settings_update::<UserSettings>(
1881            &mut store,
1882            r#"{
1883                "user": {}
1884            }"#
1885            .unindent(),
1886            |settings| settings.age = Some(37),
1887            r#"{
1888                "user": {
1889                    "age": 37
1890                }
1891            }"#
1892            .unindent(),
1893            cx,
1894        );
1895
1896        // no content
1897        check_settings_update::<UserSettings>(
1898            &mut store,
1899            r#""#.unindent(),
1900            |settings| settings.age = Some(37),
1901            r#"{
1902                "user": {
1903                    "age": 37
1904                }
1905            }
1906            "#
1907            .unindent(),
1908            cx,
1909        );
1910
1911        check_settings_update::<UserSettings>(
1912            &mut store,
1913            r#"{
1914            }
1915            "#
1916            .unindent(),
1917            |settings| settings.age = Some(37),
1918            r#"{
1919                "user": {
1920                    "age": 37
1921                }
1922            }
1923            "#
1924            .unindent(),
1925            cx,
1926        );
1927    }
1928
1929    #[gpui::test]
1930    fn test_vscode_import(cx: &mut App) {
1931        let mut store = SettingsStore::new(cx);
1932        store.register_setting::<UserSettings>(cx);
1933        store.register_setting::<JournalSettings>(cx);
1934        store.register_setting::<LanguageSettings>(cx);
1935        store.register_setting::<MultiKeySettings>(cx);
1936
1937        // create settings that werent present
1938        check_vscode_import(
1939            &mut store,
1940            r#"{
1941            }
1942            "#
1943            .unindent(),
1944            r#" { "user.age": 37 } "#.to_owned(),
1945            r#"{
1946                "user": {
1947                    "age": 37
1948                }
1949            }
1950            "#
1951            .unindent(),
1952            cx,
1953        );
1954
1955        // persist settings that were present
1956        check_vscode_import(
1957            &mut store,
1958            r#"{
1959                "user": {
1960                    "staff": true,
1961                    "age": 37
1962                }
1963            }
1964            "#
1965            .unindent(),
1966            r#"{ "user.age": 42 }"#.to_owned(),
1967            r#"{
1968                "user": {
1969                    "staff": true,
1970                    "age": 42
1971                }
1972            }
1973            "#
1974            .unindent(),
1975            cx,
1976        );
1977
1978        // don't clobber settings that aren't present in vscode
1979        check_vscode_import(
1980            &mut store,
1981            r#"{
1982                "user": {
1983                    "staff": true,
1984                    "age": 37
1985                }
1986            }
1987            "#
1988            .unindent(),
1989            r#"{}"#.to_owned(),
1990            r#"{
1991                "user": {
1992                    "staff": true,
1993                    "age": 37
1994                }
1995            }
1996            "#
1997            .unindent(),
1998            cx,
1999        );
2000
2001        // custom enum
2002        check_vscode_import(
2003            &mut store,
2004            r#"{
2005                "journal": {
2006                "hour_format": "hour12"
2007                }
2008            }
2009            "#
2010            .unindent(),
2011            r#"{ "time_format": "24" }"#.to_owned(),
2012            r#"{
2013                "journal": {
2014                "hour_format": "hour24"
2015                }
2016            }
2017            "#
2018            .unindent(),
2019            cx,
2020        );
2021
2022        // Multiple keys for one setting
2023        check_vscode_import(
2024            &mut store,
2025            r#"{
2026                "key1": "value"
2027            }
2028            "#
2029            .unindent(),
2030            r#"{
2031                "key_1_first": "hello",
2032                "key_1_second": "world"
2033            }"#
2034            .to_owned(),
2035            r#"{
2036                "key1": "hello world"
2037            }
2038            "#
2039            .unindent(),
2040            cx,
2041        );
2042
2043        // Merging lists together entries added and updated
2044        check_vscode_import(
2045            &mut store,
2046            r#"{
2047                "languages": {
2048                    "JSON": {
2049                        "language_setting_1": true
2050                    },
2051                    "Rust": {
2052                        "language_setting_2": true
2053                    }
2054                }
2055            }"#
2056            .unindent(),
2057            r#"{
2058                "vscode_languages": [
2059                    {
2060                        "name": "JavaScript",
2061                        "language_setting_1": true
2062                    },
2063                    {
2064                        "name": "Rust",
2065                        "language_setting_2": false
2066                    }
2067                ]
2068            }"#
2069            .to_owned(),
2070            r#"{
2071                "languages": {
2072                    "JavaScript": {
2073                        "language_setting_1": true
2074                    },
2075                    "JSON": {
2076                        "language_setting_1": true
2077                    },
2078                    "Rust": {
2079                        "language_setting_2": false
2080                    }
2081                }
2082            }"#
2083            .unindent(),
2084            cx,
2085        );
2086    }
2087
2088    fn check_vscode_import(
2089        store: &mut SettingsStore,
2090        old: String,
2091        vscode: String,
2092        expected: String,
2093        cx: &mut App,
2094    ) {
2095        store.set_user_settings(&old, cx).ok();
2096        let new = store.get_vscode_edits(
2097            old,
2098            &VsCodeSettings::from_str(&vscode, VsCodeSettingsSource::VsCode).unwrap(),
2099        );
2100        pretty_assertions::assert_eq!(new, expected);
2101    }
2102
2103    #[derive(Debug, PartialEq, Deserialize, SettingsUi)]
2104    struct UserSettings {
2105        name: String,
2106        age: u32,
2107        staff: bool,
2108    }
2109
2110    #[derive(Default, Clone, Serialize, Deserialize, JsonSchema, SettingsUi)]
2111    struct UserSettingsContent {
2112        name: Option<String>,
2113        age: Option<u32>,
2114        staff: Option<bool>,
2115    }
2116
2117    impl Settings for UserSettings {
2118        const KEY: Option<&'static str> = Some("user");
2119        type FileContent = UserSettingsContent;
2120
2121        fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
2122            sources.json_merge()
2123        }
2124
2125        fn import_from_vscode(vscode: &VsCodeSettings, current: &mut Self::FileContent) {
2126            vscode.u32_setting("user.age", &mut current.age);
2127        }
2128    }
2129
2130    #[derive(Debug, Deserialize, PartialEq, SettingsUi)]
2131    struct TurboSetting(bool);
2132
2133    impl Settings for TurboSetting {
2134        const KEY: Option<&'static str> = Some("turbo");
2135        type FileContent = Option<bool>;
2136
2137        fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
2138            sources.json_merge()
2139        }
2140
2141        fn import_from_vscode(_vscode: &VsCodeSettings, _current: &mut Self::FileContent) {}
2142    }
2143
2144    #[derive(Clone, Debug, PartialEq, Deserialize, SettingsUi)]
2145    struct MultiKeySettings {
2146        #[serde(default)]
2147        key1: String,
2148        #[serde(default)]
2149        key2: String,
2150    }
2151
2152    #[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
2153    struct MultiKeySettingsJson {
2154        key1: Option<String>,
2155        key2: Option<String>,
2156    }
2157
2158    impl Settings for MultiKeySettings {
2159        const KEY: Option<&'static str> = None;
2160
2161        type FileContent = MultiKeySettingsJson;
2162
2163        fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
2164            sources.json_merge()
2165        }
2166
2167        fn import_from_vscode(vscode: &VsCodeSettings, current: &mut Self::FileContent) {
2168            let first_value = vscode.read_string("key_1_first");
2169            let second_value = vscode.read_string("key_1_second");
2170
2171            if let Some((first, second)) = first_value.zip(second_value) {
2172                current.key1 = Some(format!("{} {}", first, second));
2173            }
2174        }
2175    }
2176
2177    #[derive(Debug, Deserialize, SettingsUi)]
2178    struct JournalSettings {
2179        pub path: String,
2180        pub hour_format: HourFormat,
2181    }
2182
2183    #[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
2184    #[serde(rename_all = "snake_case")]
2185    enum HourFormat {
2186        Hour12,
2187        Hour24,
2188    }
2189
2190    #[derive(Clone, Default, Debug, Serialize, Deserialize, JsonSchema)]
2191    struct JournalSettingsJson {
2192        pub path: Option<String>,
2193        pub hour_format: Option<HourFormat>,
2194    }
2195
2196    impl Settings for JournalSettings {
2197        const KEY: Option<&'static str> = Some("journal");
2198
2199        type FileContent = JournalSettingsJson;
2200
2201        fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
2202            sources.json_merge()
2203        }
2204
2205        fn import_from_vscode(vscode: &VsCodeSettings, current: &mut Self::FileContent) {
2206            vscode.enum_setting("time_format", &mut current.hour_format, |s| match s {
2207                "12" => Some(HourFormat::Hour12),
2208                "24" => Some(HourFormat::Hour24),
2209                _ => None,
2210            });
2211        }
2212    }
2213
2214    #[gpui::test]
2215    fn test_global_settings(cx: &mut App) {
2216        let mut store = SettingsStore::new(cx);
2217        store.register_setting::<UserSettings>(cx);
2218        store
2219            .set_default_settings(
2220                r#"{
2221                    "user": {
2222                        "name": "John Doe",
2223                        "age": 30,
2224                        "staff": false
2225                    }
2226                }"#,
2227                cx,
2228            )
2229            .unwrap();
2230
2231        // Set global settings - these should override defaults but not user settings
2232        store
2233            .set_global_settings(
2234                r#"{
2235                    "user": {
2236                        "name": "Global User",
2237                        "age": 35,
2238                        "staff": true
2239                    }
2240                }"#,
2241                cx,
2242            )
2243            .unwrap();
2244
2245        // Before user settings, global settings should apply
2246        assert_eq!(
2247            store.get::<UserSettings>(None),
2248            &UserSettings {
2249                name: "Global User".to_string(),
2250                age: 35,
2251                staff: true,
2252            }
2253        );
2254
2255        // Set user settings - these should override both defaults and global
2256        store
2257            .set_user_settings(
2258                r#"{
2259                    "user": {
2260                        "age": 40
2261                    }
2262                }"#,
2263                cx,
2264            )
2265            .unwrap();
2266
2267        // User settings should override global settings
2268        assert_eq!(
2269            store.get::<UserSettings>(None),
2270            &UserSettings {
2271                name: "Global User".to_string(), // Name from global settings
2272                age: 40,                         // Age from user settings
2273                staff: true,                     // Staff from global settings
2274            }
2275        );
2276    }
2277
2278    #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema, SettingsUi)]
2279    struct LanguageSettings {
2280        #[serde(default)]
2281        languages: HashMap<String, LanguageSettingEntry>,
2282    }
2283
2284    #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
2285    struct LanguageSettingEntry {
2286        language_setting_1: Option<bool>,
2287        language_setting_2: Option<bool>,
2288    }
2289
2290    impl Settings for LanguageSettings {
2291        const KEY: Option<&'static str> = None;
2292
2293        type FileContent = Self;
2294
2295        fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
2296            sources.json_merge()
2297        }
2298
2299        fn import_from_vscode(vscode: &VsCodeSettings, current: &mut Self::FileContent) {
2300            current.languages.extend(
2301                vscode
2302                    .read_value("vscode_languages")
2303                    .and_then(|value| value.as_array())
2304                    .map(|languages| {
2305                        languages
2306                            .iter()
2307                            .filter_map(|value| value.as_object())
2308                            .filter_map(|item| {
2309                                let mut rest = item.clone();
2310                                let name = rest.remove("name")?.as_str()?.to_string();
2311                                let entry = serde_json::from_value::<LanguageSettingEntry>(
2312                                    serde_json::Value::Object(rest),
2313                                )
2314                                .ok()?;
2315
2316                                Some((name, entry))
2317                            })
2318                    })
2319                    .into_iter()
2320                    .flatten(),
2321            );
2322        }
2323    }
2324}