mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Make stringify!
prettify its input
This will insert whitespace if the invocation is inside another macro
This commit is contained in:
parent
f22eea9053
commit
d05eae6ada
6 changed files with 48 additions and 133 deletions
|
@ -274,3 +274,36 @@ impl Subtree {
|
|||
}
|
||||
|
||||
pub mod buffer;
|
||||
|
||||
pub fn pretty(tkns: &[TokenTree]) -> String {
|
||||
fn tokentree_to_text(tkn: &TokenTree) -> String {
|
||||
match tkn {
|
||||
TokenTree::Leaf(Leaf::Ident(ident)) => ident.text.clone().into(),
|
||||
TokenTree::Leaf(Leaf::Literal(literal)) => literal.text.clone().into(),
|
||||
TokenTree::Leaf(Leaf::Punct(punct)) => format!("{}", punct.char),
|
||||
TokenTree::Subtree(subtree) => {
|
||||
let content = pretty(&subtree.token_trees);
|
||||
let (open, close) = match subtree.delimiter.map(|it| it.kind) {
|
||||
None => ("", ""),
|
||||
Some(DelimiterKind::Brace) => ("{", "}"),
|
||||
Some(DelimiterKind::Parenthesis) => ("(", ")"),
|
||||
Some(DelimiterKind::Bracket) => ("[", "]"),
|
||||
};
|
||||
format!("{}{}{}", open, content, close)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tkns.iter()
|
||||
.fold((String::new(), true), |(last, last_to_joint), tkn| {
|
||||
let s = [last, tokentree_to_text(tkn)].join(if last_to_joint { "" } else { " " });
|
||||
let mut is_joint = false;
|
||||
if let TokenTree::Leaf(Leaf::Punct(punct)) = tkn {
|
||||
if punct.spacing == Spacing::Joint {
|
||||
is_joint = true;
|
||||
}
|
||||
}
|
||||
(s, is_joint)
|
||||
})
|
||||
.0
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue