diff options
author | Edwin Cheng <[email protected]> | 2020-12-31 05:01:15 +0000 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2020-12-31 05:36:19 +0000 |
commit | 1bd1830f507ee4ca63d65588e0eb8855bc825f23 (patch) | |
tree | 3279a35ba6f22e7cdf22757bc2ce646c44cb5aa2 | |
parent | bed7be9ed9f3eb905ae1c377f154fe0990b4c87f (diff) |
Fix spacing in proc-macro tokens to_string
-rw-r--r-- | crates/proc_macro_srv/src/rustc_server.rs | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/crates/proc_macro_srv/src/rustc_server.rs b/crates/proc_macro_srv/src/rustc_server.rs index 503f4c101..b54aa1f3b 100644 --- a/crates/proc_macro_srv/src/rustc_server.rs +++ b/crates/proc_macro_srv/src/rustc_server.rs | |||
@@ -204,17 +204,18 @@ pub mod token_stream { | |||
204 | let content = subtree | 204 | let content = subtree |
205 | .token_trees | 205 | .token_trees |
206 | .iter() | 206 | .iter() |
207 | .map(|tkn| { | 207 | .fold((String::new(), true), |(last, last_to_joint), tkn| { |
208 | let s = to_text(tkn); | 208 | let s = [last, to_text(tkn)].join(if last_to_joint { "" } else { " " }); |
209 | let mut is_joint = false; | ||
209 | if let tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) = tkn { | 210 | if let tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) = tkn { |
210 | if punct.spacing == tt::Spacing::Alone { | 211 | if punct.spacing == tt::Spacing::Joint { |
211 | return s + " "; | 212 | is_joint = true; |
212 | } | 213 | } |
213 | } | 214 | } |
214 | s | 215 | (s, is_joint) |
215 | }) | 216 | }) |
216 | .collect::<Vec<_>>() | 217 | .0; |
217 | .concat(); | 218 | |
218 | let (open, close) = match subtree.delimiter.map(|it| it.kind) { | 219 | let (open, close) = match subtree.delimiter.map(|it| it.kind) { |
219 | None => ("", ""), | 220 | None => ("", ""), |
220 | Some(tt::DelimiterKind::Brace) => ("{", "}"), | 221 | Some(tt::DelimiterKind::Brace) => ("{", "}"), |
@@ -710,4 +711,32 @@ mod tests { | |||
710 | assert_eq!(srv.character('c').text, "'c'"); | 711 | assert_eq!(srv.character('c').text, "'c'"); |
711 | assert_eq!(srv.byte_string(b"1234586\x88").text, "b\"1234586\\x88\""); | 712 | assert_eq!(srv.byte_string(b"1234586\x88").text, "b\"1234586\\x88\""); |
712 | } | 713 | } |
714 | |||
715 | #[test] | ||
716 | fn test_rustc_server_to_string() { | ||
717 | let s = TokenStream { | ||
718 | subtree: tt::Subtree { | ||
719 | delimiter: None, | ||
720 | token_trees: vec![ | ||
721 | tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident { | ||
722 | text: "struct".into(), | ||
723 | id: tt::TokenId::unspecified(), | ||
724 | })), | ||
725 | tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident { | ||
726 | text: "T".into(), | ||
727 | id: tt::TokenId::unspecified(), | ||
728 | })), | ||
729 | tt::TokenTree::Subtree(tt::Subtree { | ||
730 | delimiter: Some(tt::Delimiter { | ||
731 | id: tt::TokenId::unspecified(), | ||
732 | kind: tt::DelimiterKind::Brace, | ||
733 | }), | ||
734 | token_trees: vec![], | ||
735 | }), | ||
736 | ], | ||
737 | }, | ||
738 | }; | ||
739 | |||
740 | assert_eq!(s.to_string(), "struct T {}"); | ||
741 | } | ||
713 | } | 742 | } |