Remove 2 suffix from gpui_macros, fix compile errors in tests
Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
parent
f5ba22659b
commit
83f4c61657
29 changed files with 71 additions and 1670 deletions
|
@ -7,10 +7,8 @@ publish = false
|
|||
[lib]
|
||||
path = "src/gpui_macros.rs"
|
||||
proc-macro = true
|
||||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
lazy_static.workspace = true
|
||||
proc-macro2 = "1.0"
|
||||
syn = "1.0"
|
||||
quote = "1.0"
|
||||
syn = { version = "1.0.72", features = ["full"] }
|
||||
quote = "1.0.9"
|
||||
proc-macro2 = "1.0.66"
|
||||
|
|
27
crates/gpui_macros/src/derive_into_element.rs
Normal file
27
crates/gpui_macros/src/derive_into_element.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, DeriveInput};
|
||||
|
||||
pub fn derive_into_element(input: TokenStream) -> TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
let type_name = &ast.ident;
|
||||
let (impl_generics, type_generics, where_clause) = ast.generics.split_for_impl();
|
||||
|
||||
let gen = quote! {
|
||||
impl #impl_generics gpui::IntoElement for #type_name #type_generics
|
||||
#where_clause
|
||||
{
|
||||
type Element = gpui::Component<Self>;
|
||||
|
||||
fn element_id(&self) -> Option<gpui::ElementId> {
|
||||
None
|
||||
}
|
||||
|
||||
fn into_element(self) -> Self::Element {
|
||||
gpui::Component::new(self)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
gen.into()
|
||||
}
|
21
crates/gpui_macros/src/derive_render.rs
Normal file
21
crates/gpui_macros/src/derive_render.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, DeriveInput};
|
||||
|
||||
pub fn derive_render(input: TokenStream) -> TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
let type_name = &ast.ident;
|
||||
let (impl_generics, type_generics, where_clause) = ast.generics.split_for_impl();
|
||||
|
||||
let gen = quote! {
|
||||
impl #impl_generics gpui::Render for #type_name #type_generics
|
||||
#where_clause
|
||||
{
|
||||
fn render(&mut self, _cx: &mut gpui::ViewContext<Self>) -> impl gpui::Element {
|
||||
()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
gen.into()
|
||||
}
|
|
@ -1,381 +1,32 @@
|
|||
mod derive_into_element;
|
||||
mod derive_render;
|
||||
mod register_action;
|
||||
mod style_helpers;
|
||||
mod test;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Ident;
|
||||
use quote::{format_ident, quote};
|
||||
use std::mem;
|
||||
use syn::{
|
||||
parse_macro_input, parse_quote, spanned::Spanned as _, AttributeArgs, DeriveInput, FnArg,
|
||||
GenericParam, Generics, ItemFn, Lit, Meta, NestedMeta, Type, WhereClause,
|
||||
};
|
||||
|
||||
#[proc_macro]
|
||||
pub fn register_action(ident: TokenStream) -> TokenStream {
|
||||
register_action::register_action_macro(ident)
|
||||
}
|
||||
|
||||
#[proc_macro_derive(IntoElement)]
|
||||
pub fn derive_into_element(input: TokenStream) -> TokenStream {
|
||||
derive_into_element::derive_into_element(input)
|
||||
}
|
||||
|
||||
#[proc_macro_derive(Render)]
|
||||
pub fn derive_render(input: TokenStream) -> TokenStream {
|
||||
derive_render::derive_render(input)
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn style_helpers(input: TokenStream) -> TokenStream {
|
||||
style_helpers::style_helpers(input)
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
let mut namespace = format_ident!("gpui");
|
||||
|
||||
let args = syn::parse_macro_input!(args as AttributeArgs);
|
||||
let mut max_retries = 0;
|
||||
let mut num_iterations = 1;
|
||||
let mut starting_seed = 0;
|
||||
let mut detect_nondeterminism = false;
|
||||
let mut on_failure_fn_name = quote!(None);
|
||||
|
||||
for arg in args {
|
||||
match arg {
|
||||
NestedMeta::Meta(Meta::Path(name))
|
||||
if name.get_ident().map_or(false, |n| n == "self") =>
|
||||
{
|
||||
namespace = format_ident!("crate");
|
||||
}
|
||||
NestedMeta::Meta(Meta::NameValue(meta)) => {
|
||||
let key_name = meta.path.get_ident().map(|i| i.to_string());
|
||||
let result = (|| {
|
||||
match key_name.as_deref() {
|
||||
Some("detect_nondeterminism") => {
|
||||
detect_nondeterminism = parse_bool(&meta.lit)?
|
||||
}
|
||||
Some("retries") => max_retries = parse_int(&meta.lit)?,
|
||||
Some("iterations") => num_iterations = parse_int(&meta.lit)?,
|
||||
Some("seed") => starting_seed = parse_int(&meta.lit)?,
|
||||
Some("on_failure") => {
|
||||
if let Lit::Str(name) = meta.lit {
|
||||
let mut path = syn::Path {
|
||||
leading_colon: None,
|
||||
segments: Default::default(),
|
||||
};
|
||||
for part in name.value().split("::") {
|
||||
path.segments.push(Ident::new(part, name.span()).into());
|
||||
}
|
||||
on_failure_fn_name = quote!(Some(#path));
|
||||
} else {
|
||||
return Err(TokenStream::from(
|
||||
syn::Error::new(
|
||||
meta.lit.span(),
|
||||
"on_failure argument must be a string",
|
||||
)
|
||||
.into_compile_error(),
|
||||
));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(TokenStream::from(
|
||||
syn::Error::new(meta.path.span(), "invalid argument")
|
||||
.into_compile_error(),
|
||||
))
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
|
||||
if let Err(tokens) = result {
|
||||
return tokens;
|
||||
}
|
||||
}
|
||||
other => {
|
||||
return TokenStream::from(
|
||||
syn::Error::new_spanned(other, "invalid argument").into_compile_error(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut inner_fn = parse_macro_input!(function as ItemFn);
|
||||
if max_retries > 0 && num_iterations > 1 {
|
||||
return TokenStream::from(
|
||||
syn::Error::new_spanned(inner_fn, "retries and randomized iterations can't be mixed")
|
||||
.into_compile_error(),
|
||||
);
|
||||
}
|
||||
let inner_fn_attributes = mem::take(&mut inner_fn.attrs);
|
||||
let inner_fn_name = format_ident!("_{}", inner_fn.sig.ident);
|
||||
let outer_fn_name = mem::replace(&mut inner_fn.sig.ident, inner_fn_name.clone());
|
||||
|
||||
let mut outer_fn: ItemFn = if inner_fn.sig.asyncness.is_some() {
|
||||
// Pass to the test function the number of app contexts that it needs,
|
||||
// based on its parameter list.
|
||||
let mut cx_vars = proc_macro2::TokenStream::new();
|
||||
let mut cx_teardowns = proc_macro2::TokenStream::new();
|
||||
let mut inner_fn_args = proc_macro2::TokenStream::new();
|
||||
for (ix, arg) in inner_fn.sig.inputs.iter().enumerate() {
|
||||
if let FnArg::Typed(arg) = arg {
|
||||
if let Type::Path(ty) = &*arg.ty {
|
||||
let last_segment = ty.path.segments.last();
|
||||
match last_segment.map(|s| s.ident.to_string()).as_deref() {
|
||||
Some("StdRng") => {
|
||||
inner_fn_args.extend(quote!(rand::SeedableRng::seed_from_u64(seed),));
|
||||
continue;
|
||||
}
|
||||
Some("Arc") => {
|
||||
if let syn::PathArguments::AngleBracketed(args) =
|
||||
&last_segment.unwrap().arguments
|
||||
{
|
||||
if let Some(syn::GenericArgument::Type(syn::Type::Path(ty))) =
|
||||
args.args.last()
|
||||
{
|
||||
let last_segment = ty.path.segments.last();
|
||||
if let Some("Deterministic") =
|
||||
last_segment.map(|s| s.ident.to_string()).as_deref()
|
||||
{
|
||||
inner_fn_args.extend(quote!(deterministic.clone(),));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else if let Type::Reference(ty) = &*arg.ty {
|
||||
if let Type::Path(ty) = &*ty.elem {
|
||||
let last_segment = ty.path.segments.last();
|
||||
if let Some("TestAppContext") =
|
||||
last_segment.map(|s| s.ident.to_string()).as_deref()
|
||||
{
|
||||
let first_entity_id = ix * 100_000;
|
||||
let cx_varname = format_ident!("cx_{}", ix);
|
||||
cx_vars.extend(quote!(
|
||||
let mut #cx_varname = #namespace::TestAppContext::new(
|
||||
foreground_platform.clone(),
|
||||
cx.platform().clone(),
|
||||
deterministic.build_foreground(#ix),
|
||||
deterministic.build_background(),
|
||||
cx.font_cache().clone(),
|
||||
cx.leak_detector(),
|
||||
#first_entity_id,
|
||||
stringify!(#outer_fn_name).to_string(),
|
||||
);
|
||||
));
|
||||
cx_teardowns.extend(quote!(
|
||||
#cx_varname.remove_all_windows();
|
||||
deterministic.run_until_parked();
|
||||
#cx_varname.update(|cx| cx.clear_globals());
|
||||
));
|
||||
inner_fn_args.extend(quote!(&mut #cx_varname,));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return TokenStream::from(
|
||||
syn::Error::new_spanned(arg, "invalid argument").into_compile_error(),
|
||||
);
|
||||
}
|
||||
|
||||
parse_quote! {
|
||||
#[test]
|
||||
fn #outer_fn_name() {
|
||||
#inner_fn
|
||||
|
||||
#namespace::test::run_test(
|
||||
#num_iterations as u64,
|
||||
#starting_seed as u64,
|
||||
#max_retries,
|
||||
#detect_nondeterminism,
|
||||
&mut |cx, foreground_platform, deterministic, seed| {
|
||||
// some of the macro contents do not use all variables, silence the warnings
|
||||
let _ = (&cx, &foreground_platform, &deterministic, &seed);
|
||||
#cx_vars
|
||||
cx.foreground().run(#inner_fn_name(#inner_fn_args));
|
||||
#cx_teardowns
|
||||
},
|
||||
#on_failure_fn_name,
|
||||
stringify!(#outer_fn_name).to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Pass to the test function the number of app contexts that it needs,
|
||||
// based on its parameter list.
|
||||
let mut cx_vars = proc_macro2::TokenStream::new();
|
||||
let mut cx_teardowns = proc_macro2::TokenStream::new();
|
||||
let mut inner_fn_args = proc_macro2::TokenStream::new();
|
||||
for (ix, arg) in inner_fn.sig.inputs.iter().enumerate() {
|
||||
if let FnArg::Typed(arg) = arg {
|
||||
if let Type::Path(ty) = &*arg.ty {
|
||||
let last_segment = ty.path.segments.last();
|
||||
|
||||
if let Some("StdRng") = last_segment.map(|s| s.ident.to_string()).as_deref() {
|
||||
inner_fn_args.extend(quote!(rand::SeedableRng::seed_from_u64(seed),));
|
||||
continue;
|
||||
}
|
||||
} else if let Type::Reference(ty) = &*arg.ty {
|
||||
if let Type::Path(ty) = &*ty.elem {
|
||||
let last_segment = ty.path.segments.last();
|
||||
match last_segment.map(|s| s.ident.to_string()).as_deref() {
|
||||
Some("AppContext") => {
|
||||
inner_fn_args.extend(quote!(cx,));
|
||||
continue;
|
||||
}
|
||||
Some("TestAppContext") => {
|
||||
let first_entity_id = ix * 100_000;
|
||||
let cx_varname = format_ident!("cx_{}", ix);
|
||||
cx_vars.extend(quote!(
|
||||
let mut #cx_varname = #namespace::TestAppContext::new(
|
||||
foreground_platform.clone(),
|
||||
cx.platform().clone(),
|
||||
deterministic.build_foreground(#ix),
|
||||
deterministic.build_background(),
|
||||
cx.font_cache().clone(),
|
||||
cx.leak_detector(),
|
||||
#first_entity_id,
|
||||
stringify!(#outer_fn_name).to_string(),
|
||||
);
|
||||
));
|
||||
cx_teardowns.extend(quote!(
|
||||
#cx_varname.remove_all_windows();
|
||||
deterministic.run_until_parked();
|
||||
#cx_varname.update(|cx| cx.clear_globals());
|
||||
));
|
||||
inner_fn_args.extend(quote!(&mut #cx_varname,));
|
||||
continue;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return TokenStream::from(
|
||||
syn::Error::new_spanned(arg, "invalid argument").into_compile_error(),
|
||||
);
|
||||
}
|
||||
|
||||
parse_quote! {
|
||||
#[test]
|
||||
fn #outer_fn_name() {
|
||||
#inner_fn
|
||||
|
||||
#namespace::test::run_test(
|
||||
#num_iterations as u64,
|
||||
#starting_seed as u64,
|
||||
#max_retries,
|
||||
#detect_nondeterminism,
|
||||
&mut |cx, foreground_platform, deterministic, seed| {
|
||||
// some of the macro contents do not use all variables, silence the warnings
|
||||
let _ = (&cx, &foreground_platform, &deterministic, &seed);
|
||||
#cx_vars
|
||||
#inner_fn_name(#inner_fn_args);
|
||||
#cx_teardowns
|
||||
},
|
||||
#on_failure_fn_name,
|
||||
stringify!(#outer_fn_name).to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
outer_fn.attrs.extend(inner_fn_attributes);
|
||||
|
||||
TokenStream::from(quote!(#outer_fn))
|
||||
}
|
||||
|
||||
fn parse_int(literal: &Lit) -> Result<usize, TokenStream> {
|
||||
let result = if let Lit::Int(int) = &literal {
|
||||
int.base10_parse()
|
||||
} else {
|
||||
Err(syn::Error::new(literal.span(), "must be an integer"))
|
||||
};
|
||||
|
||||
result.map_err(|err| TokenStream::from(err.into_compile_error()))
|
||||
}
|
||||
|
||||
fn parse_bool(literal: &Lit) -> Result<bool, TokenStream> {
|
||||
let result = if let Lit::Bool(result) = &literal {
|
||||
Ok(result.value)
|
||||
} else {
|
||||
Err(syn::Error::new(literal.span(), "must be a boolean"))
|
||||
};
|
||||
|
||||
result.map_err(|err| TokenStream::from(err.into_compile_error()))
|
||||
}
|
||||
|
||||
#[proc_macro_derive(Element)]
|
||||
pub fn element_derive(input: TokenStream) -> TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
let type_name = ast.ident;
|
||||
|
||||
let placeholder_view_generics: Generics = parse_quote! { <V: 'static> };
|
||||
let placeholder_view_type_name: Ident = parse_quote! { V };
|
||||
let view_type_name: Ident;
|
||||
let impl_generics: syn::ImplGenerics<'_>;
|
||||
let type_generics: Option<syn::TypeGenerics<'_>>;
|
||||
let where_clause: Option<&'_ WhereClause>;
|
||||
|
||||
match ast.generics.params.iter().find_map(|param| {
|
||||
if let GenericParam::Type(type_param) = param {
|
||||
Some(type_param.ident.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}) {
|
||||
Some(type_name) => {
|
||||
view_type_name = type_name;
|
||||
let generics = ast.generics.split_for_impl();
|
||||
impl_generics = generics.0;
|
||||
type_generics = Some(generics.1);
|
||||
where_clause = generics.2;
|
||||
}
|
||||
_ => {
|
||||
view_type_name = placeholder_view_type_name;
|
||||
let generics = placeholder_view_generics.split_for_impl();
|
||||
impl_generics = generics.0;
|
||||
type_generics = None;
|
||||
where_clause = generics.2;
|
||||
}
|
||||
}
|
||||
|
||||
let gen = quote! {
|
||||
impl #impl_generics Element<#view_type_name> for #type_name #type_generics
|
||||
#where_clause
|
||||
{
|
||||
|
||||
type LayoutState = gpui::elements::AnyElement<V>;
|
||||
type PaintState = ();
|
||||
|
||||
fn layout(
|
||||
&mut self,
|
||||
constraint: gpui::SizeConstraint,
|
||||
view: &mut V,
|
||||
cx: &mut gpui::ViewContext<V>,
|
||||
) -> (gpui::geometry::vector::Vector2F, gpui::elements::AnyElement<V>) {
|
||||
let mut element = self.render(view, cx).into_any();
|
||||
let size = element.layout(constraint, view, cx);
|
||||
(size, element)
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
bounds: gpui::geometry::rect::RectF,
|
||||
visible_bounds: gpui::geometry::rect::RectF,
|
||||
element: &mut gpui::elements::AnyElement<V>,
|
||||
view: &mut V,
|
||||
cx: &mut gpui::ViewContext<V>,
|
||||
) {
|
||||
element.paint(bounds.origin(), visible_bounds, view, cx);
|
||||
}
|
||||
|
||||
fn rect_for_text_range(
|
||||
&self,
|
||||
range_utf16: std::ops::Range<usize>,
|
||||
_: gpui::geometry::rect::RectF,
|
||||
_: gpui::geometry::rect::RectF,
|
||||
element: &gpui::elements::AnyElement<V>,
|
||||
_: &(),
|
||||
view: &V,
|
||||
cx: &gpui::ViewContext<V>,
|
||||
) -> Option<gpui::geometry::rect::RectF> {
|
||||
element.rect_for_text_range(range_utf16, view, cx)
|
||||
}
|
||||
|
||||
fn debug(
|
||||
&self,
|
||||
_: gpui::geometry::rect::RectF,
|
||||
element: &gpui::elements::AnyElement<V>,
|
||||
_: &(),
|
||||
view: &V,
|
||||
cx: &gpui::ViewContext<V>,
|
||||
) -> gpui::json::Value {
|
||||
element.debug(view, cx)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
gen.into()
|
||||
test::test(args, function)
|
||||
}
|
||||
|
|
53
crates/gpui_macros/src/register_action.rs
Normal file
53
crates/gpui_macros/src/register_action.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
// Input:
|
||||
//
|
||||
// struct FooBar {}
|
||||
|
||||
// Output:
|
||||
//
|
||||
// struct FooBar {}
|
||||
//
|
||||
// #[allow(non_snake_case)]
|
||||
// #[gpui2::ctor]
|
||||
// fn register_foobar_builder() {
|
||||
// gpui2::register_action_builder::<Foo>()
|
||||
// }
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Ident;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::parse_macro_input;
|
||||
|
||||
pub fn register_action_macro(ident: TokenStream) -> TokenStream {
|
||||
let name = parse_macro_input!(ident as Ident);
|
||||
let registration = register_action(&name);
|
||||
|
||||
TokenStream::from(quote! {
|
||||
#registration
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn register_action(type_name: &Ident) -> proc_macro2::TokenStream {
|
||||
let static_slice_name =
|
||||
format_ident!("__GPUI_ACTIONS_{}", type_name.to_string().to_uppercase());
|
||||
|
||||
let action_builder_fn_name = format_ident!(
|
||||
"__gpui_actions_builder_{}",
|
||||
type_name.to_string().to_lowercase()
|
||||
);
|
||||
|
||||
quote! {
|
||||
#[doc(hidden)]
|
||||
#[gpui::linkme::distributed_slice(gpui::__GPUI_ACTIONS)]
|
||||
#[linkme(crate = gpui::linkme)]
|
||||
static #static_slice_name: gpui::MacroActionBuilder = #action_builder_fn_name;
|
||||
|
||||
/// This is an auto generated function, do not use.
|
||||
#[doc(hidden)]
|
||||
fn #action_builder_fn_name() -> gpui::ActionData {
|
||||
gpui::ActionData {
|
||||
name: <#type_name as gpui::Action>::debug_name(),
|
||||
type_id: ::std::any::TypeId::of::<#type_name>(),
|
||||
build: <#type_name as gpui::Action>::build,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
561
crates/gpui_macros/src/style_helpers.rs
Normal file
561
crates/gpui_macros/src/style_helpers.rs
Normal file
|
@ -0,0 +1,561 @@
|
|||
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 style_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, prefix_doc_string) in box_prefixes() {
|
||||
methods.push(generate_custom_value_setter(
|
||||
prefix,
|
||||
if auto_allowed {
|
||||
quote! { Length }
|
||||
} else {
|
||||
quote! { DefiniteLength }
|
||||
},
|
||||
&fields,
|
||||
prefix_doc_string,
|
||||
));
|
||||
|
||||
for (suffix, length_tokens, suffix_doc_string) in box_suffixes() {
|
||||
if suffix != "auto" || auto_allowed {
|
||||
methods.push(generate_predefined_setter(
|
||||
prefix,
|
||||
suffix,
|
||||
&fields,
|
||||
&length_tokens,
|
||||
false,
|
||||
&format!("{prefix_doc_string}\n\n{suffix_doc_string}"),
|
||||
));
|
||||
}
|
||||
|
||||
if suffix != "auto" {
|
||||
methods.push(generate_predefined_setter(
|
||||
prefix,
|
||||
suffix,
|
||||
&fields,
|
||||
&length_tokens,
|
||||
true,
|
||||
&format!("{prefix_doc_string}\n\n{suffix_doc_string}"),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (prefix, fields, prefix_doc_string) in corner_prefixes() {
|
||||
methods.push(generate_custom_value_setter(
|
||||
prefix,
|
||||
quote! { AbsoluteLength },
|
||||
&fields,
|
||||
prefix_doc_string,
|
||||
));
|
||||
|
||||
for (suffix, radius_tokens, suffix_doc_string) in corner_suffixes() {
|
||||
methods.push(generate_predefined_setter(
|
||||
prefix,
|
||||
suffix,
|
||||
&fields,
|
||||
&radius_tokens,
|
||||
false,
|
||||
&format!("{prefix_doc_string}\n\n{suffix_doc_string}"),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
for (prefix, fields, prefix_doc_string) in border_prefixes() {
|
||||
for (suffix, width_tokens, suffix_doc_string) in border_suffixes() {
|
||||
methods.push(generate_predefined_setter(
|
||||
prefix,
|
||||
suffix,
|
||||
&fields,
|
||||
&width_tokens,
|
||||
false,
|
||||
&format!("{prefix_doc_string}\n\n{suffix_doc_string}"),
|
||||
));
|
||||
}
|
||||
}
|
||||
methods
|
||||
}
|
||||
|
||||
fn generate_predefined_setter(
|
||||
name: &'static str,
|
||||
length: &'static str,
|
||||
fields: &[TokenStream2],
|
||||
length_tokens: &TokenStream2,
|
||||
negate: bool,
|
||||
doc_string: &str,
|
||||
) -> TokenStream2 {
|
||||
let (negation_prefix, negation_token) = if negate {
|
||||
("neg_", quote! { - })
|
||||
} else {
|
||||
("", quote! {})
|
||||
};
|
||||
|
||||
let method_name = if length.is_empty() {
|
||||
format_ident!("{}{}", negation_prefix, name)
|
||||
} else {
|
||||
format_ident!("{}{}_{}", negation_prefix, name, length)
|
||||
};
|
||||
|
||||
let field_assignments = fields
|
||||
.iter()
|
||||
.map(|field_tokens| {
|
||||
quote! {
|
||||
style.#field_tokens = Some((#negation_token gpui::#length_tokens).into());
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let method = quote! {
|
||||
#[doc = #doc_string]
|
||||
fn #method_name(mut self) -> Self {
|
||||
let style = self.style();
|
||||
#(#field_assignments)*
|
||||
self
|
||||
}
|
||||
};
|
||||
|
||||
method
|
||||
}
|
||||
|
||||
fn generate_custom_value_setter(
|
||||
prefix: &'static str,
|
||||
length_type: TokenStream2,
|
||||
fields: &[TokenStream2],
|
||||
doc_string: &str,
|
||||
) -> TokenStream2 {
|
||||
let method_name = format_ident!("{}", prefix);
|
||||
|
||||
let mut iter = fields.iter();
|
||||
let last = iter.next_back().unwrap();
|
||||
let field_assignments = iter
|
||||
.map(|field_tokens| {
|
||||
quote! {
|
||||
style.#field_tokens = Some(length.clone().into());
|
||||
}
|
||||
})
|
||||
.chain(std::iter::once(quote! {
|
||||
style.#last = Some(length.into());
|
||||
}))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let method = quote! {
|
||||
#[doc = #doc_string]
|
||||
fn #method_name(mut self, length: impl std::clone::Clone + Into<gpui::#length_type>) -> Self {
|
||||
let style = self.style();
|
||||
#(#field_assignments)*
|
||||
self
|
||||
}
|
||||
};
|
||||
|
||||
method
|
||||
}
|
||||
|
||||
/// Returns a vec of (Property name, has 'auto' suffix, tokens for accessing the property, documentation)
|
||||
fn box_prefixes() -> Vec<(&'static str, bool, Vec<TokenStream2>, &'static str)> {
|
||||
vec![
|
||||
(
|
||||
"w",
|
||||
true,
|
||||
vec![quote! { size.width }],
|
||||
"Sets the width of the element. [Docs](https://tailwindcss.com/docs/width)",
|
||||
),
|
||||
("h", true, vec![quote! { size.height }], "Sets the height of the element. [Docs](https://tailwindcss.com/docs/height)"),
|
||||
(
|
||||
"size",
|
||||
true,
|
||||
vec![quote! {size.width}, quote! {size.height}],
|
||||
"Sets the width and height of the element."
|
||||
),
|
||||
// TODO: These don't use the same size ramp as the others
|
||||
// see https://tailwindcss.com/docs/max-width
|
||||
(
|
||||
"min_w",
|
||||
true,
|
||||
vec![quote! { min_size.width }],
|
||||
"Sets the minimum width of the element. [Docs](https://tailwindcss.com/docs/min-width)",
|
||||
),
|
||||
// TODO: These don't use the same size ramp as the others
|
||||
// see https://tailwindcss.com/docs/max-width
|
||||
(
|
||||
"min_h",
|
||||
true,
|
||||
vec![quote! { min_size.height }],
|
||||
"Sets the minimum height of the element. [Docs](https://tailwindcss.com/docs/min-height)",
|
||||
),
|
||||
// TODO: These don't use the same size ramp as the others
|
||||
// see https://tailwindcss.com/docs/max-width
|
||||
(
|
||||
"max_w",
|
||||
true,
|
||||
vec![quote! { max_size.width }],
|
||||
"Sets the maximum width of the element. [Docs](https://tailwindcss.com/docs/max-width)",
|
||||
),
|
||||
// TODO: These don't use the same size ramp as the others
|
||||
// see https://tailwindcss.com/docs/max-width
|
||||
(
|
||||
"max_h",
|
||||
true,
|
||||
vec![quote! { max_size.height }],
|
||||
"Sets the maximum height of the element. [Docs](https://tailwindcss.com/docs/max-height)",
|
||||
),
|
||||
(
|
||||
"m",
|
||||
true,
|
||||
vec![
|
||||
quote! { margin.top },
|
||||
quote! { margin.bottom },
|
||||
quote! { margin.left },
|
||||
quote! { margin.right },
|
||||
],
|
||||
"Sets the margin of the element. [Docs](https://tailwindcss.com/docs/margin)"
|
||||
),
|
||||
("mt", true, vec![quote! { margin.top }], "Sets the top margin of the element. [Docs](https://tailwindcss.com/docs/margin#add-margin-to-a-single-side)"),
|
||||
(
|
||||
"mb",
|
||||
true,
|
||||
vec![quote! { margin.bottom }],
|
||||
"Sets the bottom margin of the element. [Docs](https://tailwindcss.com/docs/margin#add-margin-to-a-single-side)"
|
||||
),
|
||||
(
|
||||
"my",
|
||||
true,
|
||||
vec![quote! { margin.top }, quote! { margin.bottom }],
|
||||
"Sets the vertical margin of the element. [Docs](https://tailwindcss.com/docs/margin#add-vertical-margin)"
|
||||
),
|
||||
(
|
||||
"mx",
|
||||
true,
|
||||
vec![quote! { margin.left }, quote! { margin.right }],
|
||||
"Sets the horizontal margin of the element. [Docs](https://tailwindcss.com/docs/margin#add-horizontal-margin)"
|
||||
),
|
||||
("ml", true, vec![quote! { margin.left }], "Sets the left margin of the element. [Docs](https://tailwindcss.com/docs/margin#add-margin-to-a-single-side)"),
|
||||
(
|
||||
"mr",
|
||||
true,
|
||||
vec![quote! { margin.right }],
|
||||
"Sets the right margin of the element. [Docs](https://tailwindcss.com/docs/margin#add-margin-to-a-single-side)"
|
||||
),
|
||||
(
|
||||
"p",
|
||||
false,
|
||||
vec![
|
||||
quote! { padding.top },
|
||||
quote! { padding.bottom },
|
||||
quote! { padding.left },
|
||||
quote! { padding.right },
|
||||
],
|
||||
"Sets the padding of the element. [Docs](https://tailwindcss.com/docs/padding)"
|
||||
),
|
||||
(
|
||||
"pt",
|
||||
false,
|
||||
vec![quote! { padding.top }],
|
||||
"Sets the top padding of the element. [Docs](https://tailwindcss.com/docs/padding#add-padding-to-a-single-side)"
|
||||
),
|
||||
(
|
||||
"pb",
|
||||
false,
|
||||
vec![quote! { padding.bottom }],
|
||||
"Sets the bottom padding of the element. [Docs](https://tailwindcss.com/docs/padding#add-padding-to-a-single-side)"
|
||||
),
|
||||
(
|
||||
"px",
|
||||
false,
|
||||
vec![quote! { padding.left }, quote! { padding.right }],
|
||||
"Sets the horizontal padding of the element. [Docs](https://tailwindcss.com/docs/padding#add-horizontal-padding)"
|
||||
),
|
||||
(
|
||||
"py",
|
||||
false,
|
||||
vec![quote! { padding.top }, quote! { padding.bottom }],
|
||||
"Sets the vertical padding of the element. [Docs](https://tailwindcss.com/docs/padding#add-vertical-padding)"
|
||||
),
|
||||
(
|
||||
"pl",
|
||||
false,
|
||||
vec![quote! { padding.left }],
|
||||
"Sets the left padding of the element. [Docs](https://tailwindcss.com/docs/padding#add-padding-to-a-single-side)"
|
||||
),
|
||||
(
|
||||
"pr",
|
||||
false,
|
||||
vec![quote! { padding.right }],
|
||||
"Sets the right padding of the element. [Docs](https://tailwindcss.com/docs/padding#add-padding-to-a-single-side)"
|
||||
),
|
||||
(
|
||||
"inset",
|
||||
true,
|
||||
vec![quote! { inset.top }, quote! { inset.right }, quote! { inset.bottom }, quote! { inset.left }],
|
||||
"Sets the top, right, bottom, and left values of a positioned element. [Docs](https://tailwindcss.com/docs/top-right-bottom-left)",
|
||||
),
|
||||
(
|
||||
"top",
|
||||
true,
|
||||
vec![quote! { inset.top }],
|
||||
"Sets the top value of a positioned element. [Docs](https://tailwindcss.com/docs/top-right-bottom-left)",
|
||||
),
|
||||
(
|
||||
"bottom",
|
||||
true,
|
||||
vec![quote! { inset.bottom }],
|
||||
"Sets the bottom value of a positioned element. [Docs](https://tailwindcss.com/docs/top-right-bottom-left)",
|
||||
),
|
||||
(
|
||||
"left",
|
||||
true,
|
||||
vec![quote! { inset.left }],
|
||||
"Sets the left value of a positioned element. [Docs](https://tailwindcss.com/docs/top-right-bottom-left)",
|
||||
),
|
||||
(
|
||||
"right",
|
||||
true,
|
||||
vec![quote! { inset.right }],
|
||||
"Sets the right value of a positioned element. [Docs](https://tailwindcss.com/docs/top-right-bottom-left)",
|
||||
),
|
||||
(
|
||||
"gap",
|
||||
false,
|
||||
vec![quote! { gap.width }, quote! { gap.height }],
|
||||
"Sets the gap between rows and columns in flex layouts. [Docs](https://tailwindcss.com/docs/gap)"
|
||||
),
|
||||
(
|
||||
"gap_x",
|
||||
false,
|
||||
vec![quote! { gap.width }],
|
||||
"Sets the gap between columns in flex layouts. [Docs](https://tailwindcss.com/docs/gap#changing-row-and-column-gaps-independently)"
|
||||
),
|
||||
(
|
||||
"gap_y",
|
||||
false,
|
||||
vec![quote! { gap.height }],
|
||||
"Sets the gap between rows in flex layouts. [Docs](https://tailwindcss.com/docs/gap#changing-row-and-column-gaps-independently)"
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
/// Returns a vec of (Suffix size, tokens that correspond to this size, documentation)
|
||||
fn box_suffixes() -> Vec<(&'static str, TokenStream2, &'static str)> {
|
||||
vec![
|
||||
("0", quote! { px(0.) }, "0px"),
|
||||
("0p5", quote! { rems(0.125) }, "2px (0.125rem)"),
|
||||
("1", quote! { rems(0.25) }, "4px (0.25rem)"),
|
||||
("1p5", quote! { rems(0.375) }, "6px (0.375rem)"),
|
||||
("2", quote! { rems(0.5) }, "8px (0.5rem)"),
|
||||
("2p5", quote! { rems(0.625) }, "10px (0.625rem)"),
|
||||
("3", quote! { rems(0.75) }, "12px (0.75rem)"),
|
||||
("3p5", quote! { rems(0.875) }, "14px (0.875rem)"),
|
||||
("4", quote! { rems(1.) }, "16px (1rem)"),
|
||||
("5", quote! { rems(1.25) }, "20px (1.25rem)"),
|
||||
("6", quote! { rems(1.5) }, "24px (1.5rem)"),
|
||||
("7", quote! { rems(1.75) }, "28px (1.75rem)"),
|
||||
("8", quote! { rems(2.0) }, "32px (2rem)"),
|
||||
("9", quote! { rems(2.25) }, "36px (2.25rem)"),
|
||||
("10", quote! { rems(2.5) }, "40px (2.5rem)"),
|
||||
("11", quote! { rems(2.75) }, "44px (2.75rem)"),
|
||||
("12", quote! { rems(3.) }, "48px (3rem)"),
|
||||
("16", quote! { rems(4.) }, "64px (4rem)"),
|
||||
("20", quote! { rems(5.) }, "80px (5rem)"),
|
||||
("24", quote! { rems(6.) }, "96px (6rem)"),
|
||||
("32", quote! { rems(8.) }, "128px (8rem)"),
|
||||
("40", quote! { rems(10.) }, "160px (10rem)"),
|
||||
("48", quote! { rems(12.) }, "192px (12rem)"),
|
||||
("56", quote! { rems(14.) }, "224px (14rem)"),
|
||||
("64", quote! { rems(16.) }, "256px (16rem)"),
|
||||
("72", quote! { rems(18.) }, "288px (18rem)"),
|
||||
("80", quote! { rems(20.) }, "320px (20rem)"),
|
||||
("96", quote! { rems(24.) }, "384px (24rem)"),
|
||||
("auto", quote! { auto() }, "Auto"),
|
||||
("px", quote! { px(1.) }, "1px"),
|
||||
("full", quote! { relative(1.) }, "100%"),
|
||||
("1_2", quote! { relative(0.5) }, "50% (1/2)"),
|
||||
("1_3", quote! { relative(1./3.) }, "33% (1/3)"),
|
||||
("2_3", quote! { relative(2./3.) }, "66% (2/3)"),
|
||||
("1_4", quote! { relative(0.25) }, "25% (1/4)"),
|
||||
("2_4", quote! { relative(0.5) }, "50% (2/4)"),
|
||||
("3_4", quote! { relative(0.75) }, "75% (3/4)"),
|
||||
("1_5", quote! { relative(0.2) }, "20% (1/5)"),
|
||||
("2_5", quote! { relative(0.4) }, "40% (2/5)"),
|
||||
("3_5", quote! { relative(0.6) }, "60% (3/5)"),
|
||||
("4_5", quote! { relative(0.8) }, "80% (4/5)"),
|
||||
("1_6", quote! { relative(1./6.) }, "16% (1/6)"),
|
||||
("5_6", quote! { relative(5./6.) }, "80% (5/6)"),
|
||||
("1_12", quote! { relative(1./12.) }, "8% (1/12)"),
|
||||
]
|
||||
}
|
||||
|
||||
fn corner_prefixes() -> Vec<(&'static str, Vec<TokenStream2>, &'static str)> {
|
||||
vec![
|
||||
(
|
||||
"rounded",
|
||||
vec![
|
||||
quote! { corner_radii.top_left },
|
||||
quote! { corner_radii.top_right },
|
||||
quote! { corner_radii.bottom_right },
|
||||
quote! { corner_radii.bottom_left },
|
||||
],
|
||||
"Sets the border radius of the element. [Docs](https://tailwindcss.com/docs/border-radius)"
|
||||
),
|
||||
(
|
||||
"rounded_t",
|
||||
vec![
|
||||
quote! { corner_radii.top_left },
|
||||
quote! { corner_radii.top_right },
|
||||
],
|
||||
"Sets the border radius of the top side of the element. [Docs](https://tailwindcss.com/docs/border-radius#rounding-sides-separately)"
|
||||
),
|
||||
(
|
||||
"rounded_b",
|
||||
vec![
|
||||
quote! { corner_radii.bottom_left },
|
||||
quote! { corner_radii.bottom_right },
|
||||
],
|
||||
"Sets the border radius of the bottom side of the element. [Docs](https://tailwindcss.com/docs/border-radius#rounding-sides-separately)"
|
||||
),
|
||||
(
|
||||
"rounded_r",
|
||||
vec![
|
||||
quote! { corner_radii.top_right },
|
||||
quote! { corner_radii.bottom_right },
|
||||
],
|
||||
"Sets the border radius of the right side of the element. [Docs](https://tailwindcss.com/docs/border-radius#rounding-sides-separately)"
|
||||
),
|
||||
(
|
||||
"rounded_l",
|
||||
vec![
|
||||
quote! { corner_radii.top_left },
|
||||
quote! { corner_radii.bottom_left },
|
||||
],
|
||||
"Sets the border radius of the left side of the element. [Docs](https://tailwindcss.com/docs/border-radius#rounding-sides-separately)"
|
||||
),
|
||||
(
|
||||
"rounded_tl",
|
||||
vec![quote! { corner_radii.top_left }],
|
||||
"Sets the border radius of the top left corner of the element. [Docs](https://tailwindcss.com/docs/border-radius#rounding-corners-separately)"
|
||||
),
|
||||
(
|
||||
"rounded_tr",
|
||||
vec![quote! { corner_radii.top_right }],
|
||||
"Sets the border radius of the top right corner of the element. [Docs](https://tailwindcss.com/docs/border-radius#rounding-corners-separately)"
|
||||
),
|
||||
(
|
||||
"rounded_bl",
|
||||
vec![quote! { corner_radii.bottom_left }],
|
||||
"Sets the border radius of the bottom left corner of the element. [Docs](https://tailwindcss.com/docs/border-radius#rounding-corners-separately)"
|
||||
),
|
||||
(
|
||||
"rounded_br",
|
||||
vec![quote! { corner_radii.bottom_right }],
|
||||
"Sets the border radius of the bottom right corner of the element. [Docs](https://tailwindcss.com/docs/border-radius#rounding-corners-separately)"
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
fn corner_suffixes() -> Vec<(&'static str, TokenStream2, &'static str)> {
|
||||
vec![
|
||||
("none", quote! { px(0.) }, "0px"),
|
||||
("sm", quote! { rems(0.125) }, "2px (0.125rem)"),
|
||||
("md", quote! { rems(0.25) }, "4px (0.25rem)"),
|
||||
("lg", quote! { rems(0.5) }, "8px (0.5rem)"),
|
||||
("xl", quote! { rems(0.75) }, "12px (0.75rem)"),
|
||||
("2xl", quote! { rems(1.) }, "16px (1rem)"),
|
||||
("3xl", quote! { rems(1.5) }, "24px (1.5rem)"),
|
||||
("full", quote! { px(9999.) }, "9999px"),
|
||||
]
|
||||
}
|
||||
|
||||
fn border_prefixes() -> Vec<(&'static str, Vec<TokenStream2>, &'static str)> {
|
||||
vec![
|
||||
(
|
||||
"border",
|
||||
vec![
|
||||
quote! { border_widths.top },
|
||||
quote! { border_widths.right },
|
||||
quote! { border_widths.bottom },
|
||||
quote! { border_widths.left },
|
||||
],
|
||||
"Sets the border width of the element. [Docs](https://tailwindcss.com/docs/border-width)"
|
||||
),
|
||||
(
|
||||
"border_t",
|
||||
vec![quote! { border_widths.top }],
|
||||
"Sets the border width of the top side of the element. [Docs](https://tailwindcss.com/docs/border-width#individual-sides)"
|
||||
),
|
||||
(
|
||||
"border_b",
|
||||
vec![quote! { border_widths.bottom }],
|
||||
"Sets the border width of the bottom side of the element. [Docs](https://tailwindcss.com/docs/border-width#individual-sides)"
|
||||
),
|
||||
(
|
||||
"border_r",
|
||||
vec![quote! { border_widths.right }],
|
||||
"Sets the border width of the right side of the element. [Docs](https://tailwindcss.com/docs/border-width#individual-sides)"
|
||||
),
|
||||
(
|
||||
"border_l",
|
||||
vec![quote! { border_widths.left }],
|
||||
"Sets the border width of the left side of the element. [Docs](https://tailwindcss.com/docs/border-width#individual-sides)"
|
||||
),
|
||||
(
|
||||
"border_x",
|
||||
vec![
|
||||
quote! { border_widths.left },
|
||||
quote! { border_widths.right },
|
||||
],
|
||||
"Sets the border width of the vertical sides of the element. [Docs](https://tailwindcss.com/docs/border-width#horizontal-and-vertical-sides)"
|
||||
),
|
||||
(
|
||||
"border_y",
|
||||
vec![
|
||||
quote! { border_widths.top },
|
||||
quote! { border_widths.bottom },
|
||||
],
|
||||
"Sets the border width of the horizontal sides of the element. [Docs](https://tailwindcss.com/docs/border-width#horizontal-and-vertical-sides)"
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
fn border_suffixes() -> Vec<(&'static str, TokenStream2, &'static str)> {
|
||||
vec![
|
||||
("", quote! { px(1.)}, "1px"),
|
||||
("0", quote! { px(0.)}, "0px"),
|
||||
("1", quote! { px(1.) }, "1px"),
|
||||
("2", quote! { px(2.) }, "2px"),
|
||||
("3", quote! { px(3.) }, "3px"),
|
||||
("4", quote! { px(4.) }, "4px"),
|
||||
("5", quote! { px(5.) }, "5px"),
|
||||
("6", quote! { px(6.) }, "6px"),
|
||||
("7", quote! { px(7.) }, "7px"),
|
||||
("8", quote! { px(8.) }, "8px"),
|
||||
("9", quote! { px(9.) }, "9px"),
|
||||
("10", quote! { px(10.) }, "10px"),
|
||||
("11", quote! { px(11.) }, "11px"),
|
||||
("12", quote! { px(12.) }, "12px"),
|
||||
("16", quote! { px(16.) }, "16px"),
|
||||
("20", quote! { px(20.) }, "20px"),
|
||||
("24", quote! { px(24.) }, "24px"),
|
||||
("32", quote! { px(32.) }, "32px"),
|
||||
]
|
||||
}
|
243
crates/gpui_macros/src/test.rs
Normal file
243
crates/gpui_macros/src/test.rs
Normal file
|
@ -0,0 +1,243 @@
|
|||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Ident;
|
||||
use quote::{format_ident, quote};
|
||||
use std::mem;
|
||||
use syn::{
|
||||
parse_macro_input, parse_quote, spanned::Spanned as _, AttributeArgs, FnArg, ItemFn, Lit, Meta,
|
||||
NestedMeta, Type,
|
||||
};
|
||||
|
||||
pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
let args = syn::parse_macro_input!(args as AttributeArgs);
|
||||
let mut max_retries = 0;
|
||||
let mut num_iterations = 1;
|
||||
let mut on_failure_fn_name = quote!(None);
|
||||
|
||||
for arg in args {
|
||||
match arg {
|
||||
NestedMeta::Meta(Meta::NameValue(meta)) => {
|
||||
let key_name = meta.path.get_ident().map(|i| i.to_string());
|
||||
let result = (|| {
|
||||
match key_name.as_deref() {
|
||||
Some("retries") => max_retries = parse_int(&meta.lit)?,
|
||||
Some("iterations") => num_iterations = parse_int(&meta.lit)?,
|
||||
Some("on_failure") => {
|
||||
if let Lit::Str(name) = meta.lit {
|
||||
let mut path = syn::Path {
|
||||
leading_colon: None,
|
||||
segments: Default::default(),
|
||||
};
|
||||
for part in name.value().split("::") {
|
||||
path.segments.push(Ident::new(part, name.span()).into());
|
||||
}
|
||||
on_failure_fn_name = quote!(Some(#path));
|
||||
} else {
|
||||
return Err(TokenStream::from(
|
||||
syn::Error::new(
|
||||
meta.lit.span(),
|
||||
"on_failure argument must be a string",
|
||||
)
|
||||
.into_compile_error(),
|
||||
));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(TokenStream::from(
|
||||
syn::Error::new(meta.path.span(), "invalid argument")
|
||||
.into_compile_error(),
|
||||
))
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
|
||||
if let Err(tokens) = result {
|
||||
return tokens;
|
||||
}
|
||||
}
|
||||
other => {
|
||||
return TokenStream::from(
|
||||
syn::Error::new_spanned(other, "invalid argument").into_compile_error(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut inner_fn = parse_macro_input!(function as ItemFn);
|
||||
if max_retries > 0 && num_iterations > 1 {
|
||||
return TokenStream::from(
|
||||
syn::Error::new_spanned(inner_fn, "retries and randomized iterations can't be mixed")
|
||||
.into_compile_error(),
|
||||
);
|
||||
}
|
||||
let inner_fn_attributes = mem::take(&mut inner_fn.attrs);
|
||||
let inner_fn_name = format_ident!("_{}", inner_fn.sig.ident);
|
||||
let outer_fn_name = mem::replace(&mut inner_fn.sig.ident, inner_fn_name.clone());
|
||||
|
||||
let mut outer_fn: ItemFn = if inner_fn.sig.asyncness.is_some() {
|
||||
// Pass to the test function the number of app contexts that it needs,
|
||||
// based on its parameter list.
|
||||
let mut cx_vars = proc_macro2::TokenStream::new();
|
||||
let mut cx_teardowns = proc_macro2::TokenStream::new();
|
||||
let mut inner_fn_args = proc_macro2::TokenStream::new();
|
||||
for (ix, arg) in inner_fn.sig.inputs.iter().enumerate() {
|
||||
if let FnArg::Typed(arg) = arg {
|
||||
if let Type::Path(ty) = &*arg.ty {
|
||||
let last_segment = ty.path.segments.last();
|
||||
match last_segment.map(|s| s.ident.to_string()).as_deref() {
|
||||
Some("StdRng") => {
|
||||
inner_fn_args.extend(quote!(rand::SeedableRng::seed_from_u64(_seed),));
|
||||
continue;
|
||||
}
|
||||
Some("BackgroundExecutor") => {
|
||||
inner_fn_args.extend(quote!(gpui::BackgroundExecutor::new(
|
||||
std::sync::Arc::new(dispatcher.clone()),
|
||||
),));
|
||||
continue;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else if let Type::Reference(ty) = &*arg.ty {
|
||||
if let Type::Path(ty) = &*ty.elem {
|
||||
let last_segment = ty.path.segments.last();
|
||||
if let Some("TestAppContext") =
|
||||
last_segment.map(|s| s.ident.to_string()).as_deref()
|
||||
{
|
||||
let cx_varname = format_ident!("cx_{}", ix);
|
||||
cx_vars.extend(quote!(
|
||||
let mut #cx_varname = gpui::TestAppContext::new(
|
||||
dispatcher.clone()
|
||||
);
|
||||
));
|
||||
cx_teardowns.extend(quote!(
|
||||
dispatcher.run_until_parked();
|
||||
#cx_varname.quit();
|
||||
dispatcher.run_until_parked();
|
||||
));
|
||||
inner_fn_args.extend(quote!(&mut #cx_varname,));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return TokenStream::from(
|
||||
syn::Error::new_spanned(arg, "invalid argument").into_compile_error(),
|
||||
);
|
||||
}
|
||||
|
||||
parse_quote! {
|
||||
#[test]
|
||||
fn #outer_fn_name() {
|
||||
#inner_fn
|
||||
|
||||
gpui::run_test(
|
||||
#num_iterations as u64,
|
||||
#max_retries,
|
||||
&mut |dispatcher, _seed| {
|
||||
let executor = gpui::BackgroundExecutor::new(std::sync::Arc::new(dispatcher.clone()));
|
||||
#cx_vars
|
||||
executor.block_test(#inner_fn_name(#inner_fn_args));
|
||||
#cx_teardowns
|
||||
},
|
||||
#on_failure_fn_name,
|
||||
stringify!(#outer_fn_name).to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Pass to the test function the number of app contexts that it needs,
|
||||
// based on its parameter list.
|
||||
let mut cx_vars = proc_macro2::TokenStream::new();
|
||||
let mut cx_teardowns = proc_macro2::TokenStream::new();
|
||||
let mut inner_fn_args = proc_macro2::TokenStream::new();
|
||||
for (ix, arg) in inner_fn.sig.inputs.iter().enumerate() {
|
||||
if let FnArg::Typed(arg) = arg {
|
||||
if let Type::Path(ty) = &*arg.ty {
|
||||
let last_segment = ty.path.segments.last();
|
||||
|
||||
if let Some("StdRng") = last_segment.map(|s| s.ident.to_string()).as_deref() {
|
||||
inner_fn_args.extend(quote!(rand::SeedableRng::seed_from_u64(_seed),));
|
||||
continue;
|
||||
}
|
||||
} else if let Type::Reference(ty) = &*arg.ty {
|
||||
if let Type::Path(ty) = &*ty.elem {
|
||||
let last_segment = ty.path.segments.last();
|
||||
match last_segment.map(|s| s.ident.to_string()).as_deref() {
|
||||
Some("AppContext") => {
|
||||
let cx_varname = format_ident!("cx_{}", ix);
|
||||
let cx_varname_lock = format_ident!("cx_{}_lock", ix);
|
||||
cx_vars.extend(quote!(
|
||||
let mut #cx_varname = gpui::TestAppContext::new(
|
||||
dispatcher.clone()
|
||||
);
|
||||
let mut #cx_varname_lock = #cx_varname.app.borrow_mut();
|
||||
));
|
||||
inner_fn_args.extend(quote!(&mut #cx_varname_lock,));
|
||||
cx_teardowns.extend(quote!(
|
||||
drop(#cx_varname_lock);
|
||||
dispatcher.run_until_parked();
|
||||
#cx_varname.update(|cx| { cx.quit() });
|
||||
dispatcher.run_until_parked();
|
||||
));
|
||||
continue;
|
||||
}
|
||||
Some("TestAppContext") => {
|
||||
let cx_varname = format_ident!("cx_{}", ix);
|
||||
cx_vars.extend(quote!(
|
||||
let mut #cx_varname = gpui::TestAppContext::new(
|
||||
dispatcher.clone()
|
||||
);
|
||||
));
|
||||
cx_teardowns.extend(quote!(
|
||||
dispatcher.run_until_parked();
|
||||
#cx_varname.quit();
|
||||
dispatcher.run_until_parked();
|
||||
));
|
||||
inner_fn_args.extend(quote!(&mut #cx_varname,));
|
||||
continue;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return TokenStream::from(
|
||||
syn::Error::new_spanned(arg, "invalid argument").into_compile_error(),
|
||||
);
|
||||
}
|
||||
|
||||
parse_quote! {
|
||||
#[test]
|
||||
fn #outer_fn_name() {
|
||||
#inner_fn
|
||||
|
||||
gpui::run_test(
|
||||
#num_iterations as u64,
|
||||
#max_retries,
|
||||
&mut |dispatcher, _seed| {
|
||||
#cx_vars
|
||||
#inner_fn_name(#inner_fn_args);
|
||||
#cx_teardowns
|
||||
},
|
||||
#on_failure_fn_name,
|
||||
stringify!(#outer_fn_name).to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
outer_fn.attrs.extend(inner_fn_attributes);
|
||||
|
||||
TokenStream::from(quote!(#outer_fn))
|
||||
}
|
||||
|
||||
fn parse_int(literal: &Lit) -> Result<usize, TokenStream> {
|
||||
let result = if let Lit::Int(int) = &literal {
|
||||
int.base10_parse()
|
||||
} else {
|
||||
Err(syn::Error::new(literal.span(), "must be an integer"))
|
||||
};
|
||||
|
||||
result.map_err(|err| TokenStream::from(err.into_compile_error()))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue