1/// Trait for recursively merging settings structures.
2///
3/// When Zed starts it loads settings from `default.json` to initialize
4/// everything. These may be further refined by loading the user's settings,
5/// and any settings profiles; and then further refined by loading any
6/// local project settings.
7///
8/// The default behaviour of merging is:
9/// * For objects with named keys (HashMap, structs, etc.). The values are merged deeply
10/// (so if the default settings has languages.JSON.prettier.allowed = true, and the user's settings has
11/// languages.JSON.tab_size = 4; the merged settings file will have both settings).
12/// * For options, a None value is ignored, but Some values are merged recursively.
13/// * For other types (including Vec), a merge overwrites the current value.
14///
15/// If you want to break the rules you can (e.g. ExtendingVec, or SaturatingBool).
16#[allow(unused)]
17pub trait MergeFrom {
18 /// Merge from a source of the same type.
19 fn merge_from(&mut self, other: &Self);
20
21 /// Merge from an optional source of the same type.
22 fn merge_from_option(&mut self, other: Option<&Self>) {
23 if let Some(other) = other {
24 self.merge_from(other);
25 }
26 }
27}
28
29macro_rules! merge_from_overwrites {
30 ($($type:ty),+) => {
31 $(
32 impl MergeFrom for $type {
33 fn merge_from(&mut self, other: &Self) {
34 *self = other.clone();
35 }
36 }
37 )+
38 }
39}
40
41merge_from_overwrites!(
42 u16,
43 u32,
44 u64,
45 usize,
46 i16,
47 i32,
48 i64,
49 bool,
50 f64,
51 f32,
52 char,
53 std::num::NonZeroUsize,
54 std::num::NonZeroU32,
55 String,
56 std::sync::Arc<str>,
57 gpui::SharedString,
58 std::path::PathBuf,
59 std::sync::Arc<std::path::Path>,
60 gpui::Modifiers,
61 gpui::FontFeatures,
62 gpui::FontWeight
63);
64
65impl<T: Clone + MergeFrom> MergeFrom for Option<T> {
66 fn merge_from(&mut self, other: &Self) {
67 let Some(other) = other else {
68 return;
69 };
70 if let Some(this) = self {
71 this.merge_from(other);
72 } else {
73 self.replace(other.clone());
74 }
75 }
76}
77
78impl<T: Clone> MergeFrom for Vec<T> {
79 fn merge_from(&mut self, other: &Self) {
80 *self = other.clone()
81 }
82}
83
84impl<T: MergeFrom> MergeFrom for Box<T> {
85 fn merge_from(&mut self, other: &Self) {
86 self.as_mut().merge_from(other.as_ref())
87 }
88}
89
90// Implementations for collections that extend/merge their contents
91impl<K, V> MergeFrom for collections::HashMap<K, V>
92where
93 K: Clone + std::hash::Hash + Eq,
94 V: Clone + MergeFrom,
95{
96 fn merge_from(&mut self, other: &Self) {
97 for (k, v) in other {
98 if let Some(existing) = self.get_mut(k) {
99 existing.merge_from(v);
100 } else {
101 self.insert(k.clone(), v.clone());
102 }
103 }
104 }
105}
106
107impl<K, V> MergeFrom for collections::BTreeMap<K, V>
108where
109 K: Clone + std::hash::Hash + Eq + Ord,
110 V: Clone + MergeFrom,
111{
112 fn merge_from(&mut self, other: &Self) {
113 for (k, v) in other {
114 if let Some(existing) = self.get_mut(k) {
115 existing.merge_from(v);
116 } else {
117 self.insert(k.clone(), v.clone());
118 }
119 }
120 }
121}
122
123impl<K, V> MergeFrom for collections::IndexMap<K, V>
124where
125 K: std::hash::Hash + Eq + Clone,
126 // Q: ?Sized + std::hash::Hash + collections::Equivalent<K> + Eq,
127 V: Clone + MergeFrom,
128{
129 fn merge_from(&mut self, other: &Self) {
130 for (k, v) in other {
131 if let Some(existing) = self.get_mut(k) {
132 existing.merge_from(v);
133 } else {
134 self.insert(k.clone(), v.clone());
135 }
136 }
137 }
138}
139
140impl<T> MergeFrom for collections::BTreeSet<T>
141where
142 T: Clone + Ord,
143{
144 fn merge_from(&mut self, other: &Self) {
145 for item in other {
146 self.insert(item.clone());
147 }
148 }
149}
150
151impl<T> MergeFrom for collections::HashSet<T>
152where
153 T: Clone + std::hash::Hash + Eq,
154{
155 fn merge_from(&mut self, other: &Self) {
156 for item in other {
157 self.insert(item.clone());
158 }
159 }
160}
161
162impl MergeFrom for serde_json::Value {
163 fn merge_from(&mut self, other: &Self) {
164 match (self, other) {
165 (serde_json::Value::Object(this), serde_json::Value::Object(other)) => {
166 for (k, v) in other {
167 if let Some(existing) = this.get_mut(k) {
168 existing.merge_from(v);
169 } else {
170 this.insert(k.clone(), v.clone());
171 }
172 }
173 }
174 (this, other) => *this = other.clone(),
175 }
176 }
177}