Added smallvec feature for inlining single completions

This commit is contained in:
Lutetium-Vanadium 2021-06-27 17:26:04 +05:30
parent 80e6d993b3
commit 6dbc00bbbd
5 changed files with 22 additions and 11 deletions

View File

@ -17,6 +17,8 @@ atty = "0.2"
ui = { package = "discourse-ui", path = "./discourse-ui" }
macros = { package = "discourse-macros", path = "./discourse-macros" }
smallvec = { version = "1.6", optional = true }
[dev-dependencies]
trybuild = { version = "1.0.42", features = ["diff"] }
# remove color printing since it messes with the snapshot's colours
@ -29,6 +31,6 @@ regex = "1.5" # examples/prompt_module.rs
fuzzy-matcher = "0.3" # examples/file.rs
[features]
default = ["crossterm"]
default = ["crossterm", "smallvec"]
crossterm = ["ui/crossterm"]
termion = ["ui/termion"]

View File

@ -2,7 +2,9 @@ use std::path::Path;
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
fn auto_complete(p: String) -> Vec<String> {
use discourse::question::Completions;
fn auto_complete(p: String) -> Completions<String> {
let current: &Path = p.as_ref();
let (mut dir, last) = if p.ends_with('/') {
(current, "")
@ -19,7 +21,7 @@ fn auto_complete(p: String) -> Vec<String> {
dir = ".".as_ref();
}
let mut files: Vec<_> = match dir.read_dir() {
let mut files: Completions<_> = match dir.read_dir() {
Ok(files) => files
.flatten()
.flat_map(|file| {
@ -32,11 +34,13 @@ fn auto_complete(p: String) -> Vec<String> {
}
})
.collect(),
Err(_) => return vec![p],
Err(_) => {
return Completions::from([p]);
}
};
if files.is_empty() {
return vec![p];
Completions::from([p])
} else {
let fuzzer = SkimMatcherV2::default();
files.sort_by_cached_key(|file| fuzzer.fuzzy_match(file, last).unwrap_or(i64::MAX));

View File

@ -6,7 +6,7 @@ use ui::{
widgets, Prompt, Validation, Widget,
};
use super::{AutoComplete, ChoiceList, Filter, Options, Transform, Validate};
use super::{AutoComplete, ChoiceList, Completions, Filter, Options, Transform, Validate};
use crate::{Answer, Answers};
#[derive(Debug, Default)]
@ -269,7 +269,7 @@ mod tests {
(
Input {
auto_complete: AutoComplete::Sync(Box::new(|s, _| {
let mut completions: Vec<_> = ('a'..='d')
let mut completions: Completions<_> = ('a'..='d')
.map(|c| {
let mut s = s.clone();
s.push(c);

View File

@ -210,8 +210,13 @@ macro_rules! handler {
};
}
#[cfg(feature = "smallvec")]
pub type Completions<T> = smallvec::SmallVec<[T; 1]>;
#[cfg(not(feature = "smallvec"))]
pub type Completions<T> = Vec<T>;
handler!(Filter, FnOnce(T, &Answers) -> T);
handler!(AutoComplete, FnMut(T, &Answers) -> Vec<T>);
handler!(AutoComplete, FnMut(T, &Answers) -> Completions<T>);
handler!(Validate, ?Sized FnMut(&T, &Answers) -> Result<(), String>);
handler!(ValidateByVal, FnMut(T, &Answers) -> Result<(), String>);
handler!(Transform, ?Sized FnOnce(&T, &Answers, &mut dyn Backend) -> error::Result<()>);
@ -240,7 +245,7 @@ macro_rules! impl_auto_complete_builder {
($t:ty; $inner:ident) => {
pub fn auto_complete<F>(mut self, auto_complete: F) -> Self
where
F: FnMut($t, &crate::Answers) -> Vec<$t> + 'a,
F: FnMut($t, &crate::Answers) -> Completions<$t> + 'a,
{
self.$inner.auto_complete =
crate::question::AutoComplete::Sync(Box::new(auto_complete));

View File

@ -1,4 +1,4 @@
use discourse::{Answer, Question};
use discourse::{question::Completions, Answer, Question};
use ui::{
events::{KeyCode, TestEvents},
style::Color,
@ -107,7 +107,7 @@ fn test_auto_complete() {
let prompt = Question::input("name")
.message("message")
.auto_complete(|s, _| {
let mut completions: Vec<_> = ('g'..='j')
let mut completions: Completions<_> = ('g'..='j')
.map(|c| {
let mut s = s.clone();
s.push(c);