Merge remote-tracking branch 'origin/master' into ensure-extras-is-array

This commit is contained in:
Patrick Thomson 2020-10-05 16:12:37 -04:00
commit adce3cb8e2
7 changed files with 64 additions and 9 deletions

4
Cargo.lock generated
View file

@ -764,7 +764,7 @@ dependencies = [
"tiny_http 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tree-sitter 0.17.0",
"tree-sitter-highlight 0.3.0",
"tree-sitter-tags 0.2.0",
"tree-sitter-tags 0.3.0",
"webbrowser 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -778,7 +778,7 @@ dependencies = [
[[package]]
name = "tree-sitter-tags"
version = "0.2.0"
version = "0.3.0"
dependencies = [
"memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -37,11 +37,11 @@ tiny_http = "0.6"
webbrowser = "0.5.1"
[dependencies.tree-sitter]
version = ">= 0.3.7"
version = ">= 0.17.0"
path = "../lib"
[dependencies.tree-sitter-highlight]
version = ">= 0.1.0"
version = ">= 0.3.0"
path = "../highlight"
[dependencies.tree-sitter-tags]

View file

@ -1691,6 +1691,36 @@ fn test_query_matches_with_multiple_captures_on_a_node() {
});
}
#[test]
fn test_query_matches_with_no_captures() {
allocations::record(|| {
let language = get_language("javascript");
let query = Query::new(
language,
r#"
(identifier)
(string) @s
"#,
)
.unwrap();
assert_query_matches(
language,
&query,
"
a = 'hi';
b = 'bye';
",
&[
(0, vec![]),
(1, vec![("s", "'hi'")]),
(0, vec![]),
(1, vec![("s", "'bye'")]),
],
);
});
}
#[test]
fn test_query_captures_basic() {
allocations::record(|| {

View file

@ -15,7 +15,9 @@ include = [
"/binding_rust/*",
"/Cargo.toml",
"/include/*",
"/src/*",
"/src/*.h",
"/src/*.c",
"/src/unicode/*",
]
[dependencies]

View file

@ -138,7 +138,7 @@ pub struct QueryCaptures<'a, T: AsRef<[u8]>> {
}
/// A particular `Node` that has been captured with a particular name within a `Query`.
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct QueryCapture<'a> {
pub node: Node<'a>,
@ -1272,7 +1272,11 @@ impl Query {
let mut length = 0u32;
let raw_predicates =
ffi::ts_query_predicates_for_pattern(ptr, i as u32, &mut length as *mut u32);
if length > 0 {
slice::from_raw_parts(raw_predicates, length as usize)
} else {
&[]
}
};
let byte_offset = unsafe { ffi::ts_query_start_byte_for_pattern(ptr, i as u32) };
@ -1649,11 +1653,15 @@ impl<'a> QueryMatch<'a> {
cursor,
id: m.id,
pattern_index: m.pattern_index as usize,
captures: unsafe {
captures: if m.capture_count > 0 {
unsafe {
slice::from_raw_parts(
m.captures as *const QueryCapture<'a>,
m.capture_count as usize,
)
}
} else {
&[]
},
}
}
@ -1729,6 +1737,16 @@ impl<'a, T: AsRef<[u8]>> Iterator for QueryCaptures<'a, T> {
}
}
impl<'a> fmt::Debug for QueryMatch<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"QueryMatch {{ id: {}, pattern_index: {}, captures: {:?} }}",
self.id, self.pattern_index, self.captures
)
}
}
impl PartialEq for Query {
fn eq(&self, other: &Self) -> bool {
self.ptr == other.ptr
@ -1826,5 +1844,6 @@ unsafe impl Send for Language {}
unsafe impl Send for Parser {}
unsafe impl Send for Query {}
unsafe impl Send for Tree {}
unsafe impl Send for QueryCursor {}
unsafe impl Sync for Language {}
unsafe impl Sync for Query {}

View file

@ -1710,6 +1710,8 @@ static TSQueryError ts_query__parse_pattern(
stream_reset(stream, node_name);
return TSQueryErrorNodeType;
}
stream_skip_whitespace(stream);
}
// Parse the child patterns
@ -2518,6 +2520,7 @@ static inline bool ts_query_cursor__advance(
} else if (ts_tree_cursor_goto_parent(&self->cursor)) {
self->depth--;
} else {
LOG("halt at root");
self->halted = true;
}
@ -2582,6 +2585,7 @@ static inline bool ts_query_cursor__advance(
self->end_byte <= ts_node_start_byte(node) ||
point_lte(self->end_point, ts_node_start_point(node))
) {
LOG("halt at end of range");
self->halted = true;
continue;
}

View file

@ -1,7 +1,7 @@
[package]
name = "tree-sitter-tags"
description = "Library for extracting tag information"
version = "0.2.0"
version = "0.3.0"
authors = [
"Max Brunsfeld <maxbrunsfeld@gmail.com>",
"Patrick Thomson <patrickt@github.com>"
@ -21,5 +21,5 @@ regex = "1"
memchr = "2.3"
[dependencies.tree-sitter]
version = ">= 0.3.7"
version = ">= 0.17.0"
path = "../lib"