aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_tt/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_tt/src/lib.rs')
-rw-r--r--crates/ra_tt/src/lib.rs57
1 files changed, 56 insertions, 1 deletions
diff --git a/crates/ra_tt/src/lib.rs b/crates/ra_tt/src/lib.rs
index bd484aa30..5248e026c 100644
--- a/crates/ra_tt/src/lib.rs
+++ b/crates/ra_tt/src/lib.rs
@@ -57,7 +57,7 @@ pub enum Leaf {
57} 57}
58impl_froms!(Leaf: Literal, Punct, Ident); 58impl_froms!(Leaf: Literal, Punct, Ident);
59 59
60#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] 60#[derive(Clone, PartialEq, Eq, Hash, Default)]
61pub struct Subtree { 61pub struct Subtree {
62 pub delimiter: Option<Delimiter>, 62 pub delimiter: Option<Delimiter>,
63 pub token_trees: Vec<TokenTree>, 63 pub token_trees: Vec<TokenTree>,
@@ -101,6 +101,61 @@ pub struct Ident {
101 pub id: TokenId, 101 pub id: TokenId,
102} 102}
103 103
104fn print_debug_subtree(f: &mut fmt::Formatter<'_>, subtree: &Subtree, level: usize) -> fmt::Result {
105 let align = std::iter::repeat(" ").take(level).collect::<String>();
106
107 let aux = match subtree.delimiter.map(|it| (it.kind, it.id.0)) {
108 None => "$".to_string(),
109 Some((DelimiterKind::Parenthesis, id)) => format!("() {}", id),
110 Some((DelimiterKind::Brace, id)) => format!("{{}} {}", id),
111 Some((DelimiterKind::Bracket, id)) => format!("[] {}", id),
112 };
113
114 if subtree.token_trees.is_empty() {
115 write!(f, "{}SUBTREE {}", align, aux)?;
116 } else {
117 writeln!(f, "{}SUBTREE {}", align, aux)?;
118 for (idx, child) in subtree.token_trees.iter().enumerate() {
119 print_debug_token(f, child, level + 1)?;
120 if idx != subtree.token_trees.len() - 1 {
121 writeln!(f, "")?;
122 }
123 }
124 }
125
126 Ok(())
127}
128
129fn print_debug_token(f: &mut fmt::Formatter<'_>, tkn: &TokenTree, level: usize) -> fmt::Result {
130 let align = std::iter::repeat(" ").take(level).collect::<String>();
131
132 match tkn {
133 TokenTree::Leaf(leaf) => match leaf {
134 Leaf::Literal(lit) => write!(f, "{}LITERAL {} {}", align, lit.text, lit.id.0)?,
135 Leaf::Punct(punct) => write!(
136 f,
137 "{}PUNCH {} [{}] {}",
138 align,
139 punct.char,
140 if punct.spacing == Spacing::Alone { "alone" } else { "joint" },
141 punct.id.0
142 )?,
143 Leaf::Ident(ident) => write!(f, "{}IDENT {} {}", align, ident.text, ident.id.0)?,
144 },
145 TokenTree::Subtree(subtree) => {
146 print_debug_subtree(f, subtree, level)?;
147 }
148 }
149
150 Ok(())
151}
152
153impl Debug for Subtree {
154 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155 print_debug_subtree(f, self, 0)
156 }
157}
158
104impl fmt::Display for TokenTree { 159impl fmt::Display for TokenTree {
105 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 160 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106 match self { 161 match self {