How to please ra for a better attribute macro experience? #15570
-
When I was writing the procedural macro, I noticed that ra was able to give me completion and highlighting when the property was parsed by syn to Ident. rustfmt can also format simple assignment expressions. #[transform(Person)]
struct People{} ra Detect procedural macros again for better completion? Are you saying this is just my imagination? If the answer is yes, then for complex attributes (such as slices), how can ra be completed, or rustfmt be able to format them? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi, it's rather unclear to me what you're asking. Could you give an example of where you expect to see completions and don't (if that's the question, I'm not sure)? |
Beta Was this translation helpful? Give feedback.
-
If I have a macro like this: #[proc_macro_attribute]
pub fn convert_to(attr: TokenStream, input: TokenStream) -> TokenStream {
let attr = parse_macro_input!(attr as syn::TypePath);
let input: DeriveInput = parse_macro_input!(input as DeriveInput);
let ident = input.ident.clone();
quote! {
#input
impl From<#ident> for #attr {
fn from(val: #ident)->Self{
todo!();
}
}
}
.into()
} then I can get code complete and higlight: But if I have a macro like this: #[proc_macro_attribute]
pub fn convert_to(_attr: TokenStream, input: TokenStream) -> TokenStream {
let input: DeriveInput = parse_macro_input!(input as DeriveInput);
quote! {
#input
}
.into()
} I can't get code complete and highlight: But for complex types this will: #[proc_macro_attribute]
pub fn convert_to(attr: TokenStream, input: TokenStream) -> TokenStream {
let attr = parse_macro_input!(attr as syn::ExprArray);
let input: DeriveInput = parse_macro_input!(input as DeriveInput);
let mut res = quote! {};
for attr in attr.elems {
match attr {
syn::Expr::Let(expr) => res.extend(quote! {
#expr;
}),
_ => todo!(),
}
}
quote! {
#input
fn fake_func(){
#res
}
}
.into()
} Ok, it works also. sorry... |
Beta Was this translation helpful? Give feedback.
If I read your example right, your second example where the completion doesn't work doesn't use the attribute contents. The way that RA understands what the tokens inside the attribute parentheses mean is by looking at the expanded result. If e.g. the token in the macro input shows up in the macro expansion as a type name (with a span referring to the original token from the input), RA knows that it's a type and can show corresponding completions. If the token isn't used, it means nothing and RA can't provide completions.
For your last example, it's worth noting that RA can only provide completion if the macro actually expands. If you have something like
#[convert_to([let abc = ])]
,syn
w…