Skip to content

Commit

Permalink
fix: improved rust-analyzer support in #[component] macro?
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj committed Nov 25, 2023
1 parent a8e25af commit 49ef2bb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
30 changes: 23 additions & 7 deletions leptos_macro/src/component.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use attribute_derive::Attribute as AttributeDerive;
use convert_case::{
Case::{Pascal, Snake},
Case::{self, Pascal, Snake},
Casing,
};
use itertools::Itertools;
Expand Down Expand Up @@ -118,8 +118,6 @@ impl ToTokens for Model {

let no_props = props.is_empty();

let mut body = body.to_owned();

// check for components that end ;
if !is_transparent {
let ends_semi =
Expand All @@ -139,7 +137,7 @@ impl ToTokens for Model {
}
}

body.sig.ident = format_ident!("__{}", body.sig.ident);
let module_name = module_name_from_fn(body);
#[allow(clippy::redundant_clone)] // false positive
let body_name = body.sig.ident.clone();

Expand Down Expand Up @@ -237,12 +235,12 @@ impl ToTokens for Model {
let body_expr = if *is_island {
quote! {
::leptos::SharedContext::with_hydration(move || {
#body_name(#prop_names)
#module_name::#body_name(#prop_names)
})
}
} else {
quote! {
#body_name(#prop_names)
#module_name::#body_name(#prop_names)
}
};

Expand Down Expand Up @@ -367,7 +365,6 @@ impl ToTokens for Model {
.collect::<TokenStream>();

let body = quote! {
#body
#destructure_props
#tracing_span_expr
#component
Expand Down Expand Up @@ -1162,3 +1159,22 @@ fn is_valid_into_view_return_type(ty: &ReturnType) -> bool {
.iter()
.any(|test| ty == test)
}

pub fn module_name_from_fn(body: &ItemFn) -> Ident {
let snake = &body
.sig
.ident
.to_string()
.from_case(Case::Camel)
.to_case(Case::Snake);
let name = format!("component_module_{snake}");
Ident::new(&name, body.sig.ident.span())
}

pub fn strip_argument_attributes(fun: &mut ItemFn) {
for argument in fun.sig.inputs.iter_mut() {
if let FnArg::Typed(ref mut argument) = argument {
argument.attrs.clear();
}
}
}
53 changes: 40 additions & 13 deletions leptos_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use proc_macro::TokenStream;
use proc_macro2::{Span, TokenTree};
use quote::ToTokens;
use rstml::{node::KeyedAttribute, parse};
use syn::parse_macro_input;
use syn::{
parse_macro_input, spanned::Spanned, token::Pub, ItemFn, Visibility,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum Mode {
Expand All @@ -31,6 +33,7 @@ impl Default for Mode {

mod params;
mod view;
use crate::component::{module_name_from_fn, strip_argument_attributes};
use view::{client_template::render_template, render_view};
mod component;
mod server;
Expand Down Expand Up @@ -598,21 +601,45 @@ pub fn component(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
false
};

let parse_result = syn::parse::<component::Model>(s.clone());
let mut fn_result = syn::parse::<ItemFn>(s.clone());
let parse_result = syn::parse::<component::Model>(s);

if let Ok(model) = parse_result {
model
.is_transparent(is_transparent)
.into_token_stream()
.into()
if let (Ok(ref mut unexpanded), Ok(model)) = (&mut fn_result, parse_result)
{
let expanded = model.is_transparent(is_transparent).into_token_stream();
if !matches!(unexpanded.vis, Visibility::Public(_)) {
unexpanded.vis = Visibility::Public(Pub {
span: unexpanded.vis.span(),
})
}
let module_name = module_name_from_fn(unexpanded);
strip_argument_attributes(unexpanded);
quote! {
#expanded
#[doc(hidden)]
mod #module_name {
use super::*;

#[allow(non_snake_case, dead_code)]
#unexpanded
}
}
} else if let Ok(mut unexpanded) = fn_result {
let module_name = module_name_from_fn(&unexpanded);
strip_argument_attributes(&mut unexpanded);
quote! {
#[doc(hidden)]
mod #module_name {
use super::*;

#[allow(non_snake_case, dead_code)]
#unexpanded
}
}
} else {
// When the input syntax is invalid, e.g. while typing, we let
// the dummy model output tokens similar to the input, which improves
// IDEs and rust-analyzer's auto-complete capabilities.
parse_macro_input!(s as component::DummyModel)
.into_token_stream()
.into()
quote! {}
}
.into()
}

/// Defines a component as an interactive island when you are using the
Expand Down

0 comments on commit 49ef2bb

Please sign in to comment.