1 //! Utilities for formatting macro expanded nodes until we get a proper formatter.
7 SyntaxNode, SyntaxToken, WalkEvent, T,
10 // FIXME: It would also be cool to share logic here and in the mbe tests,
11 // which are pretty unreadable at the moment.
12 /// Renders a [`SyntaxNode`] with whitespace inserted between tokens that require them.
13 pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
15 let mut last: Option<SyntaxKind> = None;
16 let mut mods = Vec::new();
17 let syn = syn.clone_subtree().clone_for_update();
19 let before = Position::before;
20 let after = Position::after;
22 let do_indent = |pos: fn(_) -> Position, token: &SyntaxToken, indent| {
23 (pos(token.clone()), make::tokens::whitespace(&" ".repeat(2 * indent)))
25 let do_ws = |pos: fn(_) -> Position, token: &SyntaxToken| {
26 (pos(token.clone()), make::tokens::single_space())
28 let do_nl = |pos: fn(_) -> Position, token: &SyntaxToken| {
29 (pos(token.clone()), make::tokens::single_newline())
32 for event in syn.preorder_with_tokens() {
33 let token = match event {
34 WalkEvent::Enter(NodeOrToken::Token(token)) => token,
35 WalkEvent::Leave(NodeOrToken::Node(node))
38 ATTR | MATCH_ARM | STRUCT | ENUM | UNION | FN | IMPL | MACRO_RULES
43 Position::after(node.clone()),
44 make::tokens::whitespace(&" ".repeat(2 * indent)),
47 if node.parent().is_some() {
48 mods.push((Position::after(node), make::tokens::single_newline()));
56 let is_next = |f: fn(SyntaxKind) -> bool, default| -> bool {
57 tok.next_token().map(|it| f(it.kind())).unwrap_or(default)
60 |f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
63 k if is_text(k) && is_next(|it| !it.is_punct() || it == UNDERSCORE, false) => {
64 mods.push(do_ws(after, tok));
66 L_CURLY if is_next(|it| it != R_CURLY, true) => {
68 if is_last(is_text, false) {
69 mods.push(do_ws(before, tok));
72 mods.push(do_indent(after, tok, indent));
73 mods.push(do_nl(after, tok));
75 R_CURLY if is_last(|it| it != L_CURLY, true) => {
76 indent = indent.saturating_sub(1);
79 mods.push(do_indent(before, tok, indent));
81 mods.push(do_nl(before, tok));
85 mods.push(do_indent(after, tok, indent));
87 mods.push(do_nl(after, tok));
89 LIFETIME_IDENT if is_next(is_text, true) => {
90 mods.push(do_ws(after, tok));
92 MUT_KW if is_next(|it| it == SELF_KW, false) => {
93 mods.push(do_ws(after, tok));
95 AS_KW | DYN_KW | IMPL_KW | CONST_KW => {
96 mods.push(do_ws(after, tok));
98 T![;] if is_next(|it| it != R_CURLY, true) => {
100 mods.push(do_indent(after, tok, indent));
102 mods.push(do_nl(after, tok));
104 T![=] if is_next(|it| it == T![>], false) => {
105 // FIXME: this branch is for `=>` in macro_rules!, which is currently parsed as
106 // two separate symbols.
107 mods.push(do_ws(before, tok));
108 mods.push(do_ws(after, &tok.next_token().unwrap()));
110 T![->] | T![=] | T![=>] => {
111 mods.push(do_ws(before, tok));
112 mods.push(do_ws(after, tok));
114 T![!] if is_last(|it| it == MACRO_RULES_KW, false) && is_next(is_text, false) => {
115 mods.push(do_ws(after, tok));
120 last = Some(tok.kind());
123 for (pos, insert) in mods {
124 ted::insert(pos, insert);
127 if let Some(it) = syn.last_token().filter(|it| it.kind() == SyntaxKind::WHITESPACE) {
134 fn is_text(k: SyntaxKind) -> bool {
135 k.is_keyword() || k.is_literal() || k == IDENT || k == UNDERSCORE