1use crate::{settings_store::SettingsStore, Settings};
2use anyhow::Result;
3use fs2::Fs;
4use futures::{channel::mpsc, StreamExt};
5use gpui2::{AppContext, BackgroundExecutor};
6use std::{io::ErrorKind, path::PathBuf, str, sync::Arc, time::Duration};
7use util::{paths, ResultExt};
8
9pub const EMPTY_THEME_NAME: &'static str = "empty-theme";
10
11#[cfg(any(test, feature = "test-support"))]
12pub fn test_settings() -> String {
13 let mut value = crate::settings_store::parse_json_with_comments::<serde_json::Value>(
14 crate::default_settings().as_ref(),
15 )
16 .unwrap();
17 util::merge_non_null_json_value_into(
18 serde_json::json!({
19 "buffer_font_family": "Courier",
20 "buffer_font_features": {},
21 "buffer_font_size": 14,
22 "theme": EMPTY_THEME_NAME,
23 }),
24 &mut value,
25 );
26 value.as_object_mut().unwrap().remove("languages");
27 serde_json::to_string(&value).unwrap()
28}
29
30pub fn watch_config_file(
31 executor: &BackgroundExecutor,
32 fs: Arc<dyn Fs>,
33 path: PathBuf,
34) -> mpsc::UnboundedReceiver<String> {
35 let (tx, rx) = mpsc::unbounded();
36 executor
37 .spawn(async move {
38 let events = fs.watch(&path, Duration::from_millis(100)).await;
39 futures::pin_mut!(events);
40
41 let contents = fs.load(&path).await.unwrap_or_default();
42 if tx.unbounded_send(contents).is_err() {
43 return;
44 }
45
46 loop {
47 if events.next().await.is_none() {
48 break;
49 }
50
51 if let Ok(contents) = fs.load(&path).await {
52 if !tx.unbounded_send(contents).is_ok() {
53 break;
54 }
55 }
56 }
57 })
58 .detach();
59 rx
60}
61
62pub fn handle_settings_file_changes(
63 mut user_settings_file_rx: mpsc::UnboundedReceiver<String>,
64 cx: &mut AppContext,
65) {
66 let user_settings_content = cx
67 .background_executor()
68 .block(user_settings_file_rx.next())
69 .unwrap();
70 cx.update_global(|store: &mut SettingsStore, cx| {
71 store
72 .set_user_settings(&user_settings_content, cx)
73 .log_err();
74 });
75 cx.spawn(move |mut cx| async move {
76 while let Some(user_settings_content) = user_settings_file_rx.next().await {
77 let result = cx.update_global(|store: &mut SettingsStore, cx| {
78 store
79 .set_user_settings(&user_settings_content, cx)
80 .log_err();
81 cx.refresh();
82 });
83 if result.is_err() {
84 break; // App dropped
85 }
86 }
87 })
88 .detach();
89}
90
91async fn load_settings(fs: &Arc<dyn Fs>) -> Result<String> {
92 match fs.load(&paths::SETTINGS).await {
93 result @ Ok(_) => result,
94 Err(err) => {
95 if let Some(e) = err.downcast_ref::<std::io::Error>() {
96 if e.kind() == ErrorKind::NotFound {
97 return Ok(crate::initial_user_settings_content().to_string());
98 }
99 }
100 return Err(err);
101 }
102 }
103}
104
105pub fn update_settings_file<T: Settings>(
106 fs: Arc<dyn Fs>,
107 cx: &mut AppContext,
108 update: impl 'static + Send + FnOnce(&mut T::FileContent),
109) {
110 cx.spawn(|cx| async move {
111 let old_text = load_settings(&fs).await?;
112 let new_text = cx.read_global(|store: &SettingsStore, _cx| {
113 store.new_text_for_update::<T>(old_text, update)
114 })?;
115 fs.atomic_write(paths::SETTINGS.clone(), new_text).await?;
116 anyhow::Ok(())
117 })
118 .detach_and_log_err(cx);
119}