1use proc_macro::TokenStream;
2use quote::{format_ident, quote};
3use syn::{Data, DeriveInput, Fields, Ident, parse_macro_input};
4
5/// Derives the `MergeFrom` trait for a struct.
6///
7/// This macro automatically implements `MergeFrom` by calling `merge_from`
8/// on all fields in the struct.
9///
10/// # Example
11///
12/// ```ignore
13/// #[derive(Clone, MergeFrom)]
14/// struct MySettings {
15/// field1: Option<String>,
16/// field2: SomeOtherSettings,
17/// }
18/// ```
19#[proc_macro_derive(MergeFrom)]
20pub fn derive_merge_from(input: TokenStream) -> TokenStream {
21 let input = parse_macro_input!(input as DeriveInput);
22
23 let name = &input.ident;
24 let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
25
26 let merge_body = match &input.data {
27 Data::Struct(data_struct) => match &data_struct.fields {
28 Fields::Named(fields) => {
29 let field_merges = fields.named.iter().map(|field| {
30 let field_name = &field.ident;
31 quote! {
32 self.#field_name.merge_from(&other.#field_name);
33 }
34 });
35
36 quote! {
37 #(#field_merges)*
38 }
39 }
40 Fields::Unnamed(fields) => {
41 let field_merges = fields.unnamed.iter().enumerate().map(|(i, _)| {
42 let field_index = syn::Index::from(i);
43 quote! {
44 self.#field_index.merge_from(&other.#field_index);
45 }
46 });
47
48 quote! {
49 #(#field_merges)*
50 }
51 }
52 Fields::Unit => {
53 quote! {
54 // No fields to merge for unit structs
55 }
56 }
57 },
58 Data::Enum(_) => {
59 quote! {
60 *self = other.clone();
61 }
62 }
63 Data::Union(_) => {
64 panic!("MergeFrom cannot be derived for unions");
65 }
66 };
67
68 let expanded = quote! {
69 impl #impl_generics crate::merge_from::MergeFrom for #name #ty_generics #where_clause {
70 fn merge_from(&mut self, other: &Self) {
71 use crate::merge_from::MergeFrom as _;
72 #merge_body
73 }
74 }
75 };
76
77 TokenStream::from(expanded)
78}
79
80/// This can be used to register an action with the GPUI runtime when you want to manually implement
81/// the `Action` trait. Typically you should use the `Action` derive macro or `actions!` macro
82/// instead.
83#[proc_macro_derive(RegisterSetting)]
84pub fn register_settings(ident: TokenStream) -> TokenStream {
85 let type_name = parse_macro_input!(ident as Ident);
86
87 let settings_registration_fn_name = format_ident!(
88 "__settings_registration_{}",
89 type_name.to_string().to_lowercase()
90 );
91
92 quote! {
93 impl #type_name {
94 /// This is an auto generated function, do not use.
95 #[automatically_derived]
96 #[doc(hidden)]
97 fn __autogenerated() {
98 /// This is an auto generated function, do not use.
99 #[doc(hidden)]
100 fn #settings_registration_fn_name(cx: &mut gpui::AppContext) {
101 #type_name::register(cx);
102 }
103
104 settings::settings_registration::inventory::submit! {
105 settings::settings_registration::RegisterSettingFn(#settings_registration_fn_name)
106 }
107 }
108 }
109 }
110 .into()
111}