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((
|
||||
|
|
|
|||
|
|
@ -7,13 +7,19 @@ pub mod allocations;
|
|||
#[cfg(unix)]
|
||||
use std::os::unix::io::AsRawFd;
|
||||
|
||||
use std::ffi::CStr;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::os::raw::{c_char, c_void};
|
||||
use std::ptr::NonNull;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::{char, error, fmt, hash, iter, ptr, slice, str, u16};
|
||||
use std::{
|
||||
char, error,
|
||||
ffi::CStr,
|
||||
fmt, hash, iter,
|
||||
marker::PhantomData,
|
||||
mem::MaybeUninit,
|
||||
ops,
|
||||
os::raw::{c_char, c_void},
|
||||
ptr::{self, NonNull},
|
||||
slice, str,
|
||||
sync::atomic::AtomicUsize,
|
||||
u16,
|
||||
};
|
||||
|
||||
/// The latest ABI version that is supported by the current version of the
|
||||
/// library.
|
||||
|
|
@ -1695,17 +1701,25 @@ impl QueryCursor {
|
|||
}
|
||||
|
||||
/// Set the range in which the query will be executed, in terms of byte offsets.
|
||||
pub fn set_byte_range(&mut self, start: usize, end: usize) -> &mut Self {
|
||||
pub fn set_byte_range(&mut self, range: ops::Range<usize>) -> &mut Self {
|
||||
unsafe {
|
||||
ffi::ts_query_cursor_set_byte_range(self.ptr.as_ptr(), start as u32, end as u32);
|
||||
ffi::ts_query_cursor_set_byte_range(
|
||||
self.ptr.as_ptr(),
|
||||
range.start as u32,
|
||||
range.end as u32,
|
||||
);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the range in which the query will be executed, in terms of rows and columns.
|
||||
pub fn set_point_range(&mut self, start: Point, end: Point) -> &mut Self {
|
||||
pub fn set_point_range(&mut self, range: ops::Range<Point>) -> &mut Self {
|
||||
unsafe {
|
||||
ffi::ts_query_cursor_set_point_range(self.ptr.as_ptr(), start.into(), end.into());
|
||||
ffi::ts_query_cursor_set_point_range(
|
||||
self.ptr.as_ptr(),
|
||||
range.start.into(),
|
||||
range.end.into(),
|
||||
);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
|
@ -1864,29 +1878,29 @@ impl<'a, 'tree, T: TextProvider<'a>> Iterator for QueryCaptures<'a, 'tree, T> {
|
|||
}
|
||||
|
||||
impl<'a, 'tree, T: TextProvider<'a>> QueryMatches<'a, 'tree, T> {
|
||||
pub fn set_byte_range(&mut self, start: usize, end: usize) {
|
||||
pub fn set_byte_range(&mut self, range: ops::Range<usize>) {
|
||||
unsafe {
|
||||
ffi::ts_query_cursor_set_byte_range(self.ptr, start as u32, end as u32);
|
||||
ffi::ts_query_cursor_set_byte_range(self.ptr, range.start as u32, range.end as u32);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_point_range(&mut self, start: Point, end: Point) {
|
||||
pub fn set_point_range(&mut self, range: ops::Range<Point>) {
|
||||
unsafe {
|
||||
ffi::ts_query_cursor_set_point_range(self.ptr, start.into(), end.into());
|
||||
ffi::ts_query_cursor_set_point_range(self.ptr, range.start.into(), range.end.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tree, T: TextProvider<'a>> QueryCaptures<'a, 'tree, T> {
|
||||
pub fn set_byte_range(&mut self, start: usize, end: usize) {
|
||||
pub fn set_byte_range(&mut self, range: ops::Range<usize>) {
|
||||
unsafe {
|
||||
ffi::ts_query_cursor_set_byte_range(self.ptr, start as u32, end as u32);
|
||||
ffi::ts_query_cursor_set_byte_range(self.ptr, range.start as u32, range.end as u32);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_point_range(&mut self, start: Point, end: Point) {
|
||||
pub fn set_point_range(&mut self, range: ops::Range<Point>) {
|
||||
unsafe {
|
||||
ffi::ts_query_cursor_set_point_range(self.ptr, start.into(), end.into());
|
||||
ffi::ts_query_cursor_set_point_range(self.ptr, range.start.into(), range.end.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue