Make function more concise

This commit is contained in:
greg 2019-03-20 00:24:46 -07:00
parent 5b5368ce6f
commit 153e7977d3

View File

@ -308,43 +308,43 @@ impl<T: Terminal> Completer<T> for TabCompleteHandler {
fn complete(&self, word: &str, prompter: &::linefeed::prompter::Prompter<T>, start: usize, _end: usize) -> Option<Vec<Completion>> { fn complete(&self, word: &str, prompter: &::linefeed::prompter::Prompter<T>, start: usize, _end: usize) -> Option<Vec<Completion>> {
let line = prompter.buffer(); let line = prompter.buffer();
if line.starts_with(&format!("{}", self.sigil)) { if !line.starts_with(self.sigil) {
let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace(); return None;
let mut completions = Vec::new(); }
let mut command_tree: Option<&CommandTree> = Some(&self.top_level_commands);
loop { let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace();
match words.next() { let mut completions = Vec::new();
None => { let mut command_tree: Option<&CommandTree> = Some(&self.top_level_commands);
let top = match command_tree {
Some(CommandTree::Top(_)) => true, loop {
_ => false match words.next() {
}; None => {
let word = if top { word.get(1..).unwrap() } else { word }; let top = match command_tree {
for cmd in command_tree.map(|x| x.get_children()).unwrap_or(vec![]).into_iter() { Some(CommandTree::Top(_)) => true,
if cmd.starts_with(word) { _ => false
completions.push(Completion { };
completion: format!("{}{}", if top { ":" } else { "" }, cmd), let word = if top { word.get(1..).unwrap() } else { word };
display: Some(cmd.to_string()), for cmd in command_tree.map(|x| x.get_children()).unwrap_or(vec![]).into_iter() {
suffix: ::linefeed::complete::Suffix::Some(' ') if cmd.starts_with(word) {
}) completions.push(Completion {
} completion: format!("{}{}", if top { ":" } else { "" }, cmd),
display: Some(cmd.to_string()),
suffix: ::linefeed::complete::Suffix::Some(' ')
})
} }
break;
},
Some(s) => {
let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm {
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
CommandTree::NonTerminal { children, .. } => children.iter().find(|c| c.get_cmd() == s),
CommandTree::Terminal { .. } => None,
});
command_tree = new_ptr;
} }
break;
},
Some(s) => {
let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm {
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
CommandTree::NonTerminal { children, .. } => children.iter().find(|c| c.get_cmd() == s),
CommandTree::Terminal { .. } => None,
});
command_tree = new_ptr;
} }
} }
Some(completions)
} else {
None
} }
Some(completions)
} }
} }