Auto merge of #12990 - edwin0cheng:improve-ws, r=Veykril

fix: Improve whitespace insertion in mbe

Related:  https://github.com/rust-lang/rust-analyzer/issues/12260#issuecomment-1126957162
This commit is contained in:
bors 2022-08-10 09:29:19 +00:00
commit d79d9e1a2e
9 changed files with 28 additions and 33 deletions

View File

@ -251,11 +251,15 @@ fn format_args_expand(
} }
for arg in &mut args { for arg in &mut args {
// Remove `key =`. // Remove `key =`.
if matches!(arg.token_trees.get(1), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p))) if p.char == '=' && p.spacing != tt::Spacing::Joint) if matches!(arg.token_trees.get(1), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p))) if p.char == '=')
{
// but not with `==`
if !matches!(arg.token_trees.get(2), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p))) if p.char == '=' )
{ {
arg.token_trees.drain(..2); arg.token_trees.drain(..2);
} }
} }
}
let _format_string = args.remove(0); let _format_string = args.remove(0);
let arg_tts = args.into_iter().flat_map(|arg| { let arg_tts = args.into_iter().flat_map(|arg| {
quote! { std::fmt::ArgumentV1::new(&(#arg), std::fmt::Display::fmt), } quote! { std::fmt::ArgumentV1::new(&(#arg), std::fmt::Display::fmt), }

View File

@ -228,16 +228,7 @@ struct StackEntry {
} }
let spacing = match conv.peek().map(|next| next.kind(conv)) { let spacing = match conv.peek().map(|next| next.kind(conv)) {
Some(kind) Some(kind) if !kind.is_trivia() => tt::Spacing::Joint,
if !kind.is_trivia()
&& kind.is_punct()
&& kind != T!['[']
&& kind != T!['{']
&& kind != T!['(']
&& kind != UNDERSCORE =>
{
tt::Spacing::Joint
}
_ => tt::Spacing::Alone, _ => tt::Spacing::Alone,
}; };
let char = match token.to_char(conv) { let char = match token.to_char(conv) {

View File

@ -19,7 +19,7 @@ fn test_derive_error() {
expect![[r##" expect![[r##"
SUBTREE $ SUBTREE $
IDENT compile_error 4294967295 IDENT compile_error 4294967295
PUNCH ! [alone] 4294967295 PUNCH ! [joint] 4294967295
SUBTREE () 4294967295 SUBTREE () 4294967295
LITERAL "#[derive(DeriveError)] struct S ;" 4294967295 LITERAL "#[derive(DeriveError)] struct S ;" 4294967295
PUNCH ; [alone] 4294967295"##]], PUNCH ; [alone] 4294967295"##]],
@ -109,7 +109,7 @@ fn test_fn_like_macro_clone_literals() {
PUNCH , [alone] 4294967295 PUNCH , [alone] 4294967295
LITERAL 2_u32 4294967295 LITERAL 2_u32 4294967295
PUNCH , [alone] 4294967295 PUNCH , [alone] 4294967295
PUNCH - [alone] 4294967295 PUNCH - [joint] 4294967295
LITERAL 4i64 4294967295 LITERAL 4i64 4294967295
PUNCH , [alone] 4294967295 PUNCH , [alone] 4294967295
LITERAL 3.14f32 4294967295 LITERAL 3.14f32 4294967295
@ -130,7 +130,7 @@ fn test_attr_macro() {
expect![[r##" expect![[r##"
SUBTREE $ SUBTREE $
IDENT compile_error 4294967295 IDENT compile_error 4294967295
PUNCH ! [alone] 4294967295 PUNCH ! [joint] 4294967295
SUBTREE () 4294967295 SUBTREE () 4294967295
LITERAL "#[attr_error(some arguments)] mod m {}" 4294967295 LITERAL "#[attr_error(some arguments)] mod m {}" 4294967295
PUNCH ; [alone] 4294967295"##]], PUNCH ; [alone] 4294967295"##]],