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