feat(lib): ts_query_cursor_set_max_start_depth - use 0 to reset

This commit is contained in:
Andrew Hlynskyi 2023-04-17 10:54:01 +03:00
parent 1e81a1b67f
commit d4d5e29c91
5 changed files with 26 additions and 10 deletions

BIN
Cargo.lock generated

Binary file not shown.

View file

@ -78,6 +78,7 @@ tempfile = "3"
pretty_assertions = "0.7.2"
ctor = "0.1"
unindent = "0.2"
indoc = "2.0.1"
[build-dependencies]
toml = "0.5"

View file

@ -4,6 +4,7 @@ use super::helpers::{
query_helpers::{Match, Pattern},
ITERATION_COUNT,
};
use indoc::indoc;
use lazy_static::lazy_static;
use rand::{prelude::StdRng, SeedableRng};
use std::{env, fmt::Write};
@ -4478,7 +4479,7 @@ fn test_query_max_start_depth() {
matches: &'static [(usize, &'static [(&'static str, &'static str)])],
}
let source = r#"
let source = indoc! {"
if (a1 && a2) {
if (b1 && b2) { }
if (c) { }
@ -4487,16 +4488,24 @@ fn test_query_max_start_depth() {
if (e1 && e2) { }
if (f) { }
}
"#;
"};
#[rustfmt::skip]
let rows = &[
Row {
description: "depth 0: match none",
description: "depth 0: match all",
depth: 0,
pattern: r#"
(if_statement) @capture
"#,
matches: &[]
matches: &[
(0, &[("capture", "if (a1 && a2) {\n if (b1 && b2) { }\n if (c) { }\n}")]),
(0, &[("capture", "if (b1 && b2) { }")]),
(0, &[("capture", "if (c) { }")]),
(0, &[("capture", "if (d) {\n if (e1 && e2) { }\n if (f) { }\n}")]),
(0, &[("capture", "if (e1 && e2) { }")]),
(0, &[("capture", "if (f) { }")]),
]
},
Row {
description: "depth 1: match 2 if statements at the top level",
@ -4505,8 +4514,8 @@ fn test_query_max_start_depth() {
(if_statement) @capture
"#,
matches : &[
(0, &[("capture", "if (a1 && a2) {\n if (b1 && b2) { }\n if (c) { }\n }")]),
(0, &[("capture", "if (d) {\n if (e1 && e2) { }\n if (f) { }\n }")])
(0, &[("capture", "if (a1 && a2) {\n if (b1 && b2) { }\n if (c) { }\n}")]),
(0, &[("capture", "if (d) {\n if (e1 && e2) { }\n if (f) { }\n}")]),
]
},
Row {
@ -4520,7 +4529,7 @@ fn test_query_max_start_depth() {
) @capture
"#,
matches: &[
(0, &[("capture", "if (a1 && a2) {\n if (b1 && b2) { }\n if (c) { }\n }")]),
(0, &[("capture", "if (a1 && a2) {\n if (b1 && b2) { }\n if (c) { }\n}")]),
]
},
Row {
@ -4534,9 +4543,9 @@ fn test_query_max_start_depth() {
) @capture
"#,
matches: &[
(0, &[("capture", "if (a1 && a2) {\n if (b1 && b2) { }\n if (c) { }\n }")]),
(0, &[("capture", "if (a1 && a2) {\n if (b1 && b2) { }\n if (c) { }\n}")]),
(0, &[("capture", "if (b1 && b2) { }")]),
(0, &[("capture", "if (e1 && e2) { }")])
(0, &[("capture", "if (e1 && e2) { }")]),
]
},
];

View file

@ -897,6 +897,8 @@ bool ts_query_cursor_next_capture(
*
* This prevents cursors from exploring children nodes at a certain depth.
* Note if a pattern includes many children, then they will still be checked.
*
* Set to `0` to remove the maximum start depth.
*/
void ts_query_cursor_set_max_start_depth(TSQueryCursor *, uint32_t);

View file

@ -4092,7 +4092,11 @@ void ts_query_cursor_set_max_start_depth(
TSQueryCursor *self,
uint32_t max_start_depth
) {
self->max_start_depth = max_start_depth;
if (max_start_depth == 0) {
self->max_start_depth = UINT32_MAX;
} else {
self->max_start_depth = max_start_depth;
}
}
#undef LOG