Fix different kinds values used for worktree_id (#17523)

This commit is contained in:
Kirill Bulatov 2024-09-07 00:51:09 -04:00 committed by GitHub
parent 47aec5e64d
commit 8985fd87c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 136 additions and 127 deletions

View file

@ -6,7 +6,7 @@ mod settings_store;
use gpui::AppContext;
use rust_embed::RustEmbed;
use std::{borrow::Cow, str};
use std::{borrow::Cow, fmt, str};
use util::asset_str;
pub use editable_setting_control::*;
@ -15,6 +15,39 @@ pub use keymap_file::KeymapFile;
pub use settings_file::*;
pub use settings_store::{Settings, SettingsLocation, SettingsSources, SettingsStore};
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
pub struct WorktreeId(usize);
impl From<WorktreeId> for usize {
fn from(value: WorktreeId) -> Self {
value.0
}
}
impl WorktreeId {
pub fn from_usize(handle_id: usize) -> Self {
Self(handle_id)
}
pub fn from_proto(id: u64) -> Self {
Self(id as usize)
}
pub fn to_proto(&self) -> u64 {
self.0 as u64
}
pub fn to_usize(&self) -> usize {
self.0
}
}
impl fmt::Display for WorktreeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
#[derive(RustEmbed)]
#[folder = "../../assets"]
#[include = "settings/*"]

View file

@ -18,7 +18,7 @@ use tree_sitter::Query;
use tree_sitter_json::language;
use util::{merge_non_null_json_value_into, RangeExt, ResultExt as _};
use crate::SettingsJsonSchemaParams;
use crate::{SettingsJsonSchemaParams, WorktreeId};
/// A value that can be defined as a user setting.
///
@ -153,7 +153,7 @@ impl<'a, T: Serialize> SettingsSources<'a, T> {
#[derive(Clone, Copy, Debug)]
pub struct SettingsLocation<'a> {
pub worktree_id: usize,
pub worktree_id: WorktreeId,
pub path: &'a Path,
}
@ -163,7 +163,7 @@ pub struct SettingsStore {
raw_default_settings: serde_json::Value,
raw_user_settings: serde_json::Value,
raw_extension_settings: serde_json::Value,
raw_local_settings: BTreeMap<(usize, Arc<Path>), serde_json::Value>,
raw_local_settings: BTreeMap<(WorktreeId, Arc<Path>), serde_json::Value>,
tab_size_callback: Option<(
TypeId,
Box<dyn Fn(&dyn Any) -> Option<usize> + Send + Sync + 'static>,
@ -179,7 +179,7 @@ impl Global for SettingsStore {}
#[derive(Debug)]
struct SettingValue<T> {
global_value: Option<T>,
local_values: Vec<(usize, Arc<Path>, T)>,
local_values: Vec<(WorktreeId, Arc<Path>, T)>,
}
trait AnySettingValue: 'static + Send + Sync {
@ -193,7 +193,7 @@ trait AnySettingValue: 'static + Send + Sync {
) -> Result<Box<dyn Any>>;
fn value_for_path(&self, path: Option<SettingsLocation>) -> &dyn Any;
fn set_global_value(&mut self, value: Box<dyn Any>);
fn set_local_value(&mut self, root_id: usize, path: Arc<Path>, value: Box<dyn Any>);
fn set_local_value(&mut self, root_id: WorktreeId, path: Arc<Path>, value: Box<dyn Any>);
fn json_schema(
&self,
generator: &mut SchemaGenerator,
@ -519,7 +519,7 @@ impl SettingsStore {
/// Add or remove a set of local settings via a JSON string.
pub fn set_local_settings(
&mut self,
root_id: usize,
root_id: WorktreeId,
path: Arc<Path>,
settings_content: Option<&str>,
cx: &mut AppContext,
@ -552,15 +552,24 @@ 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: &mut AppContext) -> Result<()> {
pub fn clear_local_settings(&mut self, root_id: WorktreeId, cx: &mut AppContext) -> Result<()> {
self.raw_local_settings.retain(|k, _| k.0 != root_id);
self.recompute_values(Some((root_id, "".as_ref())), cx)?;
Ok(())
}
pub fn local_settings(&self, root_id: usize) -> impl '_ + Iterator<Item = (Arc<Path>, String)> {
pub fn local_settings(
&self,
root_id: WorktreeId,
) -> impl '_ + Iterator<Item = (Arc<Path>, String)> {
self.raw_local_settings
.range((root_id, Path::new("").into())..(root_id + 1, Path::new("").into()))
.range(
(root_id, Path::new("").into())
..(
WorktreeId::from_usize(root_id.to_usize() + 1),
Path::new("").into(),
),
)
.map(|((_, path), content)| (path.clone(), serde_json::to_string(content).unwrap()))
}
@ -673,12 +682,12 @@ impl SettingsStore {
fn recompute_values(
&mut self,
changed_local_path: Option<(usize, &Path)>,
changed_local_path: Option<(WorktreeId, &Path)>,
cx: &mut AppContext,
) -> Result<()> {
// Reload the global and local values for every setting.
let mut project_settings_stack = Vec::<DeserializedSetting>::new();
let mut paths_stack = Vec::<Option<(usize, &Path)>>::new();
let mut paths_stack = Vec::<Option<(WorktreeId, &Path)>>::new();
for setting_value in self.setting_values.values_mut() {
let default_settings = setting_value.deserialize_setting(&self.raw_default_settings)?;
@ -859,7 +868,7 @@ impl<T: Settings> AnySettingValue for SettingValue<T> {
self.global_value = Some(*value.downcast().unwrap());
}
fn set_local_value(&mut self, root_id: usize, path: Arc<Path>, value: Box<dyn Any>) {
fn set_local_value(&mut self, root_id: WorktreeId, path: Arc<Path>, value: Box<dyn Any>) {
let value = *value.downcast().unwrap();
match self
.local_values
@ -1153,7 +1162,7 @@ mod tests {
store
.set_local_settings(
1,
WorktreeId::from_usize(1),
Path::new("/root1").into(),
Some(r#"{ "user": { "staff": true } }"#),
cx,
@ -1161,7 +1170,7 @@ mod tests {
.unwrap();
store
.set_local_settings(
1,
WorktreeId::from_usize(1),
Path::new("/root1/subdir").into(),
Some(r#"{ "user": { "name": "Jane Doe" } }"#),
cx,
@ -1170,7 +1179,7 @@ mod tests {
store
.set_local_settings(
1,
WorktreeId::from_usize(1),
Path::new("/root2").into(),
Some(r#"{ "user": { "age": 42 }, "key2": "b" }"#),
cx,
@ -1179,7 +1188,7 @@ mod tests {
assert_eq!(
store.get::<UserSettings>(Some(SettingsLocation {
worktree_id: 1,
worktree_id: WorktreeId::from_usize(1),
path: Path::new("/root1/something"),
})),
&UserSettings {
@ -1190,7 +1199,7 @@ mod tests {
);
assert_eq!(
store.get::<UserSettings>(Some(SettingsLocation {
worktree_id: 1,
worktree_id: WorktreeId::from_usize(1),
path: Path::new("/root1/subdir/something")
})),
&UserSettings {
@ -1201,7 +1210,7 @@ mod tests {
);
assert_eq!(
store.get::<UserSettings>(Some(SettingsLocation {
worktree_id: 1,
worktree_id: WorktreeId::from_usize(1),
path: Path::new("/root2/something")
})),
&UserSettings {
@ -1212,7 +1221,7 @@ mod tests {
);
assert_eq!(
store.get::<MultiKeySettings>(Some(SettingsLocation {
worktree_id: 1,
worktree_id: WorktreeId::from_usize(1),
path: Path::new("/root2/something")
})),
&MultiKeySettings {