Skip to content

Commit 0c77f8d

Browse files
committed
Correct match indices for haystacks with quotes
1 parent 255caa2 commit 0c77f8d

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

crates/nu-cli/src/completions/completion_options.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ impl<T> NuMatcher<'_, T> {
122122
/// to the list of matches (if given).
123123
///
124124
/// Helper to avoid code duplication between [NuMatcher::add] and [NuMatcher::matches].
125-
fn matches_aux(&mut self, haystack: &str, item: Option<T>) -> bool {
126-
let haystack = haystack.trim_matches(QUOTES);
125+
fn matches_aux(&mut self, orig_haystack: &str, item: Option<T>) -> bool {
126+
let haystack = orig_haystack.trim_start_matches(QUOTES);
127+
let offset = orig_haystack.len() - haystack.len();
128+
let haystack = haystack.trim_end_matches(QUOTES);
127129
match &mut self.state {
128130
State::Prefix { items } => {
129131
let haystack_folded = if self.options.case_sensitive {
@@ -164,7 +166,9 @@ impl<T> NuMatcher<'_, T> {
164166
let indices = indices
165167
.iter()
166168
.map(|i| {
167-
usize::try_from(*i).expect("should be on at least a 32-bit system")
169+
offset
170+
+ usize::try_from(*i)
171+
.expect("should be on at least a 32-bit system")
168172
})
169173
.collect();
170174
items.push((haystack.to_string(), item, score, indices));
@@ -381,7 +385,7 @@ mod test {
381385
assert_eq!(
382386
vec![(
383387
"'i love spaces' so much",
384-
Some(vec![0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
388+
Some(vec![3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
385389
)],
386390
matcher.results()
387391
);

0 commit comments

Comments
 (0)