Make ::set_{byte,point}_range methods take a Range
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
7f4eb9a222
commit
d72771a19f
4 changed files with 53 additions and 36 deletions
|
|
@ -309,7 +309,7 @@ fn run() -> error::Result<()> {
|
|||
let query_path = Path::new(matches.value_of("query-path").unwrap());
|
||||
let range = matches.value_of("byte-range").map(|br| {
|
||||
let r: Vec<&str> = br.split(":").collect();
|
||||
(r[0].parse().unwrap(), r[1].parse().unwrap())
|
||||
r[0].parse().unwrap()..r[1].parse().unwrap()
|
||||
});
|
||||
let should_test = matches.is_present("test");
|
||||
query::query_files_at_paths(
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
use super::error::{Error, Result};
|
||||
use crate::query_testing;
|
||||
use std::fs;
|
||||
use std::io::{self, Write};
|
||||
use std::path::Path;
|
||||
use std::{
|
||||
fs,
|
||||
io::{self, Write},
|
||||
ops::Range,
|
||||
path::Path,
|
||||
};
|
||||
use tree_sitter::{Language, Parser, Query, QueryCursor};
|
||||
|
||||
pub fn query_files_at_paths(
|
||||
|
|
@ -10,7 +13,7 @@ pub fn query_files_at_paths(
|
|||
paths: Vec<String>,
|
||||
query_path: &Path,
|
||||
ordered_captures: bool,
|
||||
range: Option<(usize, usize)>,
|
||||
range: Option<Range<usize>>,
|
||||
should_test: bool,
|
||||
) -> Result<()> {
|
||||
let stdout = io::stdout();
|
||||
|
|
@ -23,8 +26,8 @@ pub fn query_files_at_paths(
|
|||
.map_err(|e| Error::new(format!("Query compilation failed: {:?}", e)))?;
|
||||
|
||||
let mut query_cursor = QueryCursor::new();
|
||||
if let Some((beg, end)) = range {
|
||||
query_cursor.set_byte_range(beg, end);
|
||||
if let Some(range) = range {
|
||||
query_cursor.set_byte_range(range);
|
||||
}
|
||||
|
||||
let mut parser = Parser::new();
|
||||
|
|
|
|||
|
|
@ -1782,7 +1782,7 @@ fn test_query_matches_within_byte_range() {
|
|||
|
||||
let matches =
|
||||
cursor
|
||||
.set_byte_range(0, 8)
|
||||
.set_byte_range(0..8)
|
||||
.matches(&query, tree.root_node(), source.as_bytes());
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1796,7 +1796,7 @@ fn test_query_matches_within_byte_range() {
|
|||
|
||||
let matches =
|
||||
cursor
|
||||
.set_byte_range(5, 15)
|
||||
.set_byte_range(5..15)
|
||||
.matches(&query, tree.root_node(), source.as_bytes());
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1810,7 +1810,7 @@ fn test_query_matches_within_byte_range() {
|
|||
|
||||
let matches =
|
||||
cursor
|
||||
.set_byte_range(12, 0)
|
||||
.set_byte_range(12..0)
|
||||
.matches(&query, tree.root_node(), source.as_bytes());
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1839,7 +1839,7 @@ fn test_query_matches_within_point_range() {
|
|||
let mut cursor = QueryCursor::new();
|
||||
|
||||
let matches = cursor
|
||||
.set_point_range(Point::new(0, 0), Point::new(1, 3))
|
||||
.set_point_range(Point::new(0, 0)..Point::new(1, 3))
|
||||
.matches(&query, tree.root_node(), source.as_bytes());
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1852,7 +1852,7 @@ fn test_query_matches_within_point_range() {
|
|||
);
|
||||
|
||||
let matches = cursor
|
||||
.set_point_range(Point::new(1, 0), Point::new(2, 3))
|
||||
.set_point_range(Point::new(1, 0)..Point::new(2, 3))
|
||||
.matches(&query, tree.root_node(), source.as_bytes());
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1865,7 +1865,7 @@ fn test_query_matches_within_point_range() {
|
|||
);
|
||||
|
||||
let matches = cursor
|
||||
.set_point_range(Point::new(2, 1), Point::new(0, 0))
|
||||
.set_point_range(Point::new(2, 1)..Point::new(0, 0))
|
||||
.matches(&query, tree.root_node(), source.as_bytes());
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1904,7 +1904,7 @@ fn test_query_captures_within_byte_range() {
|
|||
let mut cursor = QueryCursor::new();
|
||||
let captures =
|
||||
cursor
|
||||
.set_byte_range(3, 27)
|
||||
.set_byte_range(3..27)
|
||||
.captures(&query, tree.root_node(), source.as_bytes());
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1940,7 +1940,7 @@ fn test_query_matches_with_unrooted_patterns_intersecting_byte_range() {
|
|||
|
||||
// within the type parameter list
|
||||
let offset = source.find("D: E>").unwrap();
|
||||
let matches = cursor.set_byte_range(offset, offset).matches(
|
||||
let matches = cursor.set_byte_range(offset..offset).matches(
|
||||
&query,
|
||||
tree.root_node(),
|
||||
source.as_bytes(),
|
||||
|
|
@ -1956,7 +1956,7 @@ fn test_query_matches_with_unrooted_patterns_intersecting_byte_range() {
|
|||
// from within the type parameter list to within the function body
|
||||
let start_offset = source.find("D: E>").unwrap();
|
||||
let end_offset = source.find("g(f)").unwrap();
|
||||
let matches = cursor.set_byte_range(start_offset, end_offset).matches(
|
||||
let matches = cursor.set_byte_range(start_offset..end_offset).matches(
|
||||
&query,
|
||||
tree.root_node(),
|
||||
source.as_bytes(),
|
||||
|
|
@ -2039,7 +2039,7 @@ fn test_query_captures_within_byte_range_assigned_after_iterating() {
|
|||
// Captures from these matches are reported, but only those that
|
||||
// intersect the range.
|
||||
results.clear();
|
||||
captures.set_byte_range(source.find("Ok").unwrap(), source.len());
|
||||
captures.set_byte_range(source.find("Ok").unwrap()..source.len());
|
||||
for (mat, capture_ix) in captures {
|
||||
let capture = mat.captures[capture_ix as usize];
|
||||
results.push((
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue