diff --git a/Cargo.lock b/Cargo.lock index 69317824..a85e9dad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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)", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index bcc7b42d..c3d183e1 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -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] diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index efdaf780..323a13fc 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -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(|| { diff --git a/lib/Cargo.toml b/lib/Cargo.toml index c5dbf803..2d132788 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -15,7 +15,9 @@ include = [ "/binding_rust/*", "/Cargo.toml", "/include/*", - "/src/*", + "/src/*.h", + "/src/*.c", + "/src/unicode/*", ] [dependencies] diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 97d10d13..372d937f 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -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 {} diff --git a/lib/src/query.c b/lib/src/query.c index b6e48951..ce0e4cdf 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -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; } diff --git a/tags/Cargo.toml b/tags/Cargo.toml index 531b54b4..db73bb72 100644 --- a/tags/Cargo.toml +++ b/tags/Cargo.toml @@ -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 ", "Patrick Thomson " @@ -21,5 +21,5 @@ regex = "1" memchr = "2.3" [dependencies.tree-sitter] -version = ">= 0.3.7" +version = ">= 0.17.0" path = "../lib"