Match on names only when outline query has no spaces

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-01-14 09:16:29 -08:00
parent ce51196eab
commit ea69dcd42a
4 changed files with 73 additions and 34 deletions

View file

@ -1864,11 +1864,15 @@ impl BufferSnapshot {
let item_node = mat.nodes_for_capture_index(item_capture_ix).next()?;
let range = item_node.start_byte()..item_node.end_byte();
let mut text = String::new();
let mut name_ranges = Vec::new();
let mut highlight_ranges = Vec::new();
for capture in mat.captures {
let node_is_name;
if capture.index == name_capture_ix {
node_is_name = true;
} else if capture.index == context_capture_ix {
node_is_name = false;
} else {
continue;
}
@ -1877,6 +1881,18 @@ impl BufferSnapshot {
if !text.is_empty() {
text.push(' ');
}
if node_is_name {
let mut start = text.len();
let end = start + range.len();
// When multiple names are captured, then the matcheable text
// includes the whitespace in between the names.
if !name_ranges.is_empty() {
start -= 1;
}
name_ranges.push(start..end);
}
let mut offset = range.start;
chunks.seek(offset);
@ -1911,6 +1927,7 @@ impl BufferSnapshot {
range: self.anchor_after(range.start)..self.anchor_before(range.end),
text,
highlight_ranges,
name_ranges,
})
})
.collect::<Vec<_>>();