settings_ui_macros.rs

  1use heck::{ToSnakeCase as _, ToTitleCase as _};
  2use proc_macro2::TokenStream;
  3use quote::{ToTokens, quote};
  4use syn::{Data, DeriveInput, LitStr, Token, parse_macro_input};
  5
  6/// Derive macro for the `SettingsUi` marker trait.
  7///
  8/// This macro automatically implements the `SettingsUi` trait for the annotated type.
  9/// The `SettingsUi` trait is a marker trait used to indicate that a type can be
 10/// displayed in the settings UI.
 11///
 12/// # Example
 13///
 14/// ```
 15/// use settings::SettingsUi;
 16///
 17/// #[derive(SettingsUi)]
 18/// #[settings_ui(group = "Standard")]
 19/// struct MySettings {
 20///     enabled: bool,
 21///     count: usize,
 22/// }
 23/// ```
 24#[proc_macro_derive(SettingsUi, attributes(settings_ui))]
 25pub fn derive_settings_ui(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
 26    let input = parse_macro_input!(input as DeriveInput);
 27    let name = &input.ident;
 28
 29    // Handle generic parameters if present
 30    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
 31
 32    let mut group_name = Option::<String>::None;
 33    let mut path_name = Option::<String>::None;
 34
 35    for attr in &input.attrs {
 36        if attr.path().is_ident("settings_ui") {
 37            attr.parse_nested_meta(|meta| {
 38                if meta.path.is_ident("group") {
 39                    if group_name.is_some() {
 40                        return Err(meta.error("Only one 'group' path can be specified"));
 41                    }
 42                    meta.input.parse::<Token![=]>()?;
 43                    let lit: LitStr = meta.input.parse()?;
 44                    group_name = Some(lit.value());
 45                } else if meta.path.is_ident("path") {
 46                    // todo(settings_ui) rely entirely on settings_key, remove path attribute
 47                    if path_name.is_some() {
 48                        return Err(meta.error("Only one 'path' can be specified, either with `path` in `settings_ui` or with `settings_key`"));
 49                    }
 50                    meta.input.parse::<Token![=]>()?;
 51                    let lit: LitStr = meta.input.parse()?;
 52                    path_name = Some(lit.value());
 53                }
 54                Ok(())
 55            })
 56            .unwrap_or_else(|e| panic!("in #[settings_ui] attribute: {}", e));
 57        } else if let Some(settings_key) = parse_setting_key_attr(attr) {
 58            // todo(settings_ui) either remove fallback key or handle it here
 59            if path_name.is_some() && settings_key.key.is_some() {
 60                panic!("Both 'path' and 'settings_key' are specified. Must specify only one");
 61            }
 62            path_name = settings_key.key;
 63        }
 64    }
 65
 66    let doc_str = parse_documentation_from_attrs(&input.attrs);
 67
 68    let ui_item_fn_body = generate_ui_item_body(group_name.as_ref(), &input);
 69
 70    // todo(settings_ui): make group name optional, repurpose group as tag indicating item is group, and have "title" tag for custom title
 71    let title = group_name.unwrap_or(input.ident.to_string().to_title_case());
 72
 73    let ui_entry_fn_body = map_ui_item_to_entry(
 74        path_name.as_deref(),
 75        &title,
 76        doc_str.as_deref(),
 77        quote! { Self },
 78    );
 79
 80    let expanded = quote! {
 81        impl #impl_generics settings::SettingsUi for #name #ty_generics #where_clause {
 82            fn settings_ui_item() -> settings::SettingsUiItem {
 83                #ui_item_fn_body
 84            }
 85
 86            fn settings_ui_entry() -> settings::SettingsUiEntry {
 87                #ui_entry_fn_body
 88            }
 89        }
 90    };
 91
 92    proc_macro::TokenStream::from(expanded)
 93}
 94
 95fn extract_type_from_option(ty: TokenStream) -> TokenStream {
 96    match option_inner_type(ty.clone()) {
 97        Some(inner_type) => inner_type,
 98        None => ty,
 99    }
100}
101
102fn option_inner_type(ty: TokenStream) -> Option<TokenStream> {
103    let ty = syn::parse2::<syn::Type>(ty).ok()?;
104    let syn::Type::Path(path) = ty else {
105        return None;
106    };
107    let segment = path.path.segments.last()?;
108    if segment.ident != "Option" {
109        return None;
110    }
111    let syn::PathArguments::AngleBracketed(args) = &segment.arguments else {
112        return None;
113    };
114    let arg = args.args.first()?;
115    let syn::GenericArgument::Type(ty) = arg else {
116        return None;
117    };
118    return Some(ty.to_token_stream());
119}
120
121fn map_ui_item_to_entry(
122    path: Option<&str>,
123    title: &str,
124    doc_str: Option<&str>,
125    ty: TokenStream,
126) -> TokenStream {
127    // todo(settings_ui): does quote! just work with options?
128    let path = path.map_or_else(|| quote! {None}, |path| quote! {Some(#path)});
129    let doc_str = doc_str.map_or_else(|| quote! {None}, |doc_str| quote! {Some(#doc_str)});
130    let item = ui_item_from_type(ty);
131    quote! {
132        settings::SettingsUiEntry {
133            title: #title,
134            path: #path,
135            item: #item,
136            documentation: #doc_str,
137        }
138    }
139}
140
141fn ui_item_from_type(ty: TokenStream) -> TokenStream {
142    let ty = extract_type_from_option(ty);
143    return trait_method_call(ty, quote! {settings::SettingsUi}, quote! {settings_ui_item});
144}
145
146fn trait_method_call(
147    ty: TokenStream,
148    trait_name: TokenStream,
149    method_name: TokenStream,
150) -> TokenStream {
151    // doing the <ty as settings::SettingsUi> makes the error message better:
152    //  -> "#ty Doesn't implement settings::SettingsUi" instead of "no item "settings_ui_item" for #ty"
153    // and ensures safety against name conflicts
154    //
155    // todo(settings_ui): Turn `Vec<T>` into `Vec::<T>` here as well
156    quote! {
157        <#ty as #trait_name>::#method_name()
158    }
159}
160
161fn generate_ui_item_body(group_name: Option<&String>, input: &syn::DeriveInput) -> TokenStream {
162    match (group_name, &input.data) {
163        (_, Data::Union(_)) => unimplemented!("Derive SettingsUi for Unions"),
164        (None, Data::Struct(_)) => quote! {
165            settings::SettingsUiItem::None
166        },
167        (Some(_), Data::Struct(data_struct)) => {
168            let parent_serde_attrs = parse_serde_attributes(&input.attrs);
169            item_group_from_fields(&data_struct.fields, &parent_serde_attrs)
170        }
171        (None, Data::Enum(data_enum)) => {
172            let serde_attrs = parse_serde_attributes(&input.attrs);
173            let length = data_enum.variants.len();
174
175            let mut variants = Vec::with_capacity(length);
176            let mut labels = Vec::with_capacity(length);
177
178            for variant in &data_enum.variants {
179                // todo(settings_ui): Can #[serde(rename = )] be on enum variants?
180                let ident = variant.ident.clone().to_string();
181                let variant_name = serde_attrs.rename_all.apply(&ident);
182                let title = variant_name.to_title_case();
183
184                variants.push(variant_name);
185                labels.push(title);
186            }
187
188            let is_not_union = data_enum.variants.iter().all(|v| v.fields.is_empty());
189            if is_not_union {
190                return if length > 6 {
191                    quote! {
192                        settings::SettingsUiItem::Single(settings::SettingsUiItemSingle::DropDown{ variants: &[#(#variants),*], labels: &[#(#labels),*] })
193                    }
194                } else {
195                    quote! {
196                        settings::SettingsUiItem::Single(settings::SettingsUiItemSingle::ToggleGroup{ variants: &[#(#variants),*], labels: &[#(#labels),*] })
197                    }
198                };
199            }
200            // else: Union!
201            let enum_name = &input.ident;
202
203            let options = data_enum.variants.iter().map(|variant| {
204                if variant.fields.is_empty() {
205                    return quote! {None};
206                }
207                let name = &variant.ident;
208                let item = item_group_from_fields(&variant.fields, &serde_attrs);
209                // todo(settings_ui): documentation
210                return quote! {
211                    Some(settings::SettingsUiEntry {
212                        path: None,
213                        title: stringify!(#name),
214                        documentation: None,
215                        item: #item,
216                    })
217                };
218            });
219            let defaults = data_enum.variants.iter().map(|variant| {
220                let variant_name = &variant.ident;
221                if variant.fields.is_empty() {
222                    quote! {
223                        serde_json::to_value(#enum_name::#variant_name).expect("Failed to serialize default value for #enum_name::#variant_name")
224                    }
225                } else {
226                    let fields = variant.fields.iter().enumerate().map(|(index, field)| {
227                        let field_name = field.ident.as_ref().map_or_else(|| syn::Index::from(index).into_token_stream(), |ident| ident.to_token_stream());
228                        let field_type_is_option = option_inner_type(field.ty.to_token_stream()).is_some();
229                        let field_default = if field_type_is_option {
230                            quote! {
231                                None
232                            }
233                        } else {
234                            quote! {
235                                ::std::default::Default::default()
236                            }
237                        };
238
239                        quote!{
240                            #field_name: #field_default
241                        }
242                    });
243                    quote! {
244                        serde_json::to_value(#enum_name::#variant_name {
245                            #(#fields),*
246                        }).expect("Failed to serialize default value for #enum_name::#variant_name")
247                    }
248                }
249            });
250            // todo(settings_ui): Identify #[default] attr and use it for index, defaulting to 0
251            let default_variant_index: usize = 0;
252            let determine_option_fn = {
253                let match_arms = data_enum
254                    .variants
255                    .iter()
256                    .enumerate()
257                    .map(|(index, variant)| {
258                        let variant_name = &variant.ident;
259                        quote! {
260                            Ok(#variant_name {..}) => #index
261                        }
262                    });
263                quote! {
264                    |value: &serde_json::Value, _cx: &gpui::App| -> usize {
265                        use #enum_name::*;
266                        match serde_json::from_value::<#enum_name>(value.clone()) {
267                            #(#match_arms),*,
268                            Err(_) => #default_variant_index,
269                        }
270                    }
271                }
272            };
273            // todo(settings_ui) should probably always use toggle group for unions, dropdown makes less sense
274            return quote! {
275                settings::SettingsUiItem::Union(settings::SettingsUiItemUnion {
276                    defaults: Box::new([#(#defaults),*]),
277                    labels: &[#(#labels),*],
278                    options: Box::new([#(#options),*]),
279                    determine_option: #determine_option_fn,
280                })
281            };
282            // panic!("Unhandled");
283        }
284        // todo(settings_ui) discriminated unions
285        (_, Data::Enum(_)) => quote! {
286            settings::SettingsUiItem::None
287        },
288    }
289}
290
291fn item_group_from_fields(fields: &syn::Fields, parent_serde_attrs: &SerdeOptions) -> TokenStream {
292    let group_items = fields
293        .iter()
294        .filter(|field| {
295            !field.attrs.iter().any(|attr| {
296                let mut has_skip = false;
297                if attr.path().is_ident("settings_ui") {
298                    let _ = attr.parse_nested_meta(|meta| {
299                        if meta.path.is_ident("skip") {
300                            has_skip = true;
301                        }
302                        Ok(())
303                    });
304                }
305
306                has_skip
307            })
308        })
309        .map(|field| {
310            let field_serde_attrs = parse_serde_attributes(&field.attrs);
311            let name = field.ident.as_ref().map(ToString::to_string);
312            let title = name.as_ref().map_or_else(
313                || "todo(settings_ui): Titles for tuple fields".to_string(),
314                |name| name.to_title_case(),
315            );
316            let doc_str = parse_documentation_from_attrs(&field.attrs);
317
318            (
319                title,
320                doc_str,
321                name.filter(|_| !field_serde_attrs.flatten).map(|name| {
322                    parent_serde_attrs.apply_rename_to_field(&field_serde_attrs, &name)
323                }),
324                field.ty.to_token_stream(),
325            )
326        })
327        // todo(settings_ui): Re-format field name as nice title, and support setting different title with attr
328        .map(|(title, doc_str, path, ty)| {
329            map_ui_item_to_entry(path.as_deref(), &title, doc_str.as_deref(), ty)
330        });
331
332    quote! {
333        settings::SettingsUiItem::Group(settings::SettingsUiItemGroup{ items: vec![#(#group_items),*] })
334    }
335}
336
337struct SerdeOptions {
338    rename_all: SerdeRenameAll,
339    rename: Option<String>,
340    flatten: bool,
341    untagged: bool,
342    _alias: Option<String>, // todo(settings_ui)
343}
344
345#[derive(PartialEq)]
346enum SerdeRenameAll {
347    Lowercase,
348    SnakeCase,
349    None,
350}
351
352impl SerdeRenameAll {
353    fn apply(&self, name: &str) -> String {
354        match self {
355            SerdeRenameAll::Lowercase => name.to_lowercase(),
356            SerdeRenameAll::SnakeCase => name.to_snake_case(),
357            SerdeRenameAll::None => name.to_string(),
358        }
359    }
360}
361
362impl SerdeOptions {
363    fn apply_rename_to_field(&self, field_options: &Self, name: &str) -> String {
364        // field renames take precedence over struct rename all cases
365        if let Some(rename) = &field_options.rename {
366            return rename.clone();
367        }
368        return self.rename_all.apply(name);
369    }
370}
371
372fn parse_serde_attributes(attrs: &[syn::Attribute]) -> SerdeOptions {
373    let mut options = SerdeOptions {
374        rename_all: SerdeRenameAll::None,
375        rename: None,
376        flatten: false,
377        untagged: false,
378        _alias: None,
379    };
380
381    for attr in attrs {
382        if !attr.path().is_ident("serde") {
383            continue;
384        }
385        attr.parse_nested_meta(|meta| {
386            if meta.path.is_ident("rename_all") {
387                meta.input.parse::<Token![=]>()?;
388                let lit = meta.input.parse::<LitStr>()?.value();
389
390                if options.rename_all != SerdeRenameAll::None {
391                    return Err(meta.error("duplicate `rename_all` attribute"));
392                } else if lit == "lowercase" {
393                    options.rename_all = SerdeRenameAll::Lowercase;
394                } else if lit == "snake_case" {
395                    options.rename_all = SerdeRenameAll::SnakeCase;
396                } else {
397                    return Err(meta.error(format!("invalid `rename_all` attribute: {}", lit)));
398                }
399                // todo(settings_ui): Other options?
400            } else if meta.path.is_ident("flatten") {
401                options.flatten = true;
402            } else if meta.path.is_ident("rename") {
403                if options.rename.is_some() {
404                    return Err(meta.error("Can only have one rename attribute"));
405                }
406
407                meta.input.parse::<Token![=]>()?;
408                let lit = meta.input.parse::<LitStr>()?.value();
409                options.rename = Some(lit);
410            } else if meta.path.is_ident("untagged") {
411                options.untagged = true;
412            }
413            Ok(())
414        })
415        .unwrap();
416    }
417
418    return options;
419}
420
421fn parse_documentation_from_attrs(attrs: &[syn::Attribute]) -> Option<String> {
422    let mut doc_str = Option::<String>::None;
423    for attr in attrs {
424        if attr.path().is_ident("doc") {
425            // /// ...
426            // becomes
427            // #[doc = "..."]
428            use syn::{Expr::Lit, ExprLit, Lit::Str, Meta, MetaNameValue};
429            if let Meta::NameValue(MetaNameValue {
430                value:
431                    Lit(ExprLit {
432                        lit: Str(ref lit_str),
433                        ..
434                    }),
435                ..
436            }) = attr.meta
437            {
438                let doc = lit_str.value();
439                let doc_str = doc_str.get_or_insert_default();
440                doc_str.push_str(doc.trim());
441                doc_str.push('\n');
442            }
443        }
444    }
445    return doc_str;
446}
447
448struct SettingsKey {
449    key: Option<String>,
450    fallback_key: Option<String>,
451}
452
453fn parse_setting_key_attr(attr: &syn::Attribute) -> Option<SettingsKey> {
454    if !attr.path().is_ident("settings_key") {
455        return None;
456    }
457
458    let mut settings_key = SettingsKey {
459        key: None,
460        fallback_key: None,
461    };
462
463    let mut found_none = false;
464
465    attr.parse_nested_meta(|meta| {
466        if meta.path.is_ident("None") {
467            found_none = true;
468        } else if meta.path.is_ident("key") {
469            if settings_key.key.is_some() {
470                return Err(meta.error("Only one 'group' path can be specified"));
471            }
472            meta.input.parse::<Token![=]>()?;
473            let lit: LitStr = meta.input.parse()?;
474            settings_key.key = Some(lit.value());
475        } else if meta.path.is_ident("fallback_key") {
476            if found_none {
477                return Err(meta.error("Cannot specify 'fallback_key' and 'None'"));
478            }
479
480            if settings_key.fallback_key.is_some() {
481                return Err(meta.error("Only one 'fallback_key' can be specified"));
482            }
483
484            meta.input.parse::<Token![=]>()?;
485            let lit: LitStr = meta.input.parse()?;
486            settings_key.fallback_key = Some(lit.value());
487        }
488        Ok(())
489    })
490    .unwrap_or_else(|e| panic!("in #[settings_key] attribute: {}", e));
491
492    if found_none && settings_key.fallback_key.is_some() {
493        panic!("in #[settings_key] attribute: Cannot specify 'None' and 'fallback_key'");
494    }
495    if found_none && settings_key.key.is_some() {
496        panic!("in #[settings_key] attribute: Cannot specify 'None' and 'key'");
497    }
498    if !found_none && settings_key.key.is_none() {
499        panic!("in #[settings_key] attribute: 'key' must be specified");
500    }
501
502    return Some(settings_key);
503}
504
505#[proc_macro_derive(SettingsKey, attributes(settings_key))]
506pub fn derive_settings_key(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
507    let input = parse_macro_input!(input as DeriveInput);
508    let name = &input.ident;
509
510    // Handle generic parameters if present
511    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
512
513    let mut settings_key = Option::<SettingsKey>::None;
514
515    for attr in &input.attrs {
516        let parsed_settings_key = parse_setting_key_attr(attr);
517        if parsed_settings_key.is_some() && settings_key.is_some() {
518            panic!("Duplicate #[settings_key] attribute");
519        }
520        settings_key = settings_key.or(parsed_settings_key);
521    }
522
523    let Some(SettingsKey { key, fallback_key }) = settings_key else {
524        panic!("Missing #[settings_key] attribute");
525    };
526
527    let key = key.map_or_else(|| quote! {None}, |key| quote! {Some(#key)});
528    let fallback_key = fallback_key.map_or_else(
529        || quote! {None},
530        |fallback_key| quote! {Some(#fallback_key)},
531    );
532
533    let expanded = quote! {
534        impl #impl_generics settings::SettingsKey for #name #ty_generics #where_clause {
535            const KEY: Option<&'static str> = #key;
536
537            const FALLBACK_KEY: Option<&'static str> = #fallback_key;
538        };
539    };
540
541    proc_macro::TokenStream::from(expanded)
542}
543
544#[cfg(test)]
545mod tests {
546    use syn::{Attribute, parse_quote};
547
548    use super::*;
549
550    #[test]
551    fn test_extract_key() {
552        let input: Attribute = parse_quote!(
553            #[settings_key(key = "my_key")]
554        );
555        let settings_key = parse_setting_key_attr(&input).unwrap();
556        assert_eq!(settings_key.key, Some("my_key".to_string()));
557        assert_eq!(settings_key.fallback_key, None);
558    }
559
560    #[test]
561    fn test_empty_key() {
562        let input: Attribute = parse_quote!(
563            #[settings_key(None)]
564        );
565        let settings_key = parse_setting_key_attr(&input).unwrap();
566        assert_eq!(settings_key.key, None);
567        assert_eq!(settings_key.fallback_key, None);
568    }
569}