I want to make path completion in macro but I have no idea how to do it #10106
-
Basicly I have a macro to open file with path from given string, and I want to make autocomplete to work with this macro but it only work in first level directory. #[proc_macro]
pub fn path(path: proc_macro::TokenStream) -> proc_macro::TokenStream {
let path = if let Ok(path) = syn::parse::<syn::LitStr>(path) {
path
} else {
return proc_macro::TokenStream::from(quote::quote! {compile_error!("Expected &'static str!");});
};
match std::fs::File::open(path.value()) {
Ok(_file) => {
// TODO:
}
Err(_err) => {
if std::env::var_os("RUST_IDE_PROC_MACRO_COMPLETION").is_none() &&
std::env::var_os("RUST_IDE_PROC_MACRO_COMPLETION_DUMMY_IDENTIFIER").is_none() {
// // uncomment for error checking
// // but I don't know how to handle `RUST_IDE_PROC_MACRO_COMPLETION` correctly
// let span = quote::__private::Span::call_site();
// let lit = syn::Lit::Str(syn::LitStr::new(&_err.to_string(), span));
// return proc_macro::TokenStream::from(quote::quote_spanned! {span=>
// compile_error!(#lit);
// });
}
}
}
proc_macro::TokenStream::from(quote::quote! {
static __PATH: fn() -> &'static std::path::Path = || std::path::Path::new(#path);
})
} here is my result. can someone help? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi! By the way, I'd suggest to check whether the macro is invoked from an IDE for completion purposes using this function: fn is_ide_completion() -> bool {
match std::env::var_os("RUST_IDE_PROC_MACRO_COMPLETION_DUMMY_IDENTIFIER") {
None => false,
Some(dummy_identifier) => !dummy_identifier.is_empty(),
}
} But again, in your case it seems like checking these variables is not needed. |
Beta Was this translation helpful? Give feedback.
Hi!
You seem to be doing everything right, but there is a bug is on our side. That is, any special handling of IDE completion in your macro should not be needed and just expanding to
std::path::Path::new(#path)
should work. I'm not sure what exactly the bug is, I'll try to investigate it.By the way, I'd suggest to check whether the macro is invoked from an IDE for completion purposes using this function:
But again, in your case it seems like checking these variables is not needed.