Skip to content

Commit f1350dd

Browse files
committed
add expand log
1 parent f0b7c02 commit f1350dd

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

crates/hir_expand/src/db.rs

+2
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ fn parse_macro_with_arg(
340340
None => return ExpandResult { value: None, err: result.err },
341341
};
342342

343+
log::debug!("expanded = {}", tt.as_debug_string());
344+
343345
let fragment_kind = to_fragment_kind(db, macro_call_id);
344346

345347
let (parse, rev_token_map) = match mbe::token_tree_to_syntax_node(&tt, fragment_kind) {

crates/tt/src/lib.rs

+47
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,53 @@ impl Subtree {
227227
}
228228
}
229229

230+
impl Subtree {
231+
/// A simple line string used for debugging
232+
pub fn as_debug_string(&self) -> String {
233+
let delim = match self.delimiter_kind() {
234+
Some(DelimiterKind::Brace) => ("{", "}"),
235+
Some(DelimiterKind::Bracket) => ("[", "]"),
236+
Some(DelimiterKind::Parenthesis) => ("(", ")"),
237+
None => (" ", " "),
238+
};
239+
240+
let mut res = String::new();
241+
res.push_str(delim.0);
242+
let mut iter = self.token_trees.iter();
243+
let mut last = None;
244+
while let Some(child) = iter.next() {
245+
let s = match child {
246+
TokenTree::Leaf(it) => {
247+
let s = match it {
248+
Leaf::Literal(it) => it.text.to_string(),
249+
Leaf::Punct(it) => it.char.to_string(),
250+
Leaf::Ident(it) => it.text.to_string(),
251+
};
252+
match (it, last) {
253+
(Leaf::Ident(_), Some(&TokenTree::Leaf(Leaf::Ident(_)))) => {
254+
" ".to_string() + &s
255+
}
256+
(Leaf::Punct(_), Some(&TokenTree::Leaf(Leaf::Punct(punct)))) => {
257+
if punct.spacing == Spacing::Alone {
258+
" ".to_string() + &s
259+
} else {
260+
s
261+
}
262+
}
263+
_ => s,
264+
}
265+
}
266+
TokenTree::Subtree(it) => it.as_debug_string(),
267+
};
268+
res.push_str(&s);
269+
last = Some(child);
270+
}
271+
272+
res.push_str(delim.1);
273+
res
274+
}
275+
}
276+
230277
pub mod buffer;
231278

232279
#[derive(Debug, PartialEq, Eq, Clone)]

0 commit comments

Comments
 (0)