Don't generate a space before a newline in token_stream_printer.rs
This isn't very important, because everything gets autoformatted away anyway -- everything except, unfortunately, `macro!(stuff here)` invocations, which are generated for e.g. forward declarations. So the token printer is going to need to support more precise/accurate formatting that doesn't produce lint errors, probably. :(
PiperOrigin-RevId: 442816428
diff --git a/common/token_stream_printer.rs b/common/token_stream_printer.rs
index 99081ca..997d052 100644
--- a/common/token_stream_printer.rs
+++ b/common/token_stream_printer.rs
@@ -79,7 +79,11 @@
}
fn is_ident_or_literal(tt: &TokenTree) -> bool {
- matches!(tt, TokenTree::Ident(_) | TokenTree::Literal(_))
+ match tt {
+ TokenTree::Ident(id) if id == "__NEWLINE__" => false,
+ TokenTree::Ident(_) | TokenTree::Literal(_) => true,
+ _ => false,
+ }
}
fn rustfmt(input: String) -> Result<String> {
@@ -158,7 +162,7 @@
#[test]
fn test_newline_token() -> Result<()> {
let token_stream = quote! { a __NEWLINE__ b };
- assert_eq!(tokens_to_string(token_stream)?, "a \nb");
+ assert_eq!(tokens_to_string(token_stream)?, "a\nb");
Ok(())
}
@@ -220,7 +224,7 @@
#[test]
fn test_special_tokens_in_groups() -> Result<()> {
- assert_eq!(tokens_to_string(quote! {{ a __NEWLINE__ b }})?, "{ a \nb }");
+ assert_eq!(tokens_to_string(quote! {{ a __NEWLINE__ b }})?, "{ a\nb }");
assert_eq!(tokens_to_string(quote! {(a __COMMENT__ "b")})?, "(a // b\n)");
assert_eq!(tokens_to_string(quote! {[__HASH_TOKEN__ a]})?, "[#a]");
Ok(())