Fix Component derive macro
This commit is contained in:
parent
f4cff69729
commit
c9c9db903d
2 changed files with 44 additions and 44 deletions
|
@ -3,29 +3,53 @@ use quote::quote;
|
||||||
use syn::{parse_macro_input, parse_quote, DeriveInput};
|
use syn::{parse_macro_input, parse_quote, DeriveInput};
|
||||||
|
|
||||||
pub fn derive_component(input: TokenStream) -> TokenStream {
|
pub fn derive_component(input: TokenStream) -> TokenStream {
|
||||||
let mut ast = parse_macro_input!(input as DeriveInput);
|
let ast = parse_macro_input!(input as DeriveInput);
|
||||||
|
let name = &ast.ident;
|
||||||
|
|
||||||
if !ast
|
let mut trait_generics = ast.generics.clone();
|
||||||
.generics
|
let view_type = if let Some(view_type) = specified_view_type(&ast) {
|
||||||
.params
|
quote! { #view_type }
|
||||||
.iter()
|
} else {
|
||||||
.any(|param| matches!(param, syn::GenericParam::Type(_)))
|
if let Some(first_type_param) = ast.generics.params.iter().find_map(|param| {
|
||||||
{
|
if let syn::GenericParam::Type(type_param) = param {
|
||||||
ast.generics.params.push(parse_quote! {
|
Some(type_param.ident.clone())
|
||||||
V: 'static
|
} else {
|
||||||
});
|
None
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
quote! { #first_type_param }
|
||||||
|
} else {
|
||||||
|
trait_generics.params.push(parse_quote! { V: 'static + Send + Sync });
|
||||||
|
quote! { V }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let (impl_generics, _, where_clause) = trait_generics.split_for_impl();
|
||||||
|
let (_, ty_generics, _) = ast.generics.split_for_impl();
|
||||||
|
|
||||||
|
let expanded = quote! {
|
||||||
|
impl #impl_generics gpui2::Component<#view_type> for #name #ty_generics #where_clause {
|
||||||
|
fn render(self) -> gpui2::AnyElement<#view_type> {
|
||||||
|
(move |view_state: &mut #view_type, cx: &mut gpui2::ViewContext<'_, '_, #view_type>| self.render(view_state, cx))
|
||||||
|
.render()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if name == "AssistantPanelStory" {
|
||||||
|
println!("Expanded tokens: {}", expanded.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = &ast.ident;
|
TokenStream::from(expanded)
|
||||||
let generics = &ast.generics;
|
}
|
||||||
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
|
|
||||||
|
|
||||||
let specified_view_type = ast
|
fn specified_view_type(ast: &DeriveInput) -> Option<proc_macro2::Ident> {
|
||||||
|
let component_attr = ast
|
||||||
.attrs
|
.attrs
|
||||||
.iter()
|
.iter()
|
||||||
.find(|attr| attr.path.is_ident("component"))
|
.find(|attr| attr.path.is_ident("component"))?;
|
||||||
.and_then(|attr| {
|
|
||||||
if let Ok(syn::Meta::List(meta_list)) = attr.parse_meta() {
|
if let Ok(syn::Meta::List(meta_list)) = component_attr.parse_meta() {
|
||||||
meta_list.nested.iter().find_map(|nested| {
|
meta_list.nested.iter().find_map(|nested| {
|
||||||
if let syn::NestedMeta::Meta(syn::Meta::NameValue(nv)) = nested {
|
if let syn::NestedMeta::Meta(syn::Meta::NameValue(nv)) = nested {
|
||||||
if nv.path.is_ident("view_type") {
|
if nv.path.is_ident("view_type") {
|
||||||
|
@ -43,28 +67,4 @@ pub fn derive_component(input: TokenStream) -> TokenStream {
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
let view_type = specified_view_type.unwrap_or_else(|| {
|
|
||||||
if let Some(syn::GenericParam::Type(type_param)) = generics.params.first() {
|
|
||||||
type_param.ident.clone()
|
|
||||||
} else {
|
|
||||||
panic!("Expected first type parameter to be a view type");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let expanded = quote! {
|
|
||||||
impl #impl_generics gpui2::Component<#view_type> for #name #ty_generics #where_clause {
|
|
||||||
fn render(self) -> gpui2::AnyElement<#view_type> {
|
|
||||||
(move |view_state: &mut #view_type, cx: &mut gpui2::ViewContext<'_, '_, #view_type>| self.render(view_state, cx))
|
|
||||||
.render()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if name == "AssistantPanelStory" {
|
|
||||||
println!("Expanded tokens: {}", expanded.to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
TokenStream::from(expanded)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,7 +69,7 @@ impl<S: 'static + Send + Sync> AssistantPanel<S> {
|
||||||
.overflow_y_scroll()
|
.overflow_y_scroll()
|
||||||
.child(Label::new("Is this thing on?")),
|
.child(Label::new("Is this thing on?")),
|
||||||
)
|
)
|
||||||
.renderinto_any()])
|
.render()])
|
||||||
.side(self.current_side)
|
.side(self.current_side)
|
||||||
.width(AbsoluteLength::Rems(rems(32.)))
|
.width(AbsoluteLength::Rems(rems(32.)))
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ mod stories {
|
||||||
Self {}
|
Self {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
|
fn render<V: 'static + Send + Sync>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
|
||||||
Story::container(cx)
|
Story::container(cx)
|
||||||
.child(Story::title_for::<_, AssistantPanel<V>>(cx))
|
.child(Story::title_for::<_, AssistantPanel<V>>(cx))
|
||||||
.child(Story::label(cx, "Default"))
|
.child(Story::label(cx, "Default"))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue