Skip to content

Commit

Permalink
add field completion hints for props
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmloff committed May 17, 2024
1 parent 3bf9176 commit 194899c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
22 changes: 21 additions & 1 deletion packages/core-macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ impl ComponentBody {
let completions_mod = Ident::new(&format!("{}_completions", comp_fn), comp_fn.span());

let vis = &self.item_fn.vis;
// We generate a list of required fields for the component so that rust analyzer can fill in the fields with a hint
let required_fields = self
.item_fn
.sig
.inputs
.iter()
.filter_map(|f| {
if let FnArg::Typed(pt) = f {
if let Pat::Ident(ident) = &*pt.pat {
if ident.ident == "props" {
return None;
}
}
}

Some(make_prop_struct_field(f, vis))
})
.collect::<Vec<_>>();

quote! {
#[allow(non_snake_case)]
Expand All @@ -242,7 +260,9 @@ impl ComponentBody {
#[allow(non_camel_case_types)]
/// This enum is generated to help autocomplete the braces after the component. It does nothing
pub enum Component {
#comp_fn {}
#comp_fn {
#(#required_fields),*
}
}
}

Expand Down
31 changes: 30 additions & 1 deletion packages/rsx/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use syn::{
ext::IdentExt, spanned::Spanned, token::Brace, AngleBracketedGenericArguments, Error, Expr,
Ident, LitStr, PathArguments, Token,
};
use tracing::span;

#[derive(Clone, Debug)]
pub struct Component {
Expand Down Expand Up @@ -146,6 +147,7 @@ impl ToTokens for Component {

let fn_name = self.fn_name();

let completion_hints = self.completion_hints();
let errors = self.errors();

let component_node = quote_spanned! { name.span() =>
Expand All @@ -159,11 +161,12 @@ impl ToTokens for Component {
})
};

let component = if errors.is_empty() {
let component = if errors.is_empty() && completion_hints.is_empty() {
component_node
} else {
quote_spanned! {
name.span() => {
#completion_hints
#errors
#component_node
}
Expand Down Expand Up @@ -283,6 +286,32 @@ impl Component {
self.name.segments.last().unwrap().ident.to_string()
}

/// We can add hints for rust analyzer to provide better hints for the component fields.
pub(crate) fn completion_hints(&self) -> TokenStream2 {
// If we have fields, we can't add any completion hints because completing the builder like a struct will create issues with optional fields
if !self.fields.is_empty() {
return quote! {};
}

let name = &self.name;
let braces = self
.brace
.as_ref()
.map(|b| {
let mut tokens = TokenStream2::new();
b.surround(&mut tokens, |_| {});
tokens
})
.unwrap_or_default();

quote! {
#[allow(dead_code)]
{
#name #braces
};
}
}

/// If this element is only partially complete, return the errors that occurred during parsing
pub(crate) fn errors(&self) -> TokenStream2 {
let Self { errors, .. } = self;
Expand Down

0 comments on commit 194899c

Please sign in to comment.