Remove 2 suffix for collab, rope, settings, menu

Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
Max Brunsfeld 2024-01-03 12:28:45 -08:00
parent 177e3028a9
commit 0cf65223ce
234 changed files with 3765 additions and 42229 deletions

View file

@ -13,7 +13,7 @@ test-support = ["gpui/test-support", "fs/test-support"]
[dependencies]
collections = { path = "../collections" }
gpui = { path = "../gpui" }
gpui = {package = "gpui2", path = "../gpui2" }
sqlez = { path = "../sqlez" }
fs = { path = "../fs" }
feature_flags = { path = "../feature_flags" }
@ -35,7 +35,7 @@ tree-sitter.workspace = true
tree-sitter-json = "*"
[dev-dependencies]
gpui = { path = "../gpui", features = ["test-support"] }
gpui = {package = "gpui2", path = "../gpui2", features = ["test-support"] }
fs = { path = "../fs", features = ["test-support"] }
indoc.workspace = true
pretty_assertions.workspace = true

View file

@ -1,7 +1,7 @@
use crate::{settings_store::parse_json_with_comments, SettingsAssets};
use anyhow::{anyhow, Context, Result};
use collections::BTreeMap;
use gpui::{keymap_matcher::Binding, AppContext, NoAction};
use gpui::{Action, AppContext, KeyBinding, SharedString};
use schemars::{
gen::{SchemaGenerator, SchemaSettings},
schema::{InstanceType, Schema, SchemaObject, SingleOrVec, SubschemaValidation},
@ -73,9 +73,9 @@ impl KeymapFile {
"Expected first item in array to be a string."
)));
};
cx.deserialize_action(&name, Some(data))
cx.build_action(&name, Some(data))
}
Value::String(name) => cx.deserialize_action(&name, None),
Value::String(name) => cx.build_action(&name, None),
Value::Null => Ok(no_action()),
_ => {
return Some(Err(anyhow!("Expected two-element array, got {action:?}")))
@ -87,16 +87,16 @@ impl KeymapFile {
)
})
.log_err()
.map(|action| Binding::load(&keystroke, action, context.as_deref()))
.map(|action| KeyBinding::load(&keystroke, action, context.as_deref()))
})
.collect::<Result<Vec<_>>>()?;
cx.add_bindings(bindings);
cx.bind_keys(bindings);
}
Ok(())
}
pub fn generate_json_schema(action_names: &[&'static str]) -> serde_json::Value {
pub fn generate_json_schema(action_names: &[SharedString]) -> serde_json::Value {
let mut root_schema = SchemaSettings::draft07()
.with(|settings| settings.option_add_null_type = false)
.into_generator()
@ -138,7 +138,7 @@ impl KeymapFile {
}
fn no_action() -> Box<dyn gpui::Action> {
Box::new(NoAction {})
gpui::NoAction.boxed_clone()
}
#[cfg(test)]

View file

@ -8,7 +8,7 @@ use util::asset_str;
pub use keymap_file::KeymapFile;
pub use settings_file::*;
pub use settings_store::{Setting, SettingsJsonSchemaParams, SettingsStore};
pub use settings_store::{Settings, SettingsJsonSchemaParams, SettingsStore};
#[derive(RustEmbed)]
#[folder = "../../assets"]

View file

@ -1,31 +1,11 @@
use crate::{settings_store::SettingsStore, Setting};
use crate::{settings_store::SettingsStore, KeymapFile, Settings};
use anyhow::Result;
use fs::Fs;
use futures::{channel::mpsc, StreamExt};
use gpui::{executor::Background, AppContext};
use std::{
io::ErrorKind,
path::{Path, PathBuf},
str,
sync::Arc,
time::Duration,
};
use gpui::{AppContext, BackgroundExecutor};
use std::{io::ErrorKind, path::PathBuf, str, sync::Arc, time::Duration};
use util::{paths, ResultExt};
pub fn register<T: Setting>(cx: &mut AppContext) {
cx.update_global::<SettingsStore, _, _>(|store, cx| {
store.register_setting::<T>(cx);
});
}
pub fn get<'a, T: Setting>(cx: &'a AppContext) -> &'a T {
cx.global::<SettingsStore>().get(None)
}
pub fn get_local<'a, T: Setting>(location: Option<(usize, &Path)>, cx: &'a AppContext) -> &'a T {
cx.global::<SettingsStore>().get(location)
}
pub const EMPTY_THEME_NAME: &'static str = "empty-theme";
#[cfg(any(test, feature = "test-support"))]
@ -36,6 +16,9 @@ pub fn test_settings() -> String {
.unwrap();
util::merge_non_null_json_value_into(
serde_json::json!({
"ui_font_family": "Courier",
"ui_font_features": {},
"ui_font_size": 14,
"buffer_font_family": "Courier",
"buffer_font_features": {},
"buffer_font_size": 14,
@ -48,7 +31,7 @@ pub fn test_settings() -> String {
}
pub fn watch_config_file(
executor: Arc<Background>,
executor: &BackgroundExecutor,
fs: Arc<dyn Fs>,
path: PathBuf,
) -> mpsc::UnboundedReceiver<String> {
@ -83,22 +66,27 @@ pub fn handle_settings_file_changes(
mut user_settings_file_rx: mpsc::UnboundedReceiver<String>,
cx: &mut AppContext,
) {
let user_settings_content = cx.background().block(user_settings_file_rx.next()).unwrap();
cx.update_global::<SettingsStore, _, _>(|store, cx| {
let user_settings_content = cx
.background_executor()
.block(user_settings_file_rx.next())
.unwrap();
cx.update_global(|store: &mut SettingsStore, cx| {
store
.set_user_settings(&user_settings_content, cx)
.log_err();
});
cx.spawn(move |mut cx| async move {
while let Some(user_settings_content) = user_settings_file_rx.next().await {
cx.update(|cx| {
cx.update_global::<SettingsStore, _, _>(|store, cx| {
store
.set_user_settings(&user_settings_content, cx)
.log_err();
});
cx.refresh_windows();
eprintln!("settings file changed");
let result = cx.update_global(|store: &mut SettingsStore, cx| {
store
.set_user_settings(&user_settings_content, cx)
.log_err();
cx.refresh();
});
if result.is_err() {
break; // App dropped
}
}
})
.detach();
@ -118,29 +106,29 @@ async fn load_settings(fs: &Arc<dyn Fs>) -> Result<String> {
}
}
pub fn update_settings_file<T: Setting>(
pub fn update_settings_file<T: Settings>(
fs: Arc<dyn Fs>,
cx: &mut AppContext,
update: impl 'static + Send + FnOnce(&mut T::FileContent),
) {
cx.spawn(|cx| async move {
let old_text = cx
.background()
.spawn({
let fs = fs.clone();
async move { load_settings(&fs).await }
})
.await?;
let new_text = cx.read(|cx| {
cx.global::<SettingsStore>()
.new_text_for_update::<T>(old_text, update)
});
cx.background()
.spawn(async move { fs.atomic_write(paths::SETTINGS.clone(), new_text).await })
.await?;
let old_text = load_settings(&fs).await?;
let new_text = cx.read_global(|store: &SettingsStore, _cx| {
store.new_text_for_update::<T>(old_text, update)
})?;
fs.atomic_write(paths::SETTINGS.clone(), new_text).await?;
anyhow::Ok(())
})
.detach_and_log_err(cx);
}
pub fn load_default_keymap(cx: &mut AppContext) {
for path in ["keymaps/default.json", "keymaps/vim.json"] {
KeymapFile::load_asset(path, cx).unwrap();
}
// todo!()
// if let Some(asset_path) = settings::get::<BaseKeymap>(cx).asset_path() {
// KeymapFile::load_asset(asset_path, cx).unwrap();
// }
}

View file

@ -18,7 +18,7 @@ use util::{merge_non_null_json_value_into, RangeExt, ResultExt as _};
/// A value that can be defined as a user setting.
///
/// Settings can be loaded from a combination of multiple JSON files.
pub trait Setting: 'static {
pub trait Settings: 'static + Send + Sync {
/// The name of a key within the JSON file from which this setting should
/// be deserialized. If this is `None`, then the setting will be deserialized
/// from the root object.
@ -35,7 +35,7 @@ pub trait Setting: 'static {
fn load(
default_value: &Self::FileContent,
user_values: &[&Self::FileContent],
cx: &AppContext,
cx: &mut AppContext,
) -> Result<Self>
where
Self: Sized;
@ -76,6 +76,39 @@ pub trait Setting: 'static {
fn missing_default() -> anyhow::Error {
anyhow::anyhow!("missing default")
}
fn register(cx: &mut AppContext)
where
Self: Sized,
{
cx.update_global(|store: &mut SettingsStore, cx| {
store.register_setting::<Self>(cx);
});
}
#[track_caller]
fn get<'a>(path: Option<(usize, &Path)>, cx: &'a AppContext) -> &'a Self
where
Self: Sized,
{
cx.global::<SettingsStore>().get(path)
}
#[track_caller]
fn get_global<'a>(cx: &'a AppContext) -> &'a Self
where
Self: Sized,
{
cx.global::<SettingsStore>().get(None)
}
#[track_caller]
fn override_global<'a>(settings: Self, cx: &'a mut AppContext)
where
Self: Sized,
{
cx.global_mut::<SettingsStore>().override_global(settings)
}
}
pub struct SettingsJsonSchemaParams<'a> {
@ -89,7 +122,10 @@ pub struct SettingsStore {
raw_default_settings: serde_json::Value,
raw_user_settings: serde_json::Value,
raw_local_settings: BTreeMap<(usize, Arc<Path>), serde_json::Value>,
tab_size_callback: Option<(TypeId, Box<dyn Fn(&dyn Any) -> Option<usize>>)>,
tab_size_callback: Option<(
TypeId,
Box<dyn Fn(&dyn Any) -> Option<usize> + Send + Sync + 'static>,
)>,
}
impl Default for SettingsStore {
@ -110,7 +146,7 @@ struct SettingValue<T> {
local_values: Vec<(usize, Arc<Path>, T)>,
}
trait AnySettingValue {
trait AnySettingValue: 'static + Send + Sync {
fn key(&self) -> Option<&'static str>;
fn setting_type_name(&self) -> &'static str;
fn deserialize_setting(&self, json: &serde_json::Value) -> Result<DeserializedSetting>;
@ -118,7 +154,7 @@ trait AnySettingValue {
&self,
default_value: &DeserializedSetting,
custom: &[DeserializedSetting],
cx: &AppContext,
cx: &mut AppContext,
) -> Result<Box<dyn Any>>;
fn value_for_path(&self, path: Option<(usize, &Path)>) -> &dyn Any;
fn set_global_value(&mut self, value: Box<dyn Any>);
@ -135,7 +171,7 @@ struct DeserializedSetting(Box<dyn Any>);
impl SettingsStore {
/// Add a new type of setting to the store.
pub fn register_setting<T: Setting>(&mut self, cx: &AppContext) {
pub fn register_setting<T: Settings>(&mut self, cx: &mut AppContext) {
let setting_type_id = TypeId::of::<T>();
let entry = self.setting_values.entry(setting_type_id);
if matches!(entry, hash_map::Entry::Occupied(_)) {
@ -174,7 +210,7 @@ impl SettingsStore {
///
/// Panics if the given setting type has not been registered, or if there is no
/// value for this setting.
pub fn get<T: Setting>(&self, path: Option<(usize, &Path)>) -> &T {
pub fn get<T: Settings>(&self, path: Option<(usize, &Path)>) -> &T {
self.setting_values
.get(&TypeId::of::<T>())
.unwrap_or_else(|| panic!("unregistered setting type {}", type_name::<T>()))
@ -186,7 +222,7 @@ impl SettingsStore {
/// Override the global value for a setting.
///
/// The given value will be overwritten if the user settings file changes.
pub fn override_global<T: Setting>(&mut self, value: T) {
pub fn override_global<T: Settings>(&mut self, value: T) {
self.setting_values
.get_mut(&TypeId::of::<T>())
.unwrap_or_else(|| panic!("unregistered setting type {}", type_name::<T>()))
@ -202,7 +238,7 @@ impl SettingsStore {
}
#[cfg(any(test, feature = "test-support"))]
pub fn test(cx: &AppContext) -> Self {
pub fn test(cx: &mut AppContext) -> Self {
let mut this = Self::default();
this.set_default_settings(&crate::test_settings(), cx)
.unwrap();
@ -215,9 +251,9 @@ impl SettingsStore {
/// This is only for tests. Normally, settings are only loaded from
/// JSON files.
#[cfg(any(test, feature = "test-support"))]
pub fn update_user_settings<T: Setting>(
pub fn update_user_settings<T: Settings>(
&mut self,
cx: &AppContext,
cx: &mut AppContext,
update: impl FnOnce(&mut T::FileContent),
) {
let old_text = serde_json::to_string(&self.raw_user_settings).unwrap();
@ -227,7 +263,7 @@ impl SettingsStore {
/// Update the value of a setting in a JSON file, returning the new text
/// for that JSON file.
pub fn new_text_for_update<T: Setting>(
pub fn new_text_for_update<T: Settings>(
&self,
old_text: String,
update: impl FnOnce(&mut T::FileContent),
@ -242,7 +278,7 @@ impl SettingsStore {
/// Update the value of a setting in a JSON file, returning a list
/// of edits to apply to the JSON file.
pub fn edits_for_update<T: Setting>(
pub fn edits_for_update<T: Settings>(
&self,
text: &str,
update: impl FnOnce(&mut T::FileContent),
@ -284,7 +320,7 @@ impl SettingsStore {
}
/// Configure the tab sized when updating JSON files.
pub fn set_json_tab_size_callback<T: Setting>(
pub fn set_json_tab_size_callback<T: Settings>(
&mut self,
get_tab_size: fn(&T) -> Option<usize>,
) {
@ -314,7 +350,7 @@ impl SettingsStore {
pub fn set_default_settings(
&mut self,
default_settings_content: &str,
cx: &AppContext,
cx: &mut AppContext,
) -> Result<()> {
let settings: serde_json::Value = parse_json_with_comments(default_settings_content)?;
if settings.is_object() {
@ -330,7 +366,7 @@ impl SettingsStore {
pub fn set_user_settings(
&mut self,
user_settings_content: &str,
cx: &AppContext,
cx: &mut AppContext,
) -> Result<()> {
let settings: serde_json::Value = parse_json_with_comments(user_settings_content)?;
if settings.is_object() {
@ -348,7 +384,7 @@ impl SettingsStore {
root_id: usize,
path: Arc<Path>,
settings_content: Option<&str>,
cx: &AppContext,
cx: &mut AppContext,
) -> Result<()> {
if let Some(content) = settings_content {
self.raw_local_settings
@ -361,7 +397,7 @@ impl SettingsStore {
}
/// Add or remove a set of local settings via a JSON string.
pub fn clear_local_settings(&mut self, root_id: usize, cx: &AppContext) -> Result<()> {
pub fn clear_local_settings(&mut self, root_id: usize, cx: &mut AppContext) -> Result<()> {
self.raw_local_settings.retain(|k, _| k.0 != root_id);
self.recompute_values(Some((root_id, "".as_ref())), cx)?;
Ok(())
@ -453,7 +489,7 @@ impl SettingsStore {
fn recompute_values(
&mut self,
changed_local_path: Option<(usize, &Path)>,
cx: &AppContext,
cx: &mut AppContext,
) -> Result<()> {
// Reload the global and local values for every setting.
let mut user_settings_stack = Vec::<DeserializedSetting>::new();
@ -541,7 +577,7 @@ impl Debug for SettingsStore {
}
}
impl<T: Setting> AnySettingValue for SettingValue<T> {
impl<T: Settings> AnySettingValue for SettingValue<T> {
fn key(&self) -> Option<&'static str> {
T::KEY
}
@ -554,7 +590,7 @@ impl<T: Setting> AnySettingValue for SettingValue<T> {
&self,
default_value: &DeserializedSetting,
user_values: &[DeserializedSetting],
cx: &AppContext,
cx: &mut AppContext,
) -> Result<Box<dyn Any>> {
let default_value = default_value.0.downcast_ref::<T::FileContent>().unwrap();
let values: SmallVec<[&T::FileContent; 6]> = user_values
@ -837,6 +873,7 @@ fn to_pretty_json(value: &impl Serialize, indent_size: usize, indent_prefix_len:
pub fn parse_json_with_comments<T: DeserializeOwned>(content: &str) -> Result<T> {
Ok(serde_json_lenient::from_str(content)?)
}
#[cfg(test)]
mod tests {
use super::*;
@ -1123,7 +1160,7 @@ mod tests {
);
}
fn check_settings_update<T: Setting>(
fn check_settings_update<T: Settings>(
store: &mut SettingsStore,
old_json: String,
update: fn(&mut T::FileContent),
@ -1153,14 +1190,14 @@ mod tests {
staff: Option<bool>,
}
impl Setting for UserSettings {
impl Settings for UserSettings {
const KEY: Option<&'static str> = Some("user");
type FileContent = UserSettingsJson;
fn load(
default_value: &UserSettingsJson,
user_values: &[&UserSettingsJson],
_: &AppContext,
_: &mut AppContext,
) -> Result<Self> {
Self::load_via_json_merge(default_value, user_values)
}
@ -1169,14 +1206,14 @@ mod tests {
#[derive(Debug, Deserialize, PartialEq)]
struct TurboSetting(bool);
impl Setting for TurboSetting {
impl Settings for TurboSetting {
const KEY: Option<&'static str> = Some("turbo");
type FileContent = Option<bool>;
fn load(
default_value: &Option<bool>,
user_values: &[&Option<bool>],
_: &AppContext,
_: &mut AppContext,
) -> Result<Self> {
Self::load_via_json_merge(default_value, user_values)
}
@ -1196,7 +1233,7 @@ mod tests {
key2: Option<String>,
}
impl Setting for MultiKeySettings {
impl Settings for MultiKeySettings {
const KEY: Option<&'static str> = None;
type FileContent = MultiKeySettingsJson;
@ -1204,7 +1241,7 @@ mod tests {
fn load(
default_value: &MultiKeySettingsJson,
user_values: &[&MultiKeySettingsJson],
_: &AppContext,
_: &mut AppContext,
) -> Result<Self> {
Self::load_via_json_merge(default_value, user_values)
}
@ -1229,7 +1266,7 @@ mod tests {
pub hour_format: Option<HourFormat>,
}
impl Setting for JournalSettings {
impl Settings for JournalSettings {
const KEY: Option<&'static str> = Some("journal");
type FileContent = JournalSettingsJson;
@ -1237,7 +1274,7 @@ mod tests {
fn load(
default_value: &JournalSettingsJson,
user_values: &[&JournalSettingsJson],
_: &AppContext,
_: &mut AppContext,
) -> Result<Self> {
Self::load_via_json_merge(default_value, user_values)
}
@ -1255,12 +1292,12 @@ mod tests {
language_setting_2: Option<bool>,
}
impl Setting for LanguageSettings {
impl Settings for LanguageSettings {
const KEY: Option<&'static str> = None;
type FileContent = Self;
fn load(default_value: &Self, user_values: &[&Self], _: &AppContext) -> Result<Self> {
fn load(default_value: &Self, user_values: &[&Self], _: &mut AppContext) -> Result<Self> {
Self::load_via_json_merge(default_value, user_values)
}
}