From 58617cfa0c262e3388dd760255e01224c72bda5b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 15 Jan 2020 17:08:31 -0800 Subject: [PATCH] Improve output format for `query` subcommand --- cli/src/query.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/cli/src/query.rs b/cli/src/query.rs index c3a03679..47242273 100644 --- a/cli/src/query.rs +++ b/cli/src/query.rs @@ -51,13 +51,25 @@ pub fn query_files_at_paths( for m in query_cursor.matches(&query, tree.root_node(), text_callback) { writeln!(&mut stdout, " pattern: {}", m.pattern_index)?; for capture in m.captures { - writeln!( - &mut stdout, - " capture: {}, row: {}, text: {:?}", - &query.capture_names()[capture.index as usize], - capture.node.start_position().row, - capture.node.utf8_text(&source_code).unwrap_or("") - )?; + let start = capture.node.start_position(); + let end = capture.node.end_position(); + if end.row == start.row { + writeln!( + &mut stdout, + " capture: {}, start: {}, text: {:?}", + &query.capture_names()[capture.index as usize], + start, + capture.node.utf8_text(&source_code).unwrap_or("") + )?; + } else { + writeln!( + &mut stdout, + " capture: {}, start: {}, end: {}", + &query.capture_names()[capture.index as usize], + start, + end, + )?; + } } } }