From 99257a82135078050075a6b7e6dd84d971bacb87 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Tue, 14 Mar 2023 09:46:28 -0700 Subject: [PATCH 1/2] Fix failed initialization of setting file in welcome experience --- crates/settings/src/settings_file.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/settings/src/settings_file.rs b/crates/settings/src/settings_file.rs index 50638205c519f5f882f963baeeb45c943e2895e6..3934467dcf69985867637fbf4a48b66e1973950d 100644 --- a/crates/settings/src/settings_file.rs +++ b/crates/settings/src/settings_file.rs @@ -1,8 +1,9 @@ use crate::{update_settings_file, watched_json::WatchedJsonFile, SettingsFileContent}; use anyhow::Result; +use assets::Assets; use fs::Fs; -use gpui::MutableAppContext; -use std::{path::Path, sync::Arc}; +use gpui::{AssetSource, MutableAppContext}; +use std::{io::ErrorKind, path::Path, sync::Arc}; // TODO: Switch SettingsFile to open a worktree and buffer for synchronization // And instant updates in the Zed editor @@ -39,7 +40,27 @@ impl SettingsFile { cx.background() .spawn(async move { - let old_text = fs.load(path).await?; + let old_text = match fs.load(path).await { + Ok(settings) => settings, + Err(err) => { + if let Ok(e) = err.downcast::() { + if e.kind() == ErrorKind::NotFound { + std::str::from_utf8( + Assets + .load("settings/initial_user_settings.json") + .unwrap() + .as_ref(), + ) + .unwrap() + .to_string() + } else { + anyhow::bail!("Failed to load settings"); + } + } else { + anyhow::bail!("Failed to load settings") + } + } + }; let new_text = update_settings_file(old_text, current_file_content, update); From d8a3f16891227099dd377468a6fa2be06fc65b8c Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Tue, 14 Mar 2023 09:55:02 -0700 Subject: [PATCH 2/2] Refactor load into a seperate function --- crates/settings/src/settings_file.rs | 43 ++++++++++++++-------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/crates/settings/src/settings_file.rs b/crates/settings/src/settings_file.rs index 3934467dcf69985867637fbf4a48b66e1973950d..4c01b3de726119974a1467654f80f23b94003327 100644 --- a/crates/settings/src/settings_file.rs +++ b/crates/settings/src/settings_file.rs @@ -27,6 +27,27 @@ impl SettingsFile { } } + async fn load_settings(path: &Path, fs: &Arc) -> Result { + match fs.load(path).await { + result @ Ok(_) => result, + Err(err) => { + if let Some(e) = err.downcast_ref::() { + if e.kind() == ErrorKind::NotFound { + return Ok(std::str::from_utf8( + Assets + .load("settings/initial_user_settings.json") + .unwrap() + .as_ref(), + ) + .unwrap() + .to_string()); + } + } + return Err(err); + } + } + } + pub fn update( cx: &mut MutableAppContext, update: impl 'static + Send + FnOnce(&mut SettingsFileContent), @@ -40,27 +61,7 @@ impl SettingsFile { cx.background() .spawn(async move { - let old_text = match fs.load(path).await { - Ok(settings) => settings, - Err(err) => { - if let Ok(e) = err.downcast::() { - if e.kind() == ErrorKind::NotFound { - std::str::from_utf8( - Assets - .load("settings/initial_user_settings.json") - .unwrap() - .as_ref(), - ) - .unwrap() - .to_string() - } else { - anyhow::bail!("Failed to load settings"); - } - } else { - anyhow::bail!("Failed to load settings") - } - } - }; + let old_text = SettingsFile::load_settings(path, &fs).await?; let new_text = update_settings_file(old_text, current_file_content, update);