From 36b853ac0522667be1cef98ba317637e632fea4e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sun, 13 Aug 2023 01:40:10 -0600 Subject: [PATCH] WIP --- crates/gpui/playground_macros/Cargo.toml | 14 ++++++++ .../src/playground_macros.rs | 36 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 crates/gpui/playground_macros/Cargo.toml create mode 100644 crates/gpui/playground_macros/src/playground_macros.rs diff --git a/crates/gpui/playground_macros/Cargo.toml b/crates/gpui/playground_macros/Cargo.toml new file mode 100644 index 0000000000..a1f84eb5e0 --- /dev/null +++ b/crates/gpui/playground_macros/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "playground_macros" +version = "0.1.0" +edition = "2021" + +[lib] +path = "src/playground_macros.rs" +proc-macro = true + +[dependencies] +syn = "1.0.72" +quote = "1.0.9" +proc-macro2 = "1.0.66" +once_cell = "1.18.0" diff --git a/crates/gpui/playground_macros/src/playground_macros.rs b/crates/gpui/playground_macros/src/playground_macros.rs new file mode 100644 index 0000000000..5e8a10a59c --- /dev/null +++ b/crates/gpui/playground_macros/src/playground_macros.rs @@ -0,0 +1,36 @@ +use proc_macro::TokenStream; +use quote::{format_ident, quote}; +use syn::{parse_macro_input, FnArg, ItemFn, PatType}; + +#[proc_macro_attribute] +pub fn tailwind_lengths(_attr: TokenStream, item: TokenStream) -> TokenStream { + let input_function = parse_macro_input!(item as ItemFn); + let function_signature = input_function.sig.clone(); + let function_body = input_function.block; + + let argument_name = match function_signature.inputs.iter().nth(1) { + Some(FnArg::Typed(PatType { pat, .. })) => pat, + _ => panic!("Couldn't find the second argument in the function signature"), + }; + + let scale_lengths = [ + ("0", quote! { Length::Rems(0.) }), + ("px", quote! { Length::Pixels(1.) }), + // ... + ("auto", quote! { Length::Auto }), + ]; + + let mut output_functions = proc_macro2::TokenStream::new(); + + for (length, value) in &scale_lengths { + let function_name = format_ident!("{}_{}", function_signature.ident, length); + output_functions.extend(quote! { + pub fn #function_name(mut self) -> Self { + let #argument_name = #value; + #function_body + } + }); + } + + output_functions.into() +}