use tokenizer::Token;
use util::longest_common_prefix;
pub struct CompletionOption {
pub option_string: String,
pub complete: bool,
}
impl CompletionOption {
pub fn new(option_string: String, complete: bool) -> CompletionOption {
CompletionOption {
option_string: option_string,
complete: complete,
}
}
}
pub struct Completion<'text> {
pub help_symbol: String,
pub help_text: String,
pub token: Option<Token<'text>>,
pub exhaustive: bool,
pub options: Vec<CompletionOption>,
}
impl<'text> Completion<'text> {
pub fn new(help_symbol: String,
help_text: String,
token: Option<Token<'text>>,
exhaustive: bool,
complete_options: Vec<&str>,
other_options: Vec<&str>)
-> Completion<'text> {
let mut all_options = complete_options.clone();
all_options.extend(other_options.iter().cloned());
let mut complete_options =
complete_options.iter().map(|o| o.to_string()).collect::<Vec<_>>();
let mut other_options = other_options.iter().map(|o| o.to_string()).collect::<Vec<_>>();
if let Some(t) = token {
let token_text = t.text.to_string();
complete_options.retain(|o| o.starts_with(t.text));
other_options.retain(|o| o.starts_with(t.text));
if !exhaustive && !complete_options.contains(&token_text) &&
!other_options.contains(&token_text) {
other_options.push(token_text);
}
}
let lcp = longest_common_prefix(all_options).to_string();
if !complete_options.contains(&lcp) && !other_options.contains(&lcp) {
match token {
Some(t) => {
if lcp != t.text {
other_options.push(lcp)
}
}
None => other_options.push(lcp),
}
}
let mut options = complete_options.iter()
.map(|o| CompletionOption::new(o.clone(), true))
.collect::<Vec<_>>();
options.extend(other_options.iter().map(|o| CompletionOption::new(o.clone(), false)));
Completion {
help_symbol: help_symbol,
help_text: help_text,
token: token,
exhaustive: exhaustive,
options: options,
}
}
}