From 153e7977d359ea23bc112ce44d58e8f2687cc1c4 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 20 Mar 2019 00:24:46 -0700 Subject: [PATCH] Make function more concise --- schala-repl/src/repl/mod.rs | 64 ++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/schala-repl/src/repl/mod.rs b/schala-repl/src/repl/mod.rs index 276f968..51bb3b4 100644 --- a/schala-repl/src/repl/mod.rs +++ b/schala-repl/src/repl/mod.rs @@ -308,43 +308,43 @@ impl Completer for TabCompleteHandler { fn complete(&self, word: &str, prompter: &::linefeed::prompter::Prompter, start: usize, _end: usize) -> Option> { let line = prompter.buffer(); - if line.starts_with(&format!("{}", self.sigil)) { - let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace(); - let mut completions = Vec::new(); - let mut command_tree: Option<&CommandTree> = Some(&self.top_level_commands); + if !line.starts_with(self.sigil) { + return None; + } - loop { - match words.next() { - None => { - let top = match command_tree { - Some(CommandTree::Top(_)) => true, - _ => false - }; - let word = if top { word.get(1..).unwrap() } else { word }; - for cmd in command_tree.map(|x| x.get_children()).unwrap_or(vec![]).into_iter() { - if cmd.starts_with(word) { - completions.push(Completion { - completion: format!("{}{}", if top { ":" } else { "" }, cmd), - display: Some(cmd.to_string()), - suffix: ::linefeed::complete::Suffix::Some(' ') - }) - } + let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace(); + let mut completions = Vec::new(); + let mut command_tree: Option<&CommandTree> = Some(&self.top_level_commands); + + loop { + match words.next() { + None => { + let top = match command_tree { + Some(CommandTree::Top(_)) => true, + _ => false + }; + let word = if top { word.get(1..).unwrap() } else { word }; + for cmd in command_tree.map(|x| x.get_children()).unwrap_or(vec![]).into_iter() { + 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) } }