Checkpoint

This commit is contained in:
Nathan Sobo 2023-08-19 20:29:55 -06:00
parent 0747131bd4
commit 2c6f692c56
4 changed files with 82 additions and 61 deletions

View file

@ -2,12 +2,12 @@ use proc_macro::TokenStream;
mod derive_element;
mod derive_into_element;
mod styleable_trait;
mod styleable_helpers;
mod tailwind_lengths;
#[proc_macro]
pub fn styleable_trait(args: TokenStream) -> TokenStream {
styleable_trait::styleable_trait(args)
pub fn styleable_helpers(args: TokenStream) -> TokenStream {
styleable_helpers::styleable_helpers(args)
}
#[proc_macro_derive(Element, attributes(element_crate))]

View file

@ -1,6 +1,62 @@
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use syn::{
parse::{Parse, ParseStream, Result},
parse_macro_input,
};
struct StyleableMacroInput;
impl Parse for StyleableMacroInput {
fn parse(_input: ParseStream) -> Result<Self> {
Ok(StyleableMacroInput)
}
}
pub fn styleable_helpers(input: TokenStream) -> TokenStream {
let _ = parse_macro_input!(input as StyleableMacroInput);
let methods = generate_methods();
let output = quote! {
#(#methods)*
};
output.into()
}
fn generate_methods() -> Vec<TokenStream2> {
let mut methods = Vec::new();
for (prefix, auto_allowed, fields) in tailwind_prefixes() {
for (suffix, length_tokens) in tailwind_lengths() {
if !auto_allowed && suffix == "auto" {
// Conditional to skip "auto"
continue;
}
let method_name = format_ident!("{}_{}", prefix, suffix);
let field_assignments = fields
.iter()
.map(|field_tokens| {
quote! {
style.#field_tokens = Some(gpui::geometry::#length_tokens.into());
}
})
.collect::<Vec<_>>();
let method = quote! {
fn #method_name(mut self) -> Self where Self: std::marker::Sized {
let mut style = self.declared_style();
#(#field_assignments)*
self
}
};
methods.push(method);
}
}
methods
}
fn tailwind_lengths() -> Vec<(&'static str, TokenStream2)> {
vec![
@ -73,55 +129,3 @@ fn tailwind_prefixes() -> Vec<(&'static str, bool, Vec<TokenStream2>)> {
("right", true, vec![quote! { inset.right }]),
]
}
pub fn styleable_trait(_item: TokenStream) -> TokenStream {
let mut methods = Vec::new();
for (prefix, auto_allowed, fields) in tailwind_prefixes() {
for (suffix, length_tokens) in tailwind_lengths() {
if !auto_allowed && suffix == "auto" {
// Conditional to skip "auto"
continue;
}
let method_name = format_ident!("{}_{}", prefix, suffix);
let field_assignments = fields
.iter()
.map(|field_tokens| {
quote! {
style.#field_tokens = Some(gpui::geometry::#length_tokens.into());
}
})
.collect::<Vec<_>>();
let method = quote! {
fn #method_name(mut self) -> Self where Self: Sized {
let mut style = self.declared_style();
#(#field_assignments)*
self
}
};
methods.push(method);
}
}
let output = quote! {
pub trait Styleable {
type Style: refineable::Refineable;
fn declared_style(&mut self) -> &mut playground::style::StyleRefinement;
fn style(&mut self) -> playground::style::Style {
let mut style = playground::style::Style::default();
style.refine(self.declared_style());
style
}
#(#methods)*
}
};
output.into()
}