From b07a112f894599a35a58ea536b96f342f2bc64d8 Mon Sep 17 00:00:00 2001 From: sigoden Date: Tue, 7 Nov 2023 21:45:36 +0800 Subject: feat: deprecate `.edit`, use """ instead (#225) --- src/repl/mod.rs | 47 +++++++++++++++++++++++++++-------------------- src/repl/validator.rs | 16 ---------------- 2 files changed, 27 insertions(+), 36 deletions(-) delete mode 100644 src/repl/validator.rs (limited to 'src') diff --git a/src/repl/mod.rs b/src/repl/mod.rs index a4f397c..75d2627 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -1,12 +1,10 @@ mod completer; mod highlighter; mod prompt; -mod validator; use self::completer::ReplCompleter; use self::highlighter::ReplHighlighter; use self::prompt::ReplPrompt; -use self::validator::ReplValidator; use crate::client::init_client; use crate::config::GlobalConfig; @@ -21,16 +19,15 @@ use reedline::Signal; use reedline::{ default_emacs_keybindings, default_vi_insert_keybindings, default_vi_normal_keybindings, ColumnarMenu, EditMode, Emacs, KeyCode, KeyModifiers, Keybindings, Reedline, ReedlineEvent, - ReedlineMenu, Vi, + ReedlineMenu, ValidationResult, Validator, Vi, }; use std::io::Read; const MENU_NAME: &str = "completion_menu"; -const REPL_COMMANDS: [(&str, &str); 14] = [ +const REPL_COMMANDS: [(&str, &str); 13] = [ (".help", "Print this help message"), (".info", "Print system info"), - (".edit", "Multi-line editing (CTRL+S to finish)"), (".model", "Switch LLM model"), (".role", "Use a role"), (".info role", "Show role info"), @@ -46,7 +43,7 @@ const REPL_COMMANDS: [(&str, &str); 14] = [ lazy_static! { static ref COMMAND_RE: Regex = Regex::new(r"^\s*(\.\S*)\s*").unwrap(); - static ref EDIT_RE: Regex = Regex::new(r"^\s*\.edit\s*").unwrap(); + static ref MULTILINE_RE: Regex = Regex::new(r#"(?ms)^\s*"""\s*(.*)\s*"""\s*$"#).unwrap(); } pub struct Repl { @@ -120,7 +117,12 @@ impl Repl { Ok(()) } - fn handle(&self, line: &str) -> Result { + fn handle(&self, mut line: &str) -> Result { + if let Ok(Some(captures)) = MULTILINE_RE.captures(line) { + if let Some(text_match) = captures.get(1) { + line = text_match.as_str(); + } + } match parse_command(line) { Some((cmd, args)) => match cmd { ".help" => { @@ -142,9 +144,7 @@ impl Repl { } }, ".edit" => { - if let Some(text) = args { - self.ask(text)?; - } + println!(r#"Deprecated. Use """ instead."#); } ".model" => match args { Some(name) => { @@ -278,11 +278,6 @@ Type ".help" for more information. ReedlineEvent::MenuNext, ]), ); - keybindings.add_binding( - KeyModifiers::CONTROL, - KeyCode::Char('s'), - ReedlineEvent::Submit, - ); } fn create_edit_mode(config: &GlobalConfig) -> Box { @@ -314,6 +309,20 @@ Type ".help" for more information. } } +/// A default validator which checks for mismatched quotes and brackets +struct ReplValidator; + +impl Validator for ReplValidator { + fn validate(&self, line: &str) -> ValidationResult { + let line = line.trim(); + if line.starts_with(r#"""""#) && !line[3..].ends_with(r#"""""#) { + ValidationResult::Incomplete + } else { + ValidationResult::Complete + } + } +} + fn unknown_command() -> Result<()> { bail!(r#"Unknown command. Type ".help" for more information."#); } @@ -327,7 +336,8 @@ fn dump_repl_help() { println!( r###"{head} -Press Ctrl+C to abort readline, Ctrl+D to exit the REPL"###, +Type """ to begin multi-line editing, type """ to end it. +Press Ctrl+C to abort aichat, Ctrl+D to exit the REPL"###, ); } @@ -342,6 +352,7 @@ fn parse_command(line: &str) -> Option<(&str, Option<&str>)> { _ => None, } } + #[cfg(test)] mod tests { use super::*; @@ -363,9 +374,5 @@ mod tests { parse_command(".prompt \nabc\n"), Some((".prompt", Some("abc"))) ); - assert_eq!( - parse_command(".edit\r\nabc\r\n"), - Some((".edit", Some("abc"))) - ); } } diff --git a/src/repl/validator.rs b/src/repl/validator.rs deleted file mode 100644 index 21a7a3c..0000000 --- a/src/repl/validator.rs +++ /dev/null @@ -1,16 +0,0 @@ -use super::EDIT_RE; - -use reedline::{ValidationResult, Validator}; - -/// A default validator which checks for mismatched quotes and brackets -pub struct ReplValidator; - -impl Validator for ReplValidator { - fn validate(&self, line: &str) -> ValidationResult { - if let Ok(true) = EDIT_RE.is_match(line) { - ValidationResult::Incomplete - } else { - ValidationResult::Complete - } - } -} -- cgit v1.2.3