Add a mirror function-like proc-macro expander for tests

This commit is contained in:
Lukas Wirth 2021-09-21 14:55:54 +02:00
parent 8b1e8197fe
commit ba84b91e78
4 changed files with 45 additions and 10 deletions

View File

@ -262,6 +262,10 @@ pub fn identity(_attr: TokenStream, item: TokenStream) -> TokenStream {
pub fn input_replace(attr: TokenStream, _item: TokenStream) -> TokenStream {
attr
}
#[proc_macro]
pub fn mirror(input: TokenStream) -> TokenStream {
input
}
"#;
let proc_macros = std::array::IntoIter::new([
ProcMacro {
@ -274,6 +278,11 @@ pub fn input_replace(attr: TokenStream, _item: TokenStream) -> TokenStream {
kind: crate::ProcMacroKind::Attr,
expander: Arc::new(AttributeInputReplaceProcMacroExpander),
},
ProcMacro {
name: "mirror".into(),
kind: crate::ProcMacroKind::FuncLike,
expander: Arc::new(MirrorProcMacroExpander),
},
])
.filter(|pm| proc_macros.iter().any(|name| name == pm.name))
.collect();
@ -348,3 +357,28 @@ fn expand(
.ok_or_else(|| ProcMacroExpansionError::Panic("Expected attribute input".into()))
}
}
#[derive(Debug)]
struct MirrorProcMacroExpander;
impl ProcMacroExpander for MirrorProcMacroExpander {
fn expand(
&self,
input: &Subtree,
_: Option<&Subtree>,
_: &Env,
) -> Result<Subtree, ProcMacroExpansionError> {
fn traverse(input: &Subtree) -> Subtree {
let mut res = Subtree::default();
res.delimiter = input.delimiter;
for tt in input.token_trees.iter().rev() {
let tt = match tt {
tt::TokenTree::Leaf(leaf) => tt::TokenTree::Leaf(leaf.clone()),
tt::TokenTree::Subtree(sub) => tt::TokenTree::Subtree(traverse(sub)),
};
res.token_trees.push(tt);
}
res
}
Ok(traverse(input))
}
}

View File

@ -64,10 +64,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="keyword">pub</span> <span class="keyword">trait</span> <span class="trait declaration public">Fn</span><span class="angle">&lt;</span><span class="type_param declaration">Args</span><span class="angle">&gt;</span><span class="colon">:</span> <span class="trait public">FnMut</span><span class="angle">&lt;</span><span class="type_param">Args</span><span class="angle">&gt;</span> <span class="brace">{</span><span class="brace">}</span>
<span class="brace">}</span>
<span class="keyword">struct</span> <span class="struct declaration">Foo</span> <span class="brace">{</span>
<span class="keyword">pub</span> <span class="field declaration public">x</span><span class="colon">:</span> <span class="builtin_type">i32</span><span class="comma">,</span>
<span class="keyword">pub</span> <span class="field declaration public">y</span><span class="colon">:</span> <span class="builtin_type">i32</span><span class="comma">,</span>
proc_macros::<span class="macro">mirror!</span> <span class="brace">{</span>
<span class="brace">{</span>
<span class="comma">,</span><span class="builtin_type">i32</span> <span class="colon">:</span><span class="field declaration public">x</span> <span class="keyword">pub</span>
<span class="comma">,</span><span class="builtin_type">i32</span> <span class="colon">:</span><span class="field declaration public">y</span> <span class="keyword">pub</span>
<span class="brace">}</span> <span class="struct declaration">Foo</span> <span class="keyword">struct</span>
<span class="brace">}</span>
<span class="keyword">trait</span> <span class="trait declaration">Bar</span> <span class="keyword">where</span> <span class="type_param">Self</span><span class="colon">:</span> <span class="brace">{</span>

View File

@ -10,7 +10,7 @@
fn test_highlighting() {
check_highlighting(
r#"
//- proc_macros: identity
//- proc_macros: identity, mirror
//- /main.rs crate:main deps:foo
use inner::{self as inner_mod};
mod inner {}
@ -36,10 +36,11 @@ pub trait FnMut<Args>: FnOnce<Args> {}
pub trait Fn<Args>: FnMut<Args> {}
}
struct Foo {
pub x: i32,
pub y: i32,
proc_macros::mirror! {
{
,i32 :x pub
,i32 :y pub
} Foo struct
}
trait Bar where Self: {

View File

@ -32,7 +32,6 @@
// }
// }
// ```
pub(crate) fn convert_iter_for_each_to_for(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let method = ctx.find_node_at_offset::<ast::MethodCallExpr>()?;