@@ -11,6 +11,11 @@ use super::Query;
1111use super :: source_map:: SourceKind ;
1212use super :: type_check:: Arity ;
1313
14+ /// Returns indentation string for the given level.
15+ fn indent ( level : usize ) -> String {
16+ " " . repeat ( level)
17+ }
18+
1419pub struct QueryPrinter < ' q > {
1520 query : & ' q Query ,
1621 raw : bool ,
@@ -135,13 +140,13 @@ impl<'q> QueryPrinter<'q> {
135140 fn format_symbol_tree (
136141 & self ,
137142 name : & str ,
138- indent : usize ,
143+ depth : usize ,
139144 defined : & indexmap:: IndexSet < & str > ,
140145 body_nodes : & std:: collections:: HashMap < String , SyntaxNode > ,
141146 visited : & mut indexmap:: IndexSet < String > ,
142147 w : & mut impl Write ,
143148 ) -> std:: fmt:: Result {
144- let prefix = " " . repeat ( indent) ;
149+ let prefix = indent ( depth ) ;
145150
146151 if visited. contains ( name) {
147152 writeln ! ( w, "{}{} (cycle)" , prefix, name) ?;
@@ -166,29 +171,29 @@ impl<'q> QueryPrinter<'q> {
166171 let mut refs: Vec < _ > = refs_set. iter ( ) . map ( |s| s. as_str ( ) ) . collect ( ) ;
167172 refs. sort ( ) ;
168173 for r in refs {
169- self . format_symbol_tree ( r, indent + 1 , defined, body_nodes, visited, w) ?;
174+ self . format_symbol_tree ( r, depth + 1 , defined, body_nodes, visited, w) ?;
170175 }
171176 }
172177
173178 visited. shift_remove ( name) ;
174179 Ok ( ( ) )
175180 }
176181
177- fn format_cst ( & self , node : & SyntaxNode , indent : usize , w : & mut impl Write ) -> std:: fmt:: Result {
178- let prefix = " " . repeat ( indent) ;
182+ fn format_cst ( & self , node : & SyntaxNode , depth : usize , w : & mut impl Write ) -> std:: fmt:: Result {
183+ let prefix = indent ( depth ) ;
179184 let card = self . arity_mark ( node) ;
180185 let span = self . span_str ( node. text_range ( ) ) ;
181186
182187 writeln ! ( w, "{}{:?}{}{}" , prefix, node. kind( ) , card, span) ?;
183188
184189 for child in node. children_with_tokens ( ) {
185190 match child {
186- NodeOrToken :: Node ( n) => self . format_cst ( & n, indent + 1 , w) ?,
191+ NodeOrToken :: Node ( n) => self . format_cst ( & n, depth + 1 , w) ?,
187192 NodeOrToken :: Token ( t) => {
188193 if !self . trivia && t. kind ( ) . is_trivia ( ) {
189194 continue ;
190195 }
191- let child_prefix = " " . repeat ( indent + 1 ) ;
196+ let child_prefix = indent ( depth + 1 ) ;
192197 let child_span = self . span_str ( t. text_range ( ) ) ;
193198 writeln ! (
194199 w,
@@ -220,8 +225,8 @@ impl<'q> QueryPrinter<'q> {
220225 Ok ( ( ) )
221226 }
222227
223- fn format_def ( & self , def : & ast:: Def , indent : usize , w : & mut impl Write ) -> std:: fmt:: Result {
224- let prefix = " " . repeat ( indent) ;
228+ fn format_def ( & self , def : & ast:: Def , depth : usize , w : & mut impl Write ) -> std:: fmt:: Result {
229+ let prefix = indent ( depth ) ;
225230 let card = self . arity_mark ( def. as_cst ( ) ) ;
226231 let span = self . span_str ( def. text_range ( ) ) ;
227232 let name = def. name ( ) . map ( |t| t. text ( ) . to_string ( ) ) ;
@@ -234,11 +239,11 @@ impl<'q> QueryPrinter<'q> {
234239 let Some ( body) = def. body ( ) else {
235240 return Ok ( ( ) ) ;
236241 } ;
237- self . format_expr ( & body, indent + 1 , w)
242+ self . format_expr ( & body, depth + 1 , w)
238243 }
239244
240- fn format_expr ( & self , expr : & ast:: Expr , indent : usize , w : & mut impl Write ) -> std:: fmt:: Result {
241- let prefix = " " . repeat ( indent) ;
245+ fn format_expr ( & self , expr : & ast:: Expr , depth : usize , w : & mut impl Write ) -> std:: fmt:: Result {
246+ let prefix = indent ( depth ) ;
242247 let card = self . arity_mark ( expr. as_cst ( ) ) ;
243248 let span = self . span_str ( expr. text_range ( ) ) ;
244249
@@ -253,7 +258,7 @@ impl<'q> QueryPrinter<'q> {
253258 None => writeln ! ( w, "{}NamedNode{}{}" , prefix, card, span) ?,
254259 }
255260 }
256- self . format_tree_children ( n. as_cst ( ) , indent + 1 , w) ?;
261+ self . format_tree_children ( n. as_cst ( ) , depth + 1 , w) ?;
257262 }
258263 ast:: Expr :: Ref ( r) => {
259264 let name = r. name ( ) . map ( |t| t. text ( ) . to_string ( ) ) . unwrap_or_default ( ) ;
@@ -270,15 +275,15 @@ impl<'q> QueryPrinter<'q> {
270275 ast:: Expr :: AltExpr ( a) => {
271276 writeln ! ( w, "{}Alt{}{}" , prefix, card, span) ?;
272277 for branch in a. branches ( ) {
273- self . format_branch ( & branch, indent + 1 , w) ?;
278+ self . format_branch ( & branch, depth + 1 , w) ?;
274279 }
275280 for expr in a. exprs ( ) {
276- self . format_expr ( & expr, indent + 1 , w) ?;
281+ self . format_expr ( & expr, depth + 1 , w) ?;
277282 }
278283 }
279284 ast:: Expr :: SeqExpr ( s) => {
280285 writeln ! ( w, "{}Seq{}{}" , prefix, card, span) ?;
281- self . format_tree_children ( s. as_cst ( ) , indent + 1 , w) ?;
286+ self . format_tree_children ( s. as_cst ( ) , depth + 1 , w) ?;
282287 }
283288 ast:: Expr :: CapturedExpr ( c) => {
284289 let name = c. name ( ) . map ( |t| t. text ( ) . to_string ( ) ) . unwrap_or_default ( ) ;
@@ -297,7 +302,7 @@ impl<'q> QueryPrinter<'q> {
297302 let Some ( inner) = c. inner ( ) else {
298303 return Ok ( ( ) ) ;
299304 } ;
300- self . format_expr ( & inner, indent + 1 , w) ?;
305+ self . format_expr ( & inner, depth + 1 , w) ?;
301306 }
302307 ast:: Expr :: QuantifiedExpr ( q) => {
303308 let op = q
@@ -308,15 +313,15 @@ impl<'q> QueryPrinter<'q> {
308313 let Some ( inner) = q. inner ( ) else {
309314 return Ok ( ( ) ) ;
310315 } ;
311- self . format_expr ( & inner, indent + 1 , w) ?;
316+ self . format_expr ( & inner, depth + 1 , w) ?;
312317 }
313318 ast:: Expr :: FieldExpr ( f) => {
314319 let name = f. name ( ) . map ( |t| t. text ( ) . to_string ( ) ) . unwrap_or_default ( ) ;
315320 writeln ! ( w, "{}FieldExpr{}{} {}:" , prefix, card, span, name) ?;
316321 let Some ( value) = f. value ( ) else {
317322 return Ok ( ( ) ) ;
318323 } ;
319- self . format_expr ( & value, indent + 1 , w) ?;
324+ self . format_expr ( & value, depth + 1 , w) ?;
320325 }
321326 }
322327 Ok ( ( ) )
@@ -325,34 +330,33 @@ impl<'q> QueryPrinter<'q> {
325330 fn format_tree_children (
326331 & self ,
327332 node : & SyntaxNode ,
328- indent : usize ,
333+ depth : usize ,
329334 w : & mut impl Write ,
330335 ) -> std:: fmt:: Result {
331336 use crate :: parser:: cst:: SyntaxKind ;
332337 for child in node. children ( ) {
333338 if child. kind ( ) == SyntaxKind :: Anchor {
334- self . mark_anchor ( indent , w) ?;
339+ self . mark_anchor ( depth , w) ?;
335340 } else if child. kind ( ) == SyntaxKind :: NegatedField {
336- self . format_negated_field ( & ast:: NegatedField :: cast ( child) . unwrap ( ) , indent , w) ?;
341+ self . format_negated_field ( & ast:: NegatedField :: cast ( child) . unwrap ( ) , depth , w) ?;
337342 } else if let Some ( expr) = ast:: Expr :: cast ( child) {
338- self . format_expr ( & expr, indent , w) ?;
343+ self . format_expr ( & expr, depth , w) ?;
339344 }
340345 }
341346 Ok ( ( ) )
342347 }
343348
344- fn mark_anchor ( & self , indent : usize , w : & mut impl Write ) -> std:: fmt:: Result {
345- let prefix = " " . repeat ( indent) ;
346- writeln ! ( w, "{}." , prefix)
349+ fn mark_anchor ( & self , depth : usize , w : & mut impl Write ) -> std:: fmt:: Result {
350+ writeln ! ( w, "{}." , indent( depth) )
347351 }
348352
349353 fn format_negated_field (
350354 & self ,
351355 nf : & ast:: NegatedField ,
352- indent : usize ,
356+ depth : usize ,
353357 w : & mut impl Write ,
354358 ) -> std:: fmt:: Result {
355- let prefix = " " . repeat ( indent) ;
359+ let prefix = indent ( depth ) ;
356360 let span = self . span_str ( nf. text_range ( ) ) ;
357361 let name = nf. name ( ) . map ( |t| t. text ( ) . to_string ( ) ) . unwrap_or_default ( ) ;
358362 writeln ! ( w, "{}NegatedField{} !{}" , prefix, span, name)
@@ -361,10 +365,10 @@ impl<'q> QueryPrinter<'q> {
361365 fn format_branch (
362366 & self ,
363367 branch : & ast:: Branch ,
364- indent : usize ,
368+ depth : usize ,
365369 w : & mut impl Write ,
366370 ) -> std:: fmt:: Result {
367- let prefix = " " . repeat ( indent) ;
371+ let prefix = indent ( depth ) ;
368372 let card = self . arity_mark ( branch. as_cst ( ) ) ;
369373 let span = self . span_str ( branch. text_range ( ) ) ;
370374 let label = branch. label ( ) . map ( |t| t. text ( ) . to_string ( ) ) ;
@@ -377,7 +381,7 @@ impl<'q> QueryPrinter<'q> {
377381 let Some ( body) = branch. body ( ) else {
378382 return Ok ( ( ) ) ;
379383 } ;
380- self . format_expr ( & body, indent + 1 , w)
384+ self . format_expr ( & body, depth + 1 , w)
381385 }
382386
383387 fn arity_mark ( & self , node : & SyntaxNode ) -> & ' static str {
0 commit comments