1use crate::{settings_store::SettingsStore, Settings};
2use anyhow::Result;
3use fs2::Fs;
4use futures::{channel::mpsc, StreamExt};
5use gpui2::{AppContext, Executor};
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: &Executor,
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.executor().block(user_settings_file_rx.next()).unwrap();
67 cx.update_global(|store: &mut SettingsStore, cx| {
68 store
69 .set_user_settings(&user_settings_content, cx)
70 .log_err();
71 });
72 cx.spawn(move |mut cx| async move {
73 while let Some(user_settings_content) = user_settings_file_rx.next().await {
74 let result = cx.update_global(|store: &mut SettingsStore, cx| {
75 store
76 .set_user_settings(&user_settings_content, cx)
77 .log_err();
78 cx.refresh();
79 });
80 if result.is_err() {
81 break; // App dropped
82 }
83 }
84 })
85 .detach();
86}
87
88async fn load_settings(fs: &Arc<dyn Fs>) -> Result<String> {
89 match fs.load(&paths::SETTINGS).await {
90 result @ Ok(_) => result,
91 Err(err) => {
92 if let Some(e) = err.downcast_ref::<std::io::Error>() {
93 if e.kind() == ErrorKind::NotFound {
94 return Ok(crate::initial_user_settings_content().to_string());
95 }
96 }
97 return Err(err);
98 }
99 }
100}
101
102pub fn update_settings_file<T: Settings>(
103 fs: Arc<dyn Fs>,
104 cx: &mut AppContext,
105 update: impl 'static + Send + FnOnce(&mut T::FileContent),
106) {
107 cx.spawn(|cx| async move {
108 let old_text = load_settings(&fs).await?;
109 let new_text = cx.read_global(|store: &SettingsStore, _cx| {
110 store.new_text_for_update::<T>(old_text, update)
111 })?;
112 fs.atomic_write(paths::SETTINGS.clone(), new_text).await?;
113 anyhow::Ok(())
114 })
115 .detach_and_log_err(cx);
116}