1use crate::{settings_store::SettingsStore, Setting};
2use anyhow::Result;
3use fs::Fs;
4use futures::{channel::mpsc, StreamExt};
5use gpui2::{AppContext, Executor};
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(|store: &mut SettingsStore, 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: &Executor,
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.executor().block(user_settings_file_rx.next()).unwrap();
87 cx.update_global(|store: &mut SettingsStore, 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 let result = cx.update_global(|store: &mut SettingsStore, cx| {
95 store
96 .set_user_settings(&user_settings_content, cx)
97 .log_err();
98 cx.refresh();
99 });
100 if result.is_err() {
101 break; // App dropped
102 }
103 }
104 })
105 .detach();
106}
107
108async fn load_settings(fs: &Arc<dyn Fs>) -> Result<String> {
109 match fs.load(&paths::SETTINGS).await {
110 result @ Ok(_) => result,
111 Err(err) => {
112 if let Some(e) = err.downcast_ref::<std::io::Error>() {
113 if e.kind() == ErrorKind::NotFound {
114 return Ok(crate::initial_user_settings_content().to_string());
115 }
116 }
117 return Err(err);
118 }
119 }
120}
121
122pub fn update_settings_file<T: Setting>(
123 fs: Arc<dyn Fs>,
124 cx: &mut AppContext,
125 update: impl 'static + Send + FnOnce(&mut T::FileContent),
126) {
127 cx.spawn(|cx| async move {
128 let old_text = load_settings(&fs).await?;
129 let new_text = cx.read_global(|store: &SettingsStore, _cx| {
130 store.new_text_for_update::<T>(old_text, update)
131 })?;
132 fs.atomic_write(paths::SETTINGS.clone(), new_text).await?;
133 anyhow::Ok(())
134 })
135 .detach_and_log_err(cx);
136}