use std::collections::HashMap;
use rustyline::{Changeset, Context, Editor, Helper};
use rustyline::completion::{Completer, FilenameCompleter};
use rustyline::highlight::Highlighter;
use rustyline::hint::Hinter;
use rustyline::history::DefaultHistory;
use rustyline::line_buffer::LineBuffer;
use rustyline::validate::Validator;
use crate::interface::Completion;
pub type CommandEditor = Editor<CommandHelper, DefaultHistory>;
pub struct CommandHelper {
commands: Vec<String>,
completions: HashMap<String, Completion>,
completer: FilenameCompleter,
}
impl CommandHelper {
pub fn new() -> CommandHelper {
let commands = Vec::new();
let completions = HashMap::new();
let completer = FilenameCompleter::new();
return Self { commands, completions, completer };
}
pub fn set_commands(&mut self, commands: Vec<String>) {
self.commands = commands;
}
pub fn set_completions(&mut self, completions: HashMap<String, Completion>) {
self.completions = completions;
}
fn requires_filename(&self, line: &str) -> bool {
let line = line.trim_start();
if let Some((command, remainder)) = line.split_once(' ') {
if let Some(Completion::Filename) = self.completions.get(command) {
let remainder = remainder.trim_start();
return !remainder.contains(' ');
}
}
return false;
}
fn complete_line(&self, line: &str, pos: usize) -> rustyline::Result<(usize, Vec<String>)> {
let line = &line[..pos];
if self.requires_filename(line) {
let (start, candidates) = self.completer.complete_path(line, pos)?;
let candidates = candidates.into_iter().map(|x| x.replacement).collect();
return Ok((start, candidates));
} else {
if let Some((start, initial)) = line.rsplit_once(' ') {
let start = start.len() + 1;
let candidates = self.complete_command(initial);
return Ok((start, candidates));
} else {
let candidates = self.complete_command(line);
return Ok((0, candidates));
}
}
}
fn complete_command(&self, initial: &str) -> Vec<String> {
self.commands.iter()
.filter(|x| x.starts_with(initial))
.map(|x| x.to_string())
.collect()
}
fn adjust_candidates(candidates: Vec<String>) -> Vec<String> {
if candidates.len() == 1 {
candidates.into_iter().map(CommandHelper::adjust_candidate).collect()
} else {
candidates
}
}
fn adjust_candidate(candidate: String) -> String {
if candidate.ends_with(&['\\', '/']) {
candidate
} else {
candidate + " "
}
}
}
impl Helper for CommandHelper {
}
impl Completer for CommandHelper {
type Candidate = String;
fn complete(
&self,
line: &str,
pos: usize,
_context: &Context<'_>,
) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
let (start, candidates) = self.complete_line(line, pos)?;
let candidates = CommandHelper::adjust_candidates(candidates);
return Ok((start, candidates));
}
fn update(
&self,
line: &mut LineBuffer,
start: usize,
elected: &str,
changes: &mut Changeset,
) {
let end = line.pos();
line.replace(start..end, &elected, changes);
}
}
impl Highlighter for CommandHelper {
}
impl Hinter for CommandHelper {
type Hint = String;
}
impl Validator for CommandHelper {
}
#[cfg(test)]
pub mod tests {
use std::collections::HashMap;
use crate::helper::CommandHelper;
use crate::interface::Completion;
#[test]
fn test_requires_filename_for_import_directive() {
let helper = create_helper();
assert_eq!(false, helper.requires_filename(""));
assert_eq!(false, helper.requires_filename("import"));
assert_eq!(true, helper.requires_filename("import "));
assert_eq!(true, helper.requires_filename("import file"));
assert_eq!(false, helper.requires_filename("import file "));
assert_eq!(false, helper.requires_filename("import file 999"));
assert_eq!(false, helper.requires_filename(" "));
assert_eq!(false, helper.requires_filename(" import"));
assert_eq!(true, helper.requires_filename(" import "));
assert_eq!(true, helper.requires_filename(" import file"));
assert_eq!(false, helper.requires_filename(" import file "));
assert_eq!(false, helper.requires_filename(" import file 999"));
}
#[test]
fn test_requires_filename_for_define_directive() {
let helper = create_helper();
assert_eq!(false, helper.requires_filename(""));
assert_eq!(false, helper.requires_filename("define"));
assert_eq!(false, helper.requires_filename("define "));
assert_eq!(false, helper.requires_filename("define file"));
assert_eq!(false, helper.requires_filename("define file "));
assert_eq!(false, helper.requires_filename("define file 999"));
assert_eq!(false, helper.requires_filename(" "));
assert_eq!(false, helper.requires_filename(" define"));
assert_eq!(false, helper.requires_filename(" define "));
assert_eq!(false, helper.requires_filename(" define file"));
assert_eq!(false, helper.requires_filename(" define file "));
assert_eq!(false, helper.requires_filename(" define file 999"));
}
#[test]
fn test_requires_filename_for_unknown_directive() {
let helper = create_helper();
assert_eq!(false, helper.requires_filename(""));
assert_eq!(false, helper.requires_filename("unknown"));
assert_eq!(false, helper.requires_filename("unknown "));
assert_eq!(false, helper.requires_filename("unknown file"));
assert_eq!(false, helper.requires_filename("unknown file "));
assert_eq!(false, helper.requires_filename("unknown file 999"));
assert_eq!(false, helper.requires_filename(" "));
assert_eq!(false, helper.requires_filename(" unknown"));
assert_eq!(false, helper.requires_filename(" unknown "));
assert_eq!(false, helper.requires_filename(" unknown file"));
assert_eq!(false, helper.requires_filename(" unknown file "));
assert_eq!(false, helper.requires_filename(" unknown file 999"));
}
#[test]
fn test_only_token_is_completed_with_no_matches() {
let helper = create_helper();
let (start, candidates) = helper.complete_line("xxx 999", 3).unwrap();
assert_eq!(start, 0);
assert!(candidates.is_empty())
}
#[test]
fn test_final_token_is_completed_with_no_matches() {
let helper = create_helper();
let (start, candidates) = helper.complete_line("1 2 xxx 999", 7).unwrap();
assert_eq!(start, 4);
assert!(candidates.is_empty())
}
#[test]
fn test_only_token_is_completed_with_one_match() {
let helper = create_helper();
let (start, candidates) = helper.complete_line("aaa 999", 3).unwrap();
assert_eq!(start, 0);
assert_eq!(candidates, vec![String::from("aaaaa123")]);
}
#[test]
fn test_final_token_is_completed_with_one_match() {
let helper = create_helper();
let (start, candidates) = helper.complete_line("1 2 aaa 999", 7).unwrap();
assert_eq!(start, 4);
assert_eq!(candidates, vec![String::from("aaaaa123")]);
}
#[test]
fn test_only_token_is_completed_with_multiple_matches() {
let helper = create_helper();
let (start, candidates) = helper.complete_line("bbb 999", 3).unwrap();
assert_eq!(start, 0);
assert_eq!(candidates, vec![String::from("bbbbb456"), String::from("bbbbb789")]);
}
#[test]
fn test_final_token_is_completed_with_multiple_matches() {
let helper = create_helper();
let (start, candidates) = helper.complete_line("1 2 bbb 999", 7).unwrap();
assert_eq!(start, 4);
assert_eq!(candidates, vec![String::from("bbbbb456"), String::from("bbbbb789")]);
}
fn create_helper() -> CommandHelper {
let mut helper = CommandHelper::new();
helper.set_commands(vec![
String::from("aaaaa123"),
String::from("bbbbb456"),
String::from("bbbbb789"),
]);
helper.set_completions(HashMap::from([
(String::from("import"), Completion::Filename),
(String::from("export"), Completion::Filename),
(String::from("define"), Completion::Keyword),
]));
return helper;
}
#[test]
fn test_space_is_added_to_single_candidate() {
assert_eq!(Vec::<String>::new(), adjust_candidates(vec![]));
assert_eq!(vec!["foo "], adjust_candidates(vec!["foo"]));
assert_eq!(vec!["foo", "bar"], adjust_candidates(vec!["foo", "bar"]));
assert_eq!(vec!["subdir\\"], adjust_candidates(vec!["subdir\\"]));
assert_eq!(vec!["subdir/"], adjust_candidates(vec!["subdir/"]));
}
fn adjust_candidates(candidates: Vec<&str>) -> Vec<String> {
let candidates = candidates.into_iter().map(String::from).collect();
return CommandHelper::adjust_candidates(candidates);
}
}