From 27224f46566e2af8211b5e172beb0e11dd039728 Mon Sep 17 00:00:00 2001 From: Basile Henry Date: Sat, 6 Apr 2024 20:18:35 +0200 Subject: [PATCH] helix-term: Switch to using substring instead of prefix for partial history --- helix-term/src/ui/prompt.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index e169f41bc..3eac78196 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -4,7 +4,6 @@ use arc_swap::ArcSwap; use helix_core::syntax; use helix_view::input::KeyEvent; use helix_view::keyboard::KeyCode; -use std::num::NonZeroUsize; use std::sync::Arc; use std::{borrow::Cow, ops::RangeFrom}; use tui::buffer::Buffer as Surface; @@ -32,7 +31,7 @@ pub struct Prompt { selection: Option, history_register: Option, history_pos: Option, - history_prefix: Option, + history_substring: Option, completion_fn: CompletionFn, callback_fn: CallbackFn, pub doc_fn: DocFn, @@ -85,7 +84,7 @@ impl Prompt { selection: None, history_register, history_pos: None, - history_prefix: None, + history_substring: None, completion_fn: Box::new(completion_fn), callback_fn: Box::new(callback_fn), doc_fn: Box::new(|_| None), @@ -319,7 +318,7 @@ impl Prompt { pub fn reset_history(&mut self) { self.history_pos = None; - self.history_prefix = None; + self.history_substring = None; } pub fn change_history( @@ -334,8 +333,10 @@ impl Prompt { _ => return, }; - if self.history_pos.is_none() { - self.history_prefix = NonZeroUsize::new(self.line.len()); + // Using `history_pos` as the trigger so that we only consider updating + // the substring to match on when entering history + if self.history_pos.is_none() && !self.line.is_empty() { + self.history_substring = Some(self.line.clone()) } let index = match direction { @@ -346,16 +347,14 @@ impl Prompt { .saturating_sub(1), }; - let history_line = if let Some(prefix_len) = self.history_prefix { - let prefix = &self.line[..prefix_len.get()]; - + let history_line = if let Some(substr) = self.history_substring.as_ref() { match direction { CompletionDirection::Forward => { if index > 0 { // Same as skip but without taking ownership let _ = values.nth(index - 1); } - values.find(|prev| prev.1.starts_with(prefix)) + values.find(|prev| prev.1.find(substr).is_some()) } CompletionDirection::Backward => { let r_index = values.len() - 1 - index; @@ -363,7 +362,7 @@ impl Prompt { // Same as skip but without taking ownership let _ = values.nth_back(r_index - 1); } - values.rfind(|prev| prev.1.starts_with(prefix)) + values.rfind(|prev| prev.1.find(substr).is_some()) } } } else {