From 9ae10a5dd9e92d2536f180106dcfd2abb961d498 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 11 May 2023 14:39:43 -0700 Subject: [PATCH] Add a better API for updating settings in the SettingsStore in tests --- crates/settings/src/settings_file.rs | 10 +++--- crates/settings/src/settings_store.rs | 43 ++++++++++++++++++------- crates/vim/src/test/vim_test_context.rs | 12 +++---- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/crates/settings/src/settings_file.rs b/crates/settings/src/settings_file.rs index 990ccf0249..936f3a7099 100644 --- a/crates/settings/src/settings_file.rs +++ b/crates/settings/src/settings_file.rs @@ -172,12 +172,10 @@ pub fn update_settings_file( }) .await?; - let edits = cx.read(|cx| cx.global::().update::(&old_text, update)); - - let mut new_text = old_text; - for (range, replacement) in edits.into_iter().rev() { - new_text.replace_range(range, &replacement); - } + let new_text = cx.read(|cx| { + cx.global::() + .new_text_for_update::(old_text, update) + }); cx.background() .spawn(async move { fs.atomic_write(paths::SETTINGS.clone(), new_text).await }) diff --git a/crates/settings/src/settings_store.rs b/crates/settings/src/settings_store.rs index 59f7bee10f..caa77a3603 100644 --- a/crates/settings/src/settings_store.rs +++ b/crates/settings/src/settings_store.rs @@ -184,22 +184,43 @@ impl SettingsStore { this } - /// Override the global value for a particular setting. + /// Update the value of a setting in the user's global configuration. /// /// This is only for tests. Normally, settings are only loaded from /// JSON files. #[cfg(any(test, feature = "test-support"))] - pub fn replace_value(&mut self, value: T) { - self.setting_values - .get_mut(&TypeId::of::()) - .unwrap_or_else(|| panic!("unregistered setting type {}", type_name::())) - .set_global_value(Box::new(value)) + pub fn update_user_settings( + &mut self, + cx: &AppContext, + update: impl FnOnce(&mut T::FileContent), + ) { + let old_text = if let Some(user_settings) = &self.user_deserialized_settings { + serde_json::to_string(&user_settings.untyped).unwrap() + } else { + String::new() + }; + let new_text = self.new_text_for_update::(old_text, update); + self.set_user_settings(&new_text, cx).unwrap(); } - /// Update the value of a setting. - /// - /// Returns a list of edits to apply to the JSON file. - pub fn update( + /// Update the value of a setting in a JSON file, returning the new text + /// for that JSON file. + pub fn new_text_for_update( + &self, + old_text: String, + update: impl FnOnce(&mut T::FileContent), + ) -> String { + let edits = self.edits_for_update::(&old_text, update); + let mut new_text = old_text; + for (range, replacement) in edits.into_iter().rev() { + new_text.replace_range(range, &replacement); + } + new_text + } + + /// Update the value of a setting in a JSON file, returning a list + /// of edits to apply to the JSON file. + pub fn edits_for_update( &self, text: &str, update: impl FnOnce(&mut T::FileContent), @@ -1129,7 +1150,7 @@ mod tests { cx: &mut AppContext, ) { store.set_user_settings(&old_json, cx).ok(); - let edits = store.update::(&old_json, update); + let edits = store.edits_for_update::(&old_json, update); let mut new_json = old_json; for (range, replacement) in edits.into_iter().rev() { new_json.replace_range(range, &replacement); diff --git a/crates/vim/src/test/vim_test_context.rs b/crates/vim/src/test/vim_test_context.rs index b426cea717..ac86a08235 100644 --- a/crates/vim/src/test/vim_test_context.rs +++ b/crates/vim/src/test/vim_test_context.rs @@ -21,8 +21,8 @@ impl<'a> VimTestContext<'a> { search::init(cx); crate::init(cx); - cx.update_global(|store: &mut SettingsStore, _| { - store.replace_value(VimModeSetting(enabled)); + cx.update_global(|store: &mut SettingsStore, cx| { + store.update_user_settings::(cx, |s| *s = Some(enabled)); }); settings::KeymapFileContent::load("keymaps/vim.json", cx).unwrap(); @@ -53,16 +53,16 @@ impl<'a> VimTestContext<'a> { pub fn enable_vim(&mut self) { self.cx.update(|cx| { - cx.update_global(|store: &mut SettingsStore, _| { - store.replace_value(VimModeSetting(true)) + cx.update_global(|store: &mut SettingsStore, cx| { + store.update_user_settings::(cx, |s| *s = Some(true)); }); }) } pub fn disable_vim(&mut self) { self.cx.update(|cx| { - cx.update_global(|store: &mut SettingsStore, _| { - store.replace_value(VimModeSetting(false)) + cx.update_global(|store: &mut SettingsStore, cx| { + store.update_user_settings::(cx, |s| *s = Some(false)); }); }) }