ZIm/crates/settings/src/settings_file.rs
smit 65934ae181
migrator: In-memory migration and improved UX (#24621)
This PR adds:

- Support for deprecated keymap and settings (In-memory migration)
- Migration prompt only shown in `settings.json` / `keymap.json`.

Release Notes:

- The migration banner will only appear in `settings.json` and
`keymap.json` if you have deprecated settings or keybindings, allowing
you to migrate them to work with the new version on Zed.
2025-02-12 06:47:08 +05:30

87 lines
2.6 KiB
Rust

use crate::{settings_store::SettingsStore, Settings};
use fs::Fs;
use futures::{channel::mpsc, StreamExt};
use gpui::{App, BackgroundExecutor, ReadGlobal};
use std::{path::PathBuf, sync::Arc, time::Duration};
pub const EMPTY_THEME_NAME: &str = "empty-theme";
#[cfg(any(test, feature = "test-support"))]
pub fn test_settings() -> String {
let mut value = crate::settings_store::parse_json_with_comments::<serde_json::Value>(
crate::default_settings().as_ref(),
)
.unwrap();
#[cfg(not(target_os = "windows"))]
util::merge_non_null_json_value_into(
serde_json::json!({
"ui_font_family": "Courier",
"ui_font_features": {},
"ui_font_size": 14,
"ui_font_fallback": [],
"buffer_font_family": "Courier",
"buffer_font_features": {},
"buffer_font_size": 14,
"buffer_font_fallback": [],
"theme": EMPTY_THEME_NAME,
}),
&mut value,
);
#[cfg(target_os = "windows")]
util::merge_non_null_json_value_into(
serde_json::json!({
"ui_font_family": "Courier New",
"ui_font_features": {},
"ui_font_size": 14,
"ui_font_fallback": [],
"buffer_font_family": "Courier New",
"buffer_font_features": {},
"buffer_font_size": 14,
"buffer_font_fallback": [],
"theme": EMPTY_THEME_NAME,
}),
&mut value,
);
value.as_object_mut().unwrap().remove("languages");
serde_json::to_string(&value).unwrap()
}
pub fn watch_config_file(
executor: &BackgroundExecutor,
fs: Arc<dyn Fs>,
path: PathBuf,
) -> mpsc::UnboundedReceiver<String> {
let (tx, rx) = mpsc::unbounded();
executor
.spawn(async move {
let (events, _) = fs.watch(&path, Duration::from_millis(100)).await;
futures::pin_mut!(events);
let contents = fs.load(&path).await.unwrap_or_default();
if tx.unbounded_send(contents).is_err() {
return;
}
loop {
if events.next().await.is_none() {
break;
}
if let Ok(contents) = fs.load(&path).await {
if tx.unbounded_send(contents).is_err() {
break;
}
}
}
})
.detach();
rx
}
pub fn update_settings_file<T: Settings>(
fs: Arc<dyn Fs>,
cx: &App,
update: impl 'static + Send + FnOnce(&mut T::FileContent, &App),
) {
SettingsStore::global(cx).update_settings_file::<T>(fs, update);
}