Skip to content

Commit 3f741de

Browse files
committed
refactor to pretty print
1 parent a23bc4e commit 3f741de

File tree

2 files changed

+6
-65
lines changed

2 files changed

+6
-65
lines changed

src/parse/syntax.rs

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -117,69 +117,6 @@ pub(crate) enum Syntax<'a> {
117117
},
118118
}
119119

120-
pub struct SyntaxTreeDisplay<'a>(Vec<&'a Syntax<'a>>);
121-
122-
#[allow(dead_code)]
123-
impl<'a> SyntaxTreeDisplay<'a> {
124-
pub fn from(tree: Vec<&'a Syntax<'a>>) -> Self {
125-
Self(tree)
126-
}
127-
128-
fn print_node(
129-
f: &mut fmt::Formatter<'_>,
130-
node: &Syntax,
131-
prefix: &str,
132-
is_last: bool,
133-
) -> fmt::Result {
134-
let connector = if is_last { "└── " } else { "├── " };
135-
136-
match node {
137-
Syntax::List {
138-
open_position,
139-
close_position,
140-
children,
141-
..
142-
} => {
143-
writeln!(
144-
f,
145-
"{}{}List (open: {:?}, close: {:?})",
146-
prefix, connector, open_position, close_position
147-
)?;
148-
149-
// Prepare prefix for children
150-
// If this was the last node, children don't need the vertical bar │
151-
let child_prefix = format!("{}{}", prefix, if is_last { " " } else { "│ " });
152-
153-
for (i, child) in children.iter().enumerate() {
154-
Self::print_node(f, child, &child_prefix, i == children.len() - 1)?;
155-
}
156-
}
157-
Syntax::Atom {
158-
content,
159-
position,
160-
kind,
161-
..
162-
} => {
163-
writeln!(
164-
f,
165-
"{}{}Atom: {:?} {:#?} ({:?})",
166-
prefix, connector, content, kind, position
167-
)?;
168-
}
169-
}
170-
Ok(())
171-
}
172-
}
173-
174-
impl<'a> fmt::Display for SyntaxTreeDisplay<'a> {
175-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
176-
for (i, node) in self.0.iter().enumerate() {
177-
SyntaxTreeDisplay::print_node(f, node, "", i == self.0.len() - 1)?;
178-
}
179-
Ok(())
180-
}
181-
}
182-
183120
fn dbg_pos(pos: &[SingleLineSpan]) -> String {
184121
match pos {
185122
[] => "-".into(),

src/parse/tree_sitter_parser.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,9 +1978,13 @@ mod tests {
19781978

19791979
if let Some(remaining) = current_expected {
19801980
panic!(
1981-
"Could not find all atom sequences. \nMissing: {:?}\nDebug Tree:\n{}",
1981+
"Could not find all atom sequences. \nMissing: {:?}\nDebug Tree:\n{:?}",
19821982
remaining,
1983-
SyntaxTreeDisplay::from(nodes.to_vec())
1983+
nodes
1984+
.iter()
1985+
.map(|node| node.dbg_content())
1986+
.collect::<Vec<_>>()
1987+
.join("\n")
19841988
);
19851989
}
19861990
}

0 commit comments

Comments
 (0)