mirror of
https://github.com/nushell/reedline.git
synced 2024-09-11 15:55:50 +03:00
Fix menu::menu_functions::find_common_string
(#363)
* Fix `menu::menu_functions::find_common_string` * Add test for `DefaultCompleter` * Add test strings
This commit is contained in:
parent
e982abf7e2
commit
7c594b16ba
@ -348,3 +348,40 @@ impl CompletionNode {
|
||||
completions
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn default_completer_with_non_ansi() {
|
||||
use super::*;
|
||||
|
||||
let mut completions = DefaultCompleter::default();
|
||||
completions.insert(
|
||||
vec!["nushell", "null", "number"]
|
||||
.iter()
|
||||
.map(|s| s.to_string())
|
||||
.collect(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
completions.complete("n", 3),
|
||||
vec![
|
||||
Suggestion {
|
||||
value: "null".into(),
|
||||
description: None,
|
||||
span: Span { start: 0, end: 3 },
|
||||
},
|
||||
Suggestion {
|
||||
value: "number".into(),
|
||||
description: None,
|
||||
span: Span { start: 0, end: 3 },
|
||||
},
|
||||
Suggestion {
|
||||
value: "nushell".into(),
|
||||
description: None,
|
||||
span: Span { start: 0, end: 3 },
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -61,6 +61,8 @@ fn main() -> Result<()> {
|
||||
"abaaac".into(),
|
||||
"abaaaxyc".into(),
|
||||
"abaaarabc".into(),
|
||||
"こんにちは世界".into(),
|
||||
"こんばんは世界".into(),
|
||||
];
|
||||
|
||||
let completer = Box::new(DefaultCompleter::new_with_wordlen(commands.clone(), 2));
|
||||
|
@ -138,15 +138,15 @@ pub fn find_common_string(values: &[Suggestion]) -> (Option<&Suggestion>, Option
|
||||
} else {
|
||||
first
|
||||
.value
|
||||
.chars()
|
||||
.zip(suggestion.value.chars())
|
||||
.position(|(mut lhs, mut rhs)| {
|
||||
.char_indices()
|
||||
.zip(suggestion.value.char_indices())
|
||||
.find(|((_, mut lhs), (_, mut rhs))| {
|
||||
lhs.make_ascii_lowercase();
|
||||
rhs.make_ascii_lowercase();
|
||||
|
||||
lhs != rhs
|
||||
})
|
||||
.map(|new_index| match index {
|
||||
.map(|((new_index, _), _)| match index {
|
||||
Some(index) => {
|
||||
if index <= new_index {
|
||||
index
|
||||
@ -403,4 +403,38 @@ mod tests {
|
||||
let res = string_difference(new_string, old_string);
|
||||
assert_eq!(res, (4, "b = ñ "));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_common_string_with_ansi() {
|
||||
use crate::Span;
|
||||
|
||||
let input: Vec<_> = ["nushell", "null"]
|
||||
.into_iter()
|
||||
.map(|s| Suggestion {
|
||||
value: s.into(),
|
||||
description: None,
|
||||
span: Span::new(0, s.len()),
|
||||
})
|
||||
.collect();
|
||||
let res = find_common_string(&input);
|
||||
|
||||
assert!(matches!(res, (Some(elem), Some(2)) if elem == &input[0]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_common_string_with_non_ansi() {
|
||||
use crate::Span;
|
||||
|
||||
let input: Vec<_> = ["nushell", "null"]
|
||||
.into_iter()
|
||||
.map(|s| Suggestion {
|
||||
value: s.into(),
|
||||
description: None,
|
||||
span: Span::new(0, s.len()),
|
||||
})
|
||||
.collect();
|
||||
let res = find_common_string(&input);
|
||||
|
||||
assert!(matches!(res, (Some(elem), Some(6)) if elem == &input[0]));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user