settings_file.rs

  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
  8pub const EMPTY_THEME_NAME: &str = "empty-theme";
  9
 10/// Settings for visual tests that use proper fonts instead of Courier.
 11/// Uses Helvetica Neue for UI (sans-serif) and Menlo for code (monospace),
 12/// which are available on all macOS systems.
 13#[cfg(any(test, feature = "test-support"))]
 14pub fn visual_test_settings() -> String {
 15    let mut value =
 16        crate::parse_json_with_comments::<serde_json::Value>(crate::default_settings().as_ref())
 17            .unwrap();
 18    util::merge_non_null_json_value_into(
 19        serde_json::json!({
 20            "ui_font_family": ".SystemUIFont",
 21            "ui_font_features": {},
 22            "ui_font_size": 14,
 23            "ui_font_fallback": [],
 24            "buffer_font_family": "Menlo",
 25            "buffer_font_features": {},
 26            "buffer_font_size": 14,
 27            "buffer_font_fallbacks": [],
 28            "theme": EMPTY_THEME_NAME,
 29        }),
 30        &mut value,
 31    );
 32    value.as_object_mut().unwrap().remove("languages");
 33    serde_json::to_string(&value).unwrap()
 34}
 35
 36#[cfg(any(test, feature = "test-support"))]
 37pub fn test_settings() -> String {
 38    let mut value =
 39        crate::parse_json_with_comments::<serde_json::Value>(crate::default_settings().as_ref())
 40            .unwrap();
 41    #[cfg(not(target_os = "windows"))]
 42    util::merge_non_null_json_value_into(
 43        serde_json::json!({
 44            "ui_font_family": "Courier",
 45            "ui_font_features": {},
 46            "ui_font_size": 14,
 47            "ui_font_fallback": [],
 48            "buffer_font_family": "Courier",
 49            "buffer_font_features": {},
 50            "buffer_font_size": 14,
 51            "buffer_font_fallbacks": [],
 52            "theme": EMPTY_THEME_NAME,
 53        }),
 54        &mut value,
 55    );
 56    #[cfg(target_os = "windows")]
 57    util::merge_non_null_json_value_into(
 58        serde_json::json!({
 59            "ui_font_family": "Courier New",
 60            "ui_font_features": {},
 61            "ui_font_size": 14,
 62            "ui_font_fallback": [],
 63            "buffer_font_family": "Courier New",
 64            "buffer_font_features": {},
 65            "buffer_font_size": 14,
 66            "buffer_font_fallbacks": [],
 67            "theme": EMPTY_THEME_NAME,
 68        }),
 69        &mut value,
 70    );
 71    value.as_object_mut().unwrap().remove("languages");
 72    serde_json::to_string(&value).unwrap()
 73}
 74
 75pub fn watch_config_file(
 76    executor: &BackgroundExecutor,
 77    fs: Arc<dyn Fs>,
 78    path: PathBuf,
 79) -> (mpsc::UnboundedReceiver<String>, gpui::Task<()>) {
 80    let (tx, rx) = mpsc::unbounded();
 81    let task = executor.spawn(async move {
 82        let (events, _) = fs.watch(&path, Duration::from_millis(100)).await;
 83        futures::pin_mut!(events);
 84
 85        let contents = fs.load(&path).await.unwrap_or_default();
 86        if tx.unbounded_send(contents).is_err() {
 87            return;
 88        }
 89
 90        loop {
 91            if events.next().await.is_none() {
 92                break;
 93            }
 94
 95            if let Ok(contents) = fs.load(&path).await
 96                && tx.unbounded_send(contents).is_err()
 97            {
 98                break;
 99            }
100        }
101    });
102    (rx, task)
103}
104
105pub fn watch_config_dir(
106    executor: &BackgroundExecutor,
107    fs: Arc<dyn Fs>,
108    dir_path: PathBuf,
109    config_paths: HashSet<PathBuf>,
110) -> mpsc::UnboundedReceiver<String> {
111    let (tx, rx) = mpsc::unbounded();
112    executor
113        .spawn(async move {
114            for file_path in &config_paths {
115                if fs.metadata(file_path).await.is_ok_and(|v| v.is_some())
116                    && let Ok(contents) = fs.load(file_path).await
117                    && tx.unbounded_send(contents).is_err()
118                {
119                    return;
120                }
121            }
122
123            let (events, _) = fs.watch(&dir_path, Duration::from_millis(100)).await;
124            futures::pin_mut!(events);
125
126            while let Some(event_batch) = events.next().await {
127                for event in event_batch {
128                    if config_paths.contains(&event.path) {
129                        match event.kind {
130                            Some(PathEventKind::Removed) => {
131                                if tx.unbounded_send(String::new()).is_err() {
132                                    return;
133                                }
134                            }
135                            Some(PathEventKind::Created) | Some(PathEventKind::Changed) => {
136                                if let Ok(contents) = fs.load(&event.path).await
137                                    && tx.unbounded_send(contents).is_err()
138                                {
139                                    return;
140                                }
141                            }
142                            _ => {}
143                        }
144                    }
145                }
146            }
147        })
148        .detach();
149
150    rx
151}
152
153pub fn update_settings_file(
154    fs: Arc<dyn Fs>,
155    cx: &App,
156    update: impl 'static + Send + FnOnce(&mut SettingsContent, &App),
157) {
158    SettingsStore::global(cx).update_settings_file(fs, update);
159}