1use crate::{settings_store::SettingsStore, KeymapFile, Settings};
2use anyhow::Result;
3use fs::Fs;
4use futures::{channel::mpsc, StreamExt};
5use gpui::{AppContext, BackgroundExecutor};
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: &BackgroundExecutor,
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
67 .background_executor()
68 .block(user_settings_file_rx.next())
69 .unwrap();
70 cx.update_global(|store: &mut SettingsStore, cx| {
71 store
72 .set_user_settings(&user_settings_content, cx)
73 .log_err();
74 });
75 cx.spawn(move |mut cx| async move {
76 while let Some(user_settings_content) = user_settings_file_rx.next().await {
77 let result = cx.update_global(|store: &mut SettingsStore, cx| {
78 store
79 .set_user_settings(&user_settings_content, cx)
80 .log_err();
81 cx.refresh();
82 });
83 if result.is_err() {
84 break; // App dropped
85 }
86 }
87 })
88 .detach();
89}
90
91async fn load_settings(fs: &Arc<dyn Fs>) -> Result<String> {
92 match fs.load(&paths::SETTINGS).await {
93 result @ Ok(_) => result,
94 Err(err) => {
95 if let Some(e) = err.downcast_ref::<std::io::Error>() {
96 if e.kind() == ErrorKind::NotFound {
97 return Ok(crate::initial_user_settings_content().to_string());
98 }
99 }
100 return Err(err);
101 }
102 }
103}
104
105pub fn update_settings_file<T: Settings>(
106 fs: Arc<dyn Fs>,
107 cx: &mut AppContext,
108 update: impl 'static + Send + FnOnce(&mut T::FileContent),
109) {
110 cx.spawn(|cx| async move {
111 let old_text = load_settings(&fs).await?;
112 let new_text = cx.read_global(|store: &SettingsStore, _cx| {
113 store.new_text_for_update::<T>(old_text, update)
114 })?;
115 fs.atomic_write(paths::SETTINGS.clone(), new_text).await?;
116 anyhow::Ok(())
117 })
118 .detach_and_log_err(cx);
119}
120
121pub fn load_default_keymap(cx: &mut AppContext) {
122 for path in ["keymaps/default.json", "keymaps/vim.json"] {
123 KeymapFile::load_asset(path, cx).unwrap();
124 }
125
126 // todo!()
127 // if let Some(asset_path) = settings::get::<BaseKeymap>(cx).asset_path() {
128 // KeymapFile::load_asset(asset_path, cx).unwrap();
129 // }
130}
131
132pub fn handle_keymap_file_changes(
133 mut user_keymap_file_rx: mpsc::UnboundedReceiver<String>,
134 cx: &mut AppContext,
135) {
136 cx.spawn(move |cx| async move {
137 // let mut settings_subscription = None;
138 while let Some(user_keymap_content) = user_keymap_file_rx.next().await {
139 if let Some(keymap_content) = KeymapFile::parse(&user_keymap_content).log_err() {
140 cx.update(|cx| reload_keymaps(cx, &keymap_content)).ok();
141
142 // todo!()
143 // let mut old_base_keymap = cx.read(|cx| *settings::get::<BaseKeymap>(cx));
144 // drop(settings_subscription);
145 // settings_subscription = Some(cx.update(|cx| {
146 // cx.observe_global::<SettingsStore, _>(move |cx| {
147 // let new_base_keymap = *settings::get::<BaseKeymap>(cx);
148 // if new_base_keymap != old_base_keymap {
149 // old_base_keymap = new_base_keymap.clone();
150 // reload_keymaps(cx, &keymap_content);
151 // }
152 // })
153 // }));
154 }
155 }
156 })
157 .detach();
158}
159
160fn reload_keymaps(cx: &mut AppContext, keymap_content: &KeymapFile) {
161 // todo!()
162 // cx.clear_bindings();
163 load_default_keymap(cx);
164 keymap_content.clone().add_to_cx(cx).log_err();
165 // cx.set_menus(menus::menus());
166}