From a9c0341d3817ccc3bdc1745fcca43a5086487678 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 7 May 2018 01:42:19 -0700 Subject: [PATCH] Half-assed implemention of tab completion Bah this is boring. Maybe I want to switch back to linefeed? --- schala-repl/Cargo.toml | 1 + schala-repl/src/lib.rs | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/schala-repl/Cargo.toml b/schala-repl/Cargo.toml index 5ee7a85..7ea7fc9 100644 --- a/schala-repl/Cargo.toml +++ b/schala-repl/Cargo.toml @@ -20,6 +20,7 @@ rocket_contrib = "0.3.5" phf = "0.7.12" includedir = "0.2.0" rustyline = "1.0.0" +regex = "0.2" [build-dependencies] includedir_codegen = "0.2.0" diff --git a/schala-repl/src/lib.rs b/schala-repl/src/lib.rs index 102bbd1..241fbb8 100644 --- a/schala-repl/src/lib.rs +++ b/schala-repl/src/lib.rs @@ -123,11 +123,37 @@ fn run_noninteractive(filename: &str, languages: Vec, + sigil: char, +} + +impl TabCompleteHandler { + fn complete_interpreter_directive(&self, line: &str, pos: usize) -> rustyline::Result<(usize, Vec)> { + let mut iter = line.chars(); + iter.next(); + let commands: Vec<&str> = iter + .as_str() + .split_whitespace() + .collect(); + println!("POS {}---", pos); + let completes = match &commands[..] { + &["debug", "show"] | &["debug", "hide"] => self.passes.clone(), + &["debug"] | &["debug", _] => vec!["passes".to_string(), "show".to_string(), "hide".to_string()], + &[_cmd] => vec!["debug".to_string()], + _ => vec![], + }; + Ok((pos, completes)) + } +} impl rustyline::completion::Completer for TabCompleteHandler { fn complete(&self, line: &str, pos: usize) -> rustyline::Result<(usize, Vec)> { - Ok((pos, vec!(format!("tab-completion-no-done"), format!("tab-completion-still-not-done")))) + if line.starts_with(&format!("{}", self.sigil)) { + self.complete_interpreter_directive(line, pos) + } else { + Ok((pos, vec!(format!("tab-completion-no-done"), format!("tab-completion-still-not-done")))) + } } } @@ -143,8 +169,7 @@ impl Repl { fn new(languages: Vec>, initial_index: usize) -> Repl { let i = if initial_index < languages.len() { initial_index } else { 0 }; - let mut console = Editor::::new(); - console.set_completer(Some(TabCompleteHandler {})); + let console = Editor::::new(); Repl { options: Repl::get_options(), @@ -193,6 +218,8 @@ impl Repl { loop { let language_name = self.languages[self.current_language_index].get_language_name(); + let tab_complete_handler = TabCompleteHandler { sigil: self.interpreter_directive_sigil, passes: self.get_cur_language().get_passes() }; + self.console.set_completer(Some(tab_complete_handler)); let prompt_str = format!("{} >> ", language_name); match self.console.readline(&prompt_str) {