Improve output format for query subcommand

This commit is contained in:
Max Brunsfeld 2020-01-15 17:08:31 -08:00
parent 3c4a24752b
commit 58617cfa0c

View file

@ -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,
)?;
}
}
}
}