fix negative matches

This commit is contained in:
Pascal Kuthe 2023-08-08 00:06:49 +02:00
parent a2b0491fad
commit 3abe7b4c02
No known key found for this signature in database
GPG Key ID: D715E8655AE166A6
2 changed files with 15 additions and 5 deletions

View File

@ -351,8 +351,8 @@ impl Pattern {
} }
pub fn parse_from(&mut self, pattern: &str, append: bool) { pub fn parse_from(&mut self, pattern: &str, append: bool) {
self.atoms.clear();
let invert = self.atoms.last().map_or(false, |pat| pat.invert); let invert = self.atoms.last().map_or(false, |pat| pat.invert);
self.atoms.clear();
let atoms = pattern_atoms(pattern).filter_map(|atom| { let atoms = pattern_atoms(pattern).filter_map(|atom| {
let atom = PatternAtom::parse(atom, self.normalize, self.case_matching); let atom = PatternAtom::parse(atom, self.normalize, self.case_matching);
if atom.needle.is_empty() { if atom.needle.is_empty() {

View File

@ -1,4 +1,4 @@
use crate::pattern::PatternAtom; use crate::pattern::{PatternAtom, Status};
use crate::{CaseMatching, Pattern, PatternKind}; use crate::{CaseMatching, Pattern, PatternKind};
fn parse_atom(pat: &str) -> PatternAtom { fn parse_atom(pat: &str) -> PatternAtom {
@ -6,14 +6,14 @@ fn parse_atom(pat: &str) -> PatternAtom {
} }
fn parse_atom_with(pat: &str, case_matching: CaseMatching) -> PatternAtom { fn parse_atom_with(pat: &str, case_matching: CaseMatching) -> PatternAtom {
let mut pat = parse_with(pat, case_matching); let mut pat = parse_with(pat, case_matching, false);
assert_eq!(pat.atoms.len(), 1); assert_eq!(pat.atoms.len(), 1);
pat.atoms.remove(0) pat.atoms.remove(0)
} }
fn parse_with(pat: &str, case_matching: CaseMatching) -> Pattern { fn parse_with(pat: &str, case_matching: CaseMatching, append: bool) -> Pattern {
let mut res = Pattern::new(&nucleo_matcher::MatcherConfig::DEFAULT, case_matching); let mut res = Pattern::new(&nucleo_matcher::MatcherConfig::DEFAULT, case_matching);
res.parse_from(pat, false); res.parse_from(pat, append);
res res
} }
@ -133,3 +133,13 @@ fn escape() {
assert_eq!(pat.needle.to_string(), "^foo$"); assert_eq!(pat.needle.to_string(), "^foo$");
assert_eq!(pat.kind, PatternKind::Substring); assert_eq!(pat.kind, PatternKind::Substring);
} }
#[test]
fn append() {
let mut pat = parse_with("!", CaseMatching::Smart, true);
assert_eq!(pat.status, Status::Update);
pat.parse_from("!f", true);
assert_eq!(pat.status, Status::Update);
pat.parse_from("!fo", true);
assert_eq!(pat.status, Status::Rescore);
}