mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-10 20:29:05 +00:00
Add a better API for updating settings in the SettingsStore in tests
This commit is contained in:
parent
bc5b78198a
commit
9ae10a5dd9
3 changed files with 42 additions and 23 deletions
|
@ -172,12 +172,10 @@ pub fn update_settings_file<T: Setting>(
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let edits = cx.read(|cx| cx.global::<SettingsStore>().update::<T>(&old_text, update));
|
let new_text = cx.read(|cx| {
|
||||||
|
cx.global::<SettingsStore>()
|
||||||
let mut new_text = old_text;
|
.new_text_for_update::<T>(old_text, update)
|
||||||
for (range, replacement) in edits.into_iter().rev() {
|
});
|
||||||
new_text.replace_range(range, &replacement);
|
|
||||||
}
|
|
||||||
|
|
||||||
cx.background()
|
cx.background()
|
||||||
.spawn(async move { fs.atomic_write(paths::SETTINGS.clone(), new_text).await })
|
.spawn(async move { fs.atomic_write(paths::SETTINGS.clone(), new_text).await })
|
||||||
|
|
|
@ -184,22 +184,43 @@ impl SettingsStore {
|
||||||
this
|
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
|
/// This is only for tests. Normally, settings are only loaded from
|
||||||
/// JSON files.
|
/// JSON files.
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
pub fn replace_value<T: Setting>(&mut self, value: T) {
|
pub fn update_user_settings<T: Setting>(
|
||||||
self.setting_values
|
&mut self,
|
||||||
.get_mut(&TypeId::of::<T>())
|
cx: &AppContext,
|
||||||
.unwrap_or_else(|| panic!("unregistered setting type {}", type_name::<T>()))
|
update: impl FnOnce(&mut T::FileContent),
|
||||||
.set_global_value(Box::new(value))
|
) {
|
||||||
|
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::<T>(old_text, update);
|
||||||
|
self.set_user_settings(&new_text, cx).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update the value of a setting.
|
/// Update the value of a setting in a JSON file, returning the new text
|
||||||
///
|
/// for that JSON file.
|
||||||
/// Returns a list of edits to apply to the JSON file.
|
pub fn new_text_for_update<T: Setting>(
|
||||||
pub fn update<T: Setting>(
|
&self,
|
||||||
|
old_text: String,
|
||||||
|
update: impl FnOnce(&mut T::FileContent),
|
||||||
|
) -> String {
|
||||||
|
let edits = self.edits_for_update::<T>(&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<T: Setting>(
|
||||||
&self,
|
&self,
|
||||||
text: &str,
|
text: &str,
|
||||||
update: impl FnOnce(&mut T::FileContent),
|
update: impl FnOnce(&mut T::FileContent),
|
||||||
|
@ -1129,7 +1150,7 @@ mod tests {
|
||||||
cx: &mut AppContext,
|
cx: &mut AppContext,
|
||||||
) {
|
) {
|
||||||
store.set_user_settings(&old_json, cx).ok();
|
store.set_user_settings(&old_json, cx).ok();
|
||||||
let edits = store.update::<T>(&old_json, update);
|
let edits = store.edits_for_update::<T>(&old_json, update);
|
||||||
let mut new_json = old_json;
|
let mut new_json = old_json;
|
||||||
for (range, replacement) in edits.into_iter().rev() {
|
for (range, replacement) in edits.into_iter().rev() {
|
||||||
new_json.replace_range(range, &replacement);
|
new_json.replace_range(range, &replacement);
|
||||||
|
|
|
@ -21,8 +21,8 @@ impl<'a> VimTestContext<'a> {
|
||||||
search::init(cx);
|
search::init(cx);
|
||||||
crate::init(cx);
|
crate::init(cx);
|
||||||
|
|
||||||
cx.update_global(|store: &mut SettingsStore, _| {
|
cx.update_global(|store: &mut SettingsStore, cx| {
|
||||||
store.replace_value(VimModeSetting(enabled));
|
store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(enabled));
|
||||||
});
|
});
|
||||||
|
|
||||||
settings::KeymapFileContent::load("keymaps/vim.json", cx).unwrap();
|
settings::KeymapFileContent::load("keymaps/vim.json", cx).unwrap();
|
||||||
|
@ -53,16 +53,16 @@ impl<'a> VimTestContext<'a> {
|
||||||
|
|
||||||
pub fn enable_vim(&mut self) {
|
pub fn enable_vim(&mut self) {
|
||||||
self.cx.update(|cx| {
|
self.cx.update(|cx| {
|
||||||
cx.update_global(|store: &mut SettingsStore, _| {
|
cx.update_global(|store: &mut SettingsStore, cx| {
|
||||||
store.replace_value(VimModeSetting(true))
|
store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(true));
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn disable_vim(&mut self) {
|
pub fn disable_vim(&mut self) {
|
||||||
self.cx.update(|cx| {
|
self.cx.update(|cx| {
|
||||||
cx.update_global(|store: &mut SettingsStore, _| {
|
cx.update_global(|store: &mut SettingsStore, cx| {
|
||||||
store.replace_value(VimModeSetting(false))
|
store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(false));
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue