use crate::history::Direction;
use crate::Context;
pub trait Hinter {
fn hint(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Option<String> {
let _ = (line, pos, ctx);
None
}
}
impl Hinter for () {}
impl<'r, H: ?Sized + Hinter> Hinter for &'r H {
fn hint(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Option<String> {
(**self).hint(line, pos, ctx)
}
}
pub struct HistoryHinter {}
impl Hinter for HistoryHinter {
fn hint(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Option<String> {
if pos < line.len() {
return None;
}
let start = if ctx.history_index() == ctx.history().len() {
ctx.history_index().saturating_sub(1)
} else {
ctx.history_index()
};
if let Some(history_index) =
ctx.history
.starts_with(&line[..pos], start, Direction::Reverse)
{
let entry = ctx.history.get(history_index);
if let Some(entry) = entry {
if entry == line || entry == &line[..pos] {
return None;
}
}
return entry.map(|s| s[pos..].to_owned());
}
None
}
}
#[cfg(test)]
mod test {
use super::{Hinter, HistoryHinter};
use crate::history::History;
use crate::Context;
#[test]
pub fn empty_history() {
let history = History::new();
let ctx = Context::new(&history);
let hinter = HistoryHinter {};
let hint = hinter.hint("test", 4, &ctx);
assert_eq!(None, hint);
}
}