From 296a0bf51060eab6c8eaf1b530f529ee62bce04a Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 5 Jun 2023 18:09:42 -0700 Subject: [PATCH] Populate created local settings file with an empty JSON object and comments --- assets/settings/initial_local_settings.json | 11 +++++++++ crates/collab/src/rpc.rs | 4 +-- crates/settings/src/settings.rs | 19 ++++++++++++--- crates/settings/src/settings_file.rs | 14 +++-------- crates/zed/src/zed.rs | 27 ++++++++++++++++++--- 5 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 assets/settings/initial_local_settings.json diff --git a/assets/settings/initial_local_settings.json b/assets/settings/initial_local_settings.json new file mode 100644 index 0000000000000000000000000000000000000000..69be683aa81d758e114f02b9bf41d45dcfe32d81 --- /dev/null +++ b/assets/settings/initial_local_settings.json @@ -0,0 +1,11 @@ +// Folder-specific Zed settings +// +// A subset of Zed's settings can be configured on a per-folder basis. +// +// For information on how to configure Zed, see the Zed +// documentation: https://zed.dev/docs/configuring-zed +// +// To see all of Zed's default settings without changing your +// custom settings, run the `open default settings` command +// from the command palette or from `Zed` application menu. +{} diff --git a/crates/collab/src/rpc.rs b/crates/collab/src/rpc.rs index 61c6123a827e897d9bf8c5db15e9e79fdbd4b951..8d210513c2d3dc32fdac676b4953147cc4f0208e 100644 --- a/crates/collab/src/rpc.rs +++ b/crates/collab/src/rpc.rs @@ -1424,7 +1424,7 @@ async fn join_project( )?; } - for settings_file in dbg!(worktree.settings_files) { + for settings_file in worktree.settings_files { session.peer.send( session.connection_id, proto::UpdateWorktreeSettings { @@ -1554,8 +1554,6 @@ async fn update_worktree_settings( message: proto::UpdateWorktreeSettings, session: Session, ) -> Result<()> { - dbg!(&message); - let guest_connection_ids = session .db() .await diff --git a/crates/settings/src/settings.rs b/crates/settings/src/settings.rs index 840797c6ad644cc851c298712e7db080a0650d78..1c131e5916c5d11746881d729cf93a254d0effd0 100644 --- a/crates/settings/src/settings.rs +++ b/crates/settings/src/settings.rs @@ -9,10 +9,23 @@ pub use settings_store::{Setting, SettingsJsonSchemaParams, SettingsStore}; use std::{borrow::Cow, str}; pub const DEFAULT_SETTINGS_ASSET_PATH: &str = "settings/default.json"; -pub const INITIAL_USER_SETTINGS_ASSET_PATH: &str = "settings/initial_user_settings.json"; +const INITIAL_USER_SETTINGS_ASSET_PATH: &str = "settings/initial_user_settings.json"; +const INITIAL_LOCAL_SETTINGS_ASSET_PATH: &str = "settings/initial_local_settings.json"; -pub fn initial_user_settings_content(assets: &'static impl AssetSource) -> Cow<'static, str> { - match assets.load(INITIAL_USER_SETTINGS_ASSET_PATH).unwrap() { +pub fn default_settings() -> Cow<'static, str> { + asset_str(&assets::Assets, DEFAULT_SETTINGS_ASSET_PATH) +} + +pub fn initial_user_settings_content(assets: &dyn AssetSource) -> Cow<'_, str> { + asset_str(assets, INITIAL_USER_SETTINGS_ASSET_PATH) +} + +pub fn initial_local_settings_content(assets: &dyn AssetSource) -> Cow<'_, str> { + asset_str(assets, INITIAL_LOCAL_SETTINGS_ASSET_PATH) +} + +fn asset_str<'a>(assets: &'a dyn AssetSource, path: &str) -> Cow<'a, str> { + match assets.load(path).unwrap() { Cow::Borrowed(s) => Cow::Borrowed(str::from_utf8(s).unwrap()), Cow::Owned(s) => Cow::Owned(String::from_utf8(s).unwrap()), } diff --git a/crates/settings/src/settings_file.rs b/crates/settings/src/settings_file.rs index 3505330eda6fb94cc4ded9d90f313c40a11e7866..edd4fe0d9dea93cb7444656772311f6dbd439e9b 100644 --- a/crates/settings/src/settings_file.rs +++ b/crates/settings/src/settings_file.rs @@ -1,11 +1,10 @@ -use crate::{settings_store::SettingsStore, Setting, DEFAULT_SETTINGS_ASSET_PATH}; +use crate::{settings_store::SettingsStore, Setting}; use anyhow::Result; use assets::Assets; use fs::Fs; use futures::{channel::mpsc, StreamExt}; -use gpui::{executor::Background, AppContext, AssetSource}; +use gpui::{executor::Background, AppContext}; use std::{ - borrow::Cow, io::ErrorKind, path::{Path, PathBuf}, str, @@ -28,19 +27,12 @@ pub fn get_local<'a, T: Setting>(location: Option<(usize, &Path)>, cx: &'a AppCo cx.global::().get(location) } -pub fn default_settings() -> Cow<'static, str> { - match Assets.load(DEFAULT_SETTINGS_ASSET_PATH).unwrap() { - Cow::Borrowed(s) => Cow::Borrowed(str::from_utf8(s).unwrap()), - Cow::Owned(s) => Cow::Owned(String::from_utf8(s).unwrap()), - } -} - pub const EMPTY_THEME_NAME: &'static str = "empty-theme"; #[cfg(any(test, feature = "test-support"))] pub fn test_settings() -> String { let mut value = crate::settings_store::parse_json_with_comments::( - default_settings().as_ref(), + crate::default_settings().as_ref(), ) .unwrap(); util::merge_non_null_json_value_into( diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index ae9370384ae4b8f5ee669f52cbeab58aa699591e..f0e88133f3e20c668b2c9c86108ed3abfe1f712e 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -30,7 +30,9 @@ use project_panel::ProjectPanel; use search::{BufferSearchBar, ProjectSearchBar}; use serde::Deserialize; use serde_json::to_string_pretty; -use settings::{KeymapFileContent, SettingsStore, DEFAULT_SETTINGS_ASSET_PATH}; +use settings::{ + initial_local_settings_content, KeymapFileContent, SettingsStore, DEFAULT_SETTINGS_ASSET_PATH, +}; use std::{borrow::Cow, str, sync::Arc}; use terminal_view::terminal_panel::{self, TerminalPanel}; use util::{ @@ -596,11 +598,30 @@ fn open_local_settings_file( .await?; } - workspace + let editor = workspace .update(&mut cx, |workspace, cx| { workspace.open_path((tree_id, file_path), None, true, cx) })? - .await?; + .await? + .downcast::() + .ok_or_else(|| anyhow!("unexpected item type"))?; + + editor + .downgrade() + .update(&mut cx, |editor, cx| { + if let Some(buffer) = editor.buffer().read(cx).as_singleton() { + if buffer.read(cx).is_empty() { + buffer.update(cx, |buffer, cx| { + buffer.edit( + [(0..0, initial_local_settings_content(&Assets))], + None, + cx, + ) + }); + } + } + }) + .ok(); anyhow::Ok(()) })