cli: fix query test capture range matching across lines

Signed-off-by: cuishuang <imcusg@gmail.com>
This commit is contained in:
cuishuang 2026-09-03 23:54:51 +08:00
parent 351bd71e52
commit b037276333
3 changed files with 31 additions and 3 deletions

View file

@ -229,9 +229,11 @@ pub fn assert_expected_captures(
let pairs = parse_position_comments(parser, language, contents.as_bytes())?;
for assertion in &pairs {
if let Some(found) = &infos.iter().find(|p| {
assertion.position >= p.start
&& (assertion.position.row < p.end.row
|| assertion.position.column + assertion.length - 1 < p.end.column)
let assertion_end = Utf8Point::new(
assertion.position.row,
assertion.position.column + assertion.length - 1,
);
assertion.position >= p.start && assertion_end < p.end
}) {
if assertion.expected_capture_name != found.name && found.name != "name" {
return Err(anyhow!(

View file

@ -8,6 +8,7 @@ mod node_test;
mod parser_test;
mod pathological_test;
mod query_test;
mod query_testing_test;
mod tags_test;
mod test_highlight_test;
mod test_tags_test;

View file

@ -0,0 +1,25 @@
use std::fs;
use tempfile::NamedTempFile;
use tree_sitter::Parser;
use super::helpers::fixtures::get_test_fixture_language;
use crate::query_testing::{CaptureInfo, Utf8Point, assert_expected_captures};
#[test]
fn test_query_assertion_after_multiline_capture_does_not_match() {
let language = get_test_fixture_language("readme_grammar");
let source_file = NamedTempFile::with_suffix(".txt").unwrap();
fs::write(source_file.path(), "a\nb\nc\n# ^ foo\n").unwrap();
let captures = [CaptureInfo {
name: "foo".to_string(),
start: Utf8Point::new(0, 0),
end: Utf8Point::new(1, 10),
}];
let result =
assert_expected_captures(&captures, source_file.path(), &mut Parser::new(), &language);
assert!(result.is_err());
}