From 5b8803e29fbf3f8d89ed281f355cd7ebb2a251a4 Mon Sep 17 00:00:00 2001 From: Kyler Clay Date: Mon, 12 May 2025 16:32:44 -0400 Subject: [PATCH] improved hinting --- src/prompt/highlight.rs | 1 - src/prompt/readline.rs | 25 +++++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/prompt/highlight.rs b/src/prompt/highlight.rs index eef41e8..521b4da 100644 --- a/src/prompt/highlight.rs +++ b/src/prompt/highlight.rs @@ -98,7 +98,6 @@ impl FernHighlighter { .rev() .collect::>(); for token in tokens { - flog!(DEBUG, token.flags); match token.class { _ if token.flags.intersects(TkFlags::IS_CMDSUB | TkFlags::IS_SUBSH) => { let styled = self.highlight_subsh(token.clone()); diff --git a/src/prompt/readline.rs b/src/prompt/readline.rs index 017afc2..5ff6c98 100644 --- a/src/prompt/readline.rs +++ b/src/prompt/readline.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use rustyline::{completion::Completer, hint::{Hint, Hinter}, validate::{ValidationResult, Validator}, Helper}; +use rustyline::{completion::Completer, hint::{Hint, Hinter}, history::SearchDirection, validate::{ValidationResult, Validator}, Helper}; use crate::{libsh::term::{Style, Styled}, parse::{lex::{LexFlags, LexStream}, ParseStream}}; use crate::prelude::*; @@ -10,7 +10,17 @@ pub struct FernReadline; impl FernReadline { pub fn new() -> Self { - Self::default() + Self + } + pub fn search_hist(value: &str, ctx: &rustyline::Context<'_>) -> Option { + let len = ctx.history().len(); + for i in 0..len { + let entry = ctx.history().get(i, SearchDirection::Reverse).unwrap().unwrap(); + if entry.entry.starts_with(value) { + return Some(entry.entry.into_owned()) + } + } + None } } @@ -48,12 +58,11 @@ impl Hint for FernHint { impl Hinter for FernReadline { type Hint = FernHint; fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option { - let ent = ctx.history().search( - line, - ctx.history().len().saturating_sub(1), - rustyline::history::SearchDirection::Reverse - ).ok()??; - let entry_raw = ent.entry.get(pos..)?.to_string(); + if line.is_empty() { + return None + } + let ent = Self::search_hist(line,ctx)?; + let entry_raw = ent.get(pos..)?.to_string(); Some(FernHint::new(entry_raw)) } }