nu-cli/completions: support record for custom completions (#5298)

This commit is contained in:
Herlon Aguiar 2022-04-22 22:17:08 +02:00 committed by GitHub
parent ee29a15119
commit 661283c4d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,10 +34,9 @@ impl CustomCompletion {
offset: usize, offset: usize,
) -> Vec<Suggestion> { ) -> Vec<Suggestion> {
list.filter_map(move |x| { list.filter_map(move |x| {
let s = x.as_string(); // Match for string values
if let Ok(s) = x.as_string() {
match s { return Some(Suggestion {
Ok(s) => Some(Suggestion {
value: s, value: s,
description: None, description: None,
extra: None, extra: None,
@ -45,9 +44,46 @@ impl CustomCompletion {
start: span.start - offset, start: span.start - offset,
end: span.end - offset, end: span.end - offset,
}, },
}), });
Err(_) => None,
} }
// Match for record values
if let Ok((cols, vals)) = x.as_record() {
let mut suggestion = Suggestion {
value: String::from(""), // Initialize with empty string
description: None,
extra: None,
span: reedline::Span {
start: span.start - offset,
end: span.end - offset,
},
};
// Iterate the cols looking for `value` and `description`
cols.iter().zip(vals).for_each(|it| {
// Match `value` column
if it.0 == "value" {
// Convert the value to string
if let Ok(val_str) = it.1.as_string() {
// Update the suggestion value
suggestion.value = val_str;
}
}
// Match `description` column
if it.0 == "description" {
// Convert the value to string
if let Ok(desc_str) = it.1.as_string() {
// Update the suggestion value
suggestion.description = Some(desc_str);
}
}
});
return Some(suggestion);
}
None
}) })
.collect() .collect()
} }