diff options
-rw-r--r-- | src/token_stream.rs | 59 |
1 files changed, 33 insertions, 26 deletions
diff --git a/src/token_stream.rs b/src/token_stream.rs index 13df5ba..99e8ca5 100644 --- a/src/token_stream.rs +++ b/src/token_stream.rs @@ -34,37 +34,40 @@ fn recurse_replace_ident( token_stream .into_iter() .windows() - .filter_map(|(token_tree, next_token_tree)| match token_tree { - TokenTree::Punct(punct) if punct.as_char() == prefix => { - if matches!(next_token_tree, Some(TokenTree::Ident(ident)) if &ident == target_ident) { - prev_token_was_prefix = true; - - None - } - else { - prev_token_was_prefix = false; + .filter_map(|(token_tree, next_token_tree)| { + let output = match token_tree { + TokenTree::Punct(punct) if punct.as_char() == prefix => { + if matches!( + next_token_tree, + Some(TokenTree::Ident(ident)) if &ident == target_ident + ) { + prev_token_was_prefix = true; + + return None; + } Some(TokenTree::Punct(punct)) } - } - TokenTree::Ident(ident) if prev_token_was_prefix && &ident == target_ident => { - prev_token_was_prefix = false; - - Some(substitution.clone()) - } - TokenTree::Group(group) => { - prev_token_was_prefix = false; - - Some(TokenTree::Group(Group::new( + TokenTree::Ident(ident) + if prev_token_was_prefix && &ident == target_ident => + { + Some(substitution.clone()) + } + TokenTree::Group(group) => Some(TokenTree::Group(Group::new( group.delimiter(), - recurse_replace_ident(group.stream(), target_ident, substitution, prefix), - ))) - } - tt => { - prev_token_was_prefix = false; + recurse_replace_ident( + group.stream(), + target_ident, + substitution, + prefix, + ), + ))), + tt => Some(tt), + }; - Some(tt) - } + prev_token_was_prefix = false; + + output }) .collect() } @@ -120,6 +123,8 @@ mod tests assert_eq!( quote! { let abc = (hello, "123").iter_map(|_| #ht xyz); + + fn #ht hello() {} } .replace_ident( &format_ident!("xyz"), @@ -128,6 +133,8 @@ mod tests .to_string(), quote! { let abc = (hello, "123").iter_map(|_| foo); + + fn #ht hello() {} } .to_string() ); |