From 762da3dbf4d788f7b9508b06f39780185cba1475 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Sun, 26 Nov 2023 19:24:31 +0100 Subject: [PATCH] fzf: add `FzfParser::parse_not_extended` --- src/algos/fzf/parser.rs | 19 ++++++++++++++++--- src/algos/fzf/query.rs | 26 +++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/algos/fzf/parser.rs b/src/algos/fzf/parser.rs index d355330..458badc 100644 --- a/src/algos/fzf/parser.rs +++ b/src/algos/fzf/parser.rs @@ -35,6 +35,12 @@ impl core::fmt::Debug for FzfParser { } impl FzfParser { + /// TODO: docs + #[inline] + pub fn new() -> Self { + Self::default() + } + /// TODO: docs #[inline] pub fn parse<'a>(&'a mut self, query: &str) -> FzfQuery<'a> { @@ -85,13 +91,20 @@ impl FzfParser { num_conditions += 1; } - FzfQuery::new(&self.conditions[..num_conditions]) + FzfQuery::new_extended(&self.conditions[..num_conditions]) } /// TODO: docs #[inline] - pub fn new() -> Self { - Self::default() + pub fn parse_not_extended<'a>(&'a mut self, query: &str) -> FzfQuery<'a> { + let mut char_len = 0; + + for ch in query.chars() { + self.chars[char_len] = ch; + char_len += 1; + } + + FzfQuery::new_not_extended(&self.chars[..char_len]) } } diff --git a/src/algos/fzf/query.rs b/src/algos/fzf/query.rs index f0aaae0..24f4313 100644 --- a/src/algos/fzf/query.rs +++ b/src/algos/fzf/query.rs @@ -47,7 +47,7 @@ impl<'a> FzfQuery<'a> { /// TODO: docs #[inline] - pub(super) fn new(conditions: &'a [Condition<'a>]) -> Self { + pub(super) fn new_extended(conditions: &'a [Condition<'a>]) -> Self { // If there's only one condition with a single pattern, and that // pattern is fuzzy, then we can use the non-extended search mode. if conditions.len() == 1 { @@ -68,6 +68,12 @@ impl<'a> FzfQuery<'a> { Self { search_mode: SearchMode::Extended(conditions) } } + + /// TODO: docs + #[inline] + pub(super) fn new_not_extended(chars: &'a [char]) -> Self { + Self { search_mode: SearchMode::NotExtended(Pattern::raw(chars)) } + } } /// TODO: docs @@ -193,6 +199,24 @@ impl<'a> Pattern<'a> { self.leading_spaces } + /// TODO: docs + #[inline] + fn raw(text: &'a [char]) -> Self { + let leading_spaces = text.iter().take_while(|&&c| c == ' ').count(); + + let trailing_spaces = + text.iter().rev().take_while(|&&c| c == ' ').count(); + + Self { + leading_spaces, + trailing_spaces, + has_uppercase: text.iter().copied().any(char::is_uppercase), + text, + match_type: MatchType::Fuzzy, + is_inverse: false, + } + } + /// TODO: docs #[inline] pub(super) fn parse(mut text: &'a [char]) -> Self {