Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
extend to_string space handling
  • Loading branch information
cyrgani committed Nov 5, 2025
commit 485c0a146b268ecac2ab8496ccc3048eeedf2f0f
47 changes: 40 additions & 7 deletions library/proc_macro/src/bridge/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ impl server::TokenStream for NoRustc {
}

let mut s = String::new();
for tree in &tokens.0 {
s.push_str(&match tree {
let mut last = String::new();
let mut second_last = String::new();

for (idx, tree) in tokens.0.iter().enumerate() {
let mut space = true;
let new_part = match tree {
TokenTree::Group(group) => {
let inner = if let Some(stream) = &group.stream {
self.to_string(stream)
Expand All @@ -177,7 +181,13 @@ impl server::TokenStream for NoRustc {
};
match group.delimiter {
Delimiter::Parenthesis => format!("({inner})"),
Delimiter::Brace => format!("{{{inner}}}"),
Delimiter::Brace => {
if inner.is_empty() {
"{ }".to_string()
} else {
format!("{{ {inner} }}")
}
}
Delimiter::Bracket => format!("[{inner}]"),
Delimiter::None => inner,
}
Expand Down Expand Up @@ -212,11 +222,34 @@ impl server::TokenStream for NoRustc {
LitKind::StrRaw(raw) => format!("r{0}\"{inner}\"{0}", get_hashes_str(raw)),
}
}
TokenTree::Punct(punct) => (punct.ch as char).to_string(),
});
s.push(' ');
TokenTree::Punct(punct) => {
let c = punct.ch as char;
if c == '\'' {
space = false;
}
c.to_string()
}
};

const NON_SEPARATABLE_TOKENS: &[(char, char)] = &[(':', ':'), ('-', '>')];

for (first, second) in NON_SEPARATABLE_TOKENS {
if second_last == first.to_string() && last == second.to_string() && new_part != ":"
{
s.pop(); // pop ' '
s.pop(); // pop `second`
s.pop(); // pop ' '
s.push(*second);
s.push(' ');
}
}
s.push_str(&new_part);
second_last = last;
last = new_part;
if space && idx + 1 != tokens.0.len() {
s.push(' ');
}
}
s.pop();
s
}

Expand Down