1use crate::{settings_content::SettingsContent, settings_store::SettingsStore};
2use collections::HashSet;
3use fs::{Fs, PathEventKind};
4use futures::{StreamExt, channel::mpsc};
5use gpui::{App, BackgroundExecutor, ReadGlobal};
6use std::{path::PathBuf, sync::Arc, time::Duration};
7
8#[cfg(test)]
9mod tests {
10 use super::*;
11 use fs::FakeFs;
12
13 use gpui::TestAppContext;
14 use serde_json::json;
15 use std::path::Path;
16
17 #[gpui::test]
18 async fn test_watch_config_dir_reloads_tracked_file_on_rescan(cx: &mut TestAppContext) {
19 cx.executor().allow_parking();
20
21 let fs = FakeFs::new(cx.background_executor.clone());
22 let config_dir = PathBuf::from("/root/config");
23 let settings_path = PathBuf::from("/root/config/settings.json");
24
25 fs.insert_tree(
26 Path::new("/root"),
27 json!({
28 "config": {
29 "settings.json": "A"
30 }
31 }),
32 )
33 .await;
34
35 let mut rx = watch_config_dir(
36 &cx.background_executor,
37 fs.clone(),
38 config_dir.clone(),
39 HashSet::from_iter([settings_path.clone()]),
40 );
41
42 assert_eq!(rx.next().await.as_deref(), Some("A"));
43 cx.run_until_parked();
44
45 fs.pause_events();
46 fs.insert_file(&settings_path, b"B".to_vec()).await;
47 fs.clear_buffered_events();
48
49 fs.emit_fs_event(&settings_path, Some(PathEventKind::Rescan));
50 fs.unpause_events_and_flush();
51 assert_eq!(rx.next().await.as_deref(), Some("B"));
52
53 fs.pause_events();
54 fs.insert_file(&settings_path, b"A".to_vec()).await;
55 fs.clear_buffered_events();
56
57 fs.emit_fs_event(&config_dir, Some(PathEventKind::Rescan));
58 fs.unpause_events_and_flush();
59 assert_eq!(rx.next().await.as_deref(), Some("A"));
60 }
61}
62
63pub const EMPTY_THEME_NAME: &str = "empty-theme";
64
65/// Settings for visual tests that use proper fonts instead of Courier.
66/// Uses Helvetica Neue for UI (sans-serif) and Menlo for code (monospace),
67/// which are available on all macOS systems.
68#[cfg(any(test, feature = "test-support"))]
69pub fn visual_test_settings() -> String {
70 let mut value =
71 crate::parse_json_with_comments::<serde_json::Value>(crate::default_settings().as_ref())
72 .unwrap();
73 util::merge_non_null_json_value_into(
74 serde_json::json!({
75 "ui_font_family": ".SystemUIFont",
76 "ui_font_features": {},
77 "ui_font_size": 14,
78 "ui_font_fallback": [],
79 "buffer_font_family": "Menlo",
80 "buffer_font_features": {},
81 "buffer_font_size": 14,
82 "buffer_font_fallbacks": [],
83 "theme": EMPTY_THEME_NAME,
84 }),
85 &mut value,
86 );
87 value.as_object_mut().unwrap().remove("languages");
88 serde_json::to_string(&value).unwrap()
89}
90
91#[cfg(any(test, feature = "test-support"))]
92pub fn test_settings() -> String {
93 let mut value =
94 crate::parse_json_with_comments::<serde_json::Value>(crate::default_settings().as_ref())
95 .unwrap();
96 #[cfg(not(target_os = "windows"))]
97 util::merge_non_null_json_value_into(
98 serde_json::json!({
99 "ui_font_family": "Courier",
100 "ui_font_features": {},
101 "ui_font_size": 14,
102 "ui_font_fallback": [],
103 "buffer_font_family": "Courier",
104 "buffer_font_features": {},
105 "buffer_font_size": 14,
106 "buffer_font_fallbacks": [],
107 "theme": EMPTY_THEME_NAME,
108 }),
109 &mut value,
110 );
111 #[cfg(target_os = "windows")]
112 util::merge_non_null_json_value_into(
113 serde_json::json!({
114 "ui_font_family": "Courier New",
115 "ui_font_features": {},
116 "ui_font_size": 14,
117 "ui_font_fallback": [],
118 "buffer_font_family": "Courier New",
119 "buffer_font_features": {},
120 "buffer_font_size": 14,
121 "buffer_font_fallbacks": [],
122 "theme": EMPTY_THEME_NAME,
123 }),
124 &mut value,
125 );
126 value.as_object_mut().unwrap().remove("languages");
127 serde_json::to_string(&value).unwrap()
128}
129
130pub fn watch_config_file(
131 executor: &BackgroundExecutor,
132 fs: Arc<dyn Fs>,
133 path: PathBuf,
134) -> (mpsc::UnboundedReceiver<String>, gpui::Task<()>) {
135 let (tx, rx) = mpsc::unbounded();
136 let task = executor.spawn(async move {
137 let (events, _) = fs.watch(&path, Duration::from_millis(100)).await;
138 futures::pin_mut!(events);
139
140 let contents = fs.load(&path).await.unwrap_or_default();
141 if tx.unbounded_send(contents).is_err() {
142 return;
143 }
144
145 loop {
146 if events.next().await.is_none() {
147 break;
148 }
149
150 if let Ok(contents) = fs.load(&path).await
151 && tx.unbounded_send(contents).is_err()
152 {
153 break;
154 }
155 }
156 });
157 (rx, task)
158}
159
160pub fn watch_config_dir(
161 executor: &BackgroundExecutor,
162 fs: Arc<dyn Fs>,
163 dir_path: PathBuf,
164 config_paths: HashSet<PathBuf>,
165) -> mpsc::UnboundedReceiver<String> {
166 let (tx, rx) = mpsc::unbounded();
167 executor
168 .spawn(async move {
169 for file_path in &config_paths {
170 if fs.metadata(file_path).await.is_ok_and(|v| v.is_some())
171 && let Ok(contents) = fs.load(file_path).await
172 && tx.unbounded_send(contents).is_err()
173 {
174 return;
175 }
176 }
177
178 let (events, _) = fs.watch(&dir_path, Duration::from_millis(100)).await;
179 futures::pin_mut!(events);
180
181 while let Some(event_batch) = events.next().await {
182 for event in event_batch {
183 if config_paths.contains(&event.path) {
184 match event.kind {
185 Some(PathEventKind::Removed) => {
186 if tx.unbounded_send(String::new()).is_err() {
187 return;
188 }
189 }
190 Some(PathEventKind::Created) | Some(PathEventKind::Changed) => {
191 if let Ok(contents) = fs.load(&event.path).await
192 && tx.unbounded_send(contents).is_err()
193 {
194 return;
195 }
196 }
197 Some(PathEventKind::Rescan) => {
198 for file_path in &config_paths {
199 let contents = fs.load(file_path).await.unwrap_or_default();
200 if tx.unbounded_send(contents).is_err() {
201 return;
202 }
203 }
204 }
205 _ => {}
206 }
207 } else if matches!(event.kind, Some(PathEventKind::Rescan))
208 && event.path == dir_path
209 {
210 for file_path in &config_paths {
211 let contents = fs.load(file_path).await.unwrap_or_default();
212 if tx.unbounded_send(contents).is_err() {
213 return;
214 }
215 }
216 }
217 }
218 }
219 })
220 .detach();
221
222 rx
223}
224
225pub fn update_settings_file(
226 fs: Arc<dyn Fs>,
227 cx: &App,
228 update: impl 'static + Send + FnOnce(&mut SettingsContent, &App),
229) {
230 SettingsStore::global(cx).update_settings_file(fs, update);
231}