This commit is contained in:
Antonio Scandurra
2023-10-21 18:21:14 +02:00
parent b7d30fca2b
commit aa3fb28f81
9 changed files with 161 additions and 42 deletions
+14 -13
View File
@@ -2,7 +2,7 @@ use crate::{settings_store::SettingsStore, Setting};
use anyhow::Result;
use fs::Fs;
use futures::{channel::mpsc, StreamExt};
use gpui2::{AppContext, Context};
use gpui2::{AppContext, Context, Executor};
use std::{
io::ErrorKind,
path::{Path, PathBuf},
@@ -48,7 +48,7 @@ pub fn test_settings() -> String {
}
pub fn watch_config_file(
executor: Arc<Background>,
executor: &Executor,
fs: Arc<dyn Fs>,
path: PathBuf,
) -> mpsc::UnboundedReceiver<String> {
@@ -83,7 +83,7 @@ pub fn handle_settings_file_changes(
mut user_settings_file_rx: mpsc::UnboundedReceiver<String>,
cx: &mut AppContext,
) {
let user_settings_content = cx.background().block(user_settings_file_rx.next()).unwrap();
let user_settings_content = cx.executor().block(user_settings_file_rx.next()).unwrap();
cx.update_global(|store: &mut SettingsStore, cx| {
store
.set_user_settings(&user_settings_content, cx)
@@ -91,14 +91,15 @@ pub fn handle_settings_file_changes(
});
cx.spawn(move |mut cx| async move {
while let Some(user_settings_content) = user_settings_file_rx.next().await {
cx.update(|cx| {
cx.update_global(|store: &mut SettingsStore, cx| {
store
.set_user_settings(&user_settings_content, cx)
.log_err();
});
cx.refresh_windows();
let result = cx.update_global(|store: &mut SettingsStore, cx| {
store
.set_user_settings(&user_settings_content, cx)
.log_err();
cx.refresh();
});
if result.is_err() {
break; // App dropped
}
}
})
.detach();
@@ -124,10 +125,10 @@ pub fn update_settings_file<T: Setting>(
update: impl 'static + Send + FnOnce(&mut T::FileContent),
) {
cx.spawn(|cx| async move {
let old_text = load_settings(&fs).await;
let new_text = cx.read_global(|store: &SettingsStore, cx| {
let old_text = load_settings(&fs).await?;
let new_text = cx.read_global(|store: &SettingsStore, _cx| {
store.new_text_for_update::<T>(old_text, update)
});
})?;
fs.atomic_write(paths::SETTINGS.clone(), new_text).await?;
anyhow::Ok(())
})
+6 -3
View File
@@ -18,7 +18,7 @@ use util::{merge_non_null_json_value_into, RangeExt, ResultExt as _};
/// A value that can be defined as a user setting.
///
/// Settings can be loaded from a combination of multiple JSON files.
pub trait Setting: 'static {
pub trait Setting: 'static + Send + Sync {
/// The name of a key within the JSON file from which this setting should
/// be deserialized. If this is `None`, then the setting will be deserialized
/// from the root object.
@@ -89,7 +89,10 @@ pub struct SettingsStore {
raw_default_settings: serde_json::Value,
raw_user_settings: serde_json::Value,
raw_local_settings: BTreeMap<(usize, Arc<Path>), serde_json::Value>,
tab_size_callback: Option<(TypeId, Box<dyn Fn(&dyn Any) -> Option<usize>>)>,
tab_size_callback: Option<(
TypeId,
Box<dyn Fn(&dyn Any) -> Option<usize> + Send + Sync + 'static>,
)>,
}
impl Default for SettingsStore {
@@ -110,7 +113,7 @@ struct SettingValue<T> {
local_values: Vec<(usize, Arc<Path>, T)>,
}
trait AnySettingValue {
trait AnySettingValue: 'static + Send + Sync {
fn key(&self) -> Option<&'static str>;
fn setting_type_name(&self) -> &'static str;
fn deserialize_setting(&self, json: &serde_json::Value) -> Result<DeserializedSetting>;