diff options
author | Canop <cano.petrole@gmail.com> | 2020-12-13 16:41:35 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2020-12-13 17:22:07 +0100 |
commit | 2c06c7ca0a580618d932325e15d8d40e7640f24d (patch) | |
tree | 5764d009d564a514a285426be6ce497cc65660c9 /src/pattern | |
parent | ee4f93f4919be4e716bb9b73476473bbc8bd7159 (diff) |
keep selection visible on unfiltering
The selection was sometimes scrolled away on unfiltering.
Also change some formatting.
Diffstat (limited to 'src/pattern')
-rw-r--r-- | src/pattern/composite_pattern.rs | 65 | ||||
-rw-r--r-- | src/pattern/content_regex_pattern.rs | 2 | ||||
-rw-r--r-- | src/pattern/exact_pattern.rs | 5 | ||||
-rw-r--r-- | src/pattern/mod.rs | 2 | ||||
-rw-r--r-- | src/pattern/name_match.rs | 2 | ||||
-rw-r--r-- | src/pattern/operator.rs | 2 | ||||
-rw-r--r-- | src/pattern/search_mode.rs | 2 |
7 files changed, 38 insertions, 42 deletions
diff --git a/src/pattern/composite_pattern.rs b/src/pattern/composite_pattern.rs index 85ab8d6..96a9081 100644 --- a/src/pattern/composite_pattern.rs +++ b/src/pattern/composite_pattern.rs @@ -5,9 +5,7 @@ use { errors::PatternError, }, bet::*, - std::{ - path::Path, - }, + std::path::Path, }; /// A pattern composing other ones with operators @@ -18,9 +16,7 @@ pub struct CompositePattern { impl CompositePattern { pub fn new(expr: BeTree<PatternOperator, Pattern>) -> Self { - Self { - expr - } + Self { expr } } pub fn score_of_string(&self, candidate: &str) -> Option<i32> { @@ -29,22 +25,24 @@ impl CompositePattern { // score evaluation |pat| Ok(pat.score_of_string(candidate)), // operator - |op, a, b| Ok(match (op, a, b) { - (And, None, _) => None, // normally not called due to short-circuit - (And, Some(sa), Some(Some(sb))) => Some(sa+sb), - (Or, None, Some(Some(sb))) => Some(sb), - (Or, Some(sa), Some(None)) => Some(sa), - (Or, Some(sa), Some(Some(sb))) => Some(sa+sb), - (Not, Some(_), _) => None, - (Not, None, _) => Some(1), - _ => None, - }), + |op, a, b| { + Ok(match (op, a, b) { + (And, None, _) => None, // normally not called due to short-circuit + (And, Some(sa), Some(Some(sb))) => Some(sa + sb), + (Or, None, Some(Some(sb))) => Some(sb), + (Or, Some(sa), Some(None)) => Some(sa), + (Or, Some(sa), Some(Some(sb))) => Some(sa + sb), + (Not, Some(_), _) => None, + (Not, None, _) => Some(1), + _ => None, + }) + }, // short-circuit. We don't short circuit on 'or' because // we want to use both scores |op, a| match (op, a) { (And, None) => true, _ => false, - } + }, ); match composite_result { Err(e) => { @@ -65,22 +63,24 @@ impl CompositePattern { // score evaluation |pat| Ok(pat.score_of(candidate)), // operator - |op, a, b| Ok(match (op, a, b) { - (And, None, _) => None, // normally not called due to short-circuit - (And, Some(sa), Some(Some(sb))) => Some(sa+sb), - (Or, None, Some(Some(sb))) => Some(sb), - (Or, Some(sa), Some(None)) => Some(sa), - (Or, Some(sa), Some(Some(sb))) => Some(sa+sb), - (Not, Some(_), _) => None, - (Not, None, _) => Some(1), - _ => None, - }), + |op, a, b| { + Ok(match (op, a, b) { + (And, None, _) => None, // normally not called due to short-circuit + (And, Some(sa), Some(Some(sb))) => Some(sa + sb), + (Or, None, Some(Some(sb))) => Some(sb), + (Or, Some(sa), Some(None)) => Some(sa), + (Or, Some(sa), Some(Some(sb))) => Some(sa + sb), + (Not, Some(_), _) => None, + (Not, None, _) => Some(1), + _ => None, + }) + }, // short-circuit. We don't short circuit on 'or' because // we want to use both scores |op, a| match (op, a) { (And, None) => true, _ => false, - } + }, ); match composite_result { Err(e) => { @@ -95,10 +95,7 @@ impl CompositePattern { } } - pub fn search_string( - &self, - candidate: &str, - ) -> Option<NameMatch> { + pub fn search_string(&self, candidate: &str) -> Option<NameMatch> { // an ideal algorithm would call score_of on patterns when the object is different // to deal with exclusions but I'll start today with something simpler use PatternOperator::*; @@ -115,7 +112,7 @@ impl CompositePattern { |op, a| match (op, a) { (Or, Some(_)) => true, _ => false, - } + }, ); // it's possible we didn't find a result because the composition match composite_result { @@ -150,7 +147,7 @@ impl CompositePattern { |op, a| match (op, a) { (Or, Some(_)) => true, _ => false, - } + }, ); match composite_result { Err(e) => { diff --git a/src/pattern/content_regex_pattern.rs b/src/pattern/content_regex_pattern.rs index 8b53847..7bf7639 100644 --- a/src/pattern/content_regex_pattern.rs +++ b/src/pattern/content_regex_pattern.rs @@ -6,9 +6,9 @@ use { }, regex, std::{ - io::{self, BufReader, BufRead}, fmt, fs::File, + io::{self, BufReader, BufRead}, path::Path, }, }; diff --git a/src/pattern/exact_pattern.rs b/src/pattern/exact_pattern.rs index 6c0189c..e377e1d 100644 --- a/src/pattern/exact_pattern.rs +++ b/src/pattern/exact_pattern.rs @@ -52,7 +52,7 @@ impl ExactPattern { score += BONUS_EXACT; } } else { - if is_word_separator(candidate.as_bytes()[start-1]) { + if is_word_separator(candidate.as_bytes()[start - 1]) { score += BONUS_START_WORD; } score += BONUS_DISTANCE_FROM_START * start as i32; @@ -84,7 +84,8 @@ impl ExactPattern { /// compute the score of the best match pub fn score_of(&self, candidate: &str) -> Option<i32> { - candidate.find(&self.pattern) + candidate + .find(&self.pattern) .map(|start| self.score(start, candidate)) } } diff --git a/src/pattern/mod.rs b/src/pattern/mod.rs index 85167b5..d05c671 100644 --- a/src/pattern/mod.rs +++ b/src/pattern/mod.rs @@ -6,9 +6,9 @@ mod content_regex_pattern; mod exact_pattern; mod fuzzy_pattern; mod input_pattern; +mod name_match; mod operator; mod pattern; -mod name_match; mod pattern_object; mod pattern_parts; mod regex_pattern; diff --git a/src/pattern/name_match.rs b/src/pattern/name_match.rs index 91a2238..30f61a9 100644 --- a/src/pattern/name_match.rs +++ b/src/pattern/name_match.rs @@ -13,7 +13,7 @@ impl NameMatch { let mut index_in_pos = 0; let mut wrapped = false; for (idx, c) in name.chars().enumerate() { - if index_in_pos<self.pos.len() && self.pos[index_in_pos]==idx { + if index_in_pos < self.pos.len() && self.pos[index_in_pos] == idx { index_in_pos += 1; if !wrapped { result.push_str(match_start); diff --git a/src/pattern/operator.rs b/src/pattern/operator.rs index 86a69e7..b4d7643 100644 --- a/src/pattern/operator.rs +++ b/src/pattern/operator.rs @@ -1,5 +1,3 @@ - - /// operators combining patterns #[derive(Debug, Clone, Copy, PartialEq)] pub enum PatternOperator { diff --git a/src/pattern/search_mode.rs b/src/pattern/search_mode.rs index c25c737..1254c62 100644 --- a/src/pattern/search_mode.rs +++ b/src/pattern/search_mode.rs @@ -157,7 +157,7 @@ impl SearchModeMapEntry { // TODO look at issues and/or code in serde-toml None } else if regex!(r"^\w*/$").is_match(conf_key) { - Some(conf_key[0..conf_key.len()-1].to_string()) + Some(conf_key[0..conf_key.len() - 1].to_string()) } else { return Err(ConfError::InvalidKey { raw: conf_key.to_string(), |