Skip to content

Commit

Permalink
fix content search result not displayed in tree in composite
Browse files Browse the repository at this point in the history
(regression)
  • Loading branch information
Canop committed Oct 7, 2024
1 parent 3022096 commit 8c81e5a
Showing 1 changed file with 45 additions and 69 deletions.
114 changes: 45 additions & 69 deletions src/pattern/composite_pattern.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use {
super::*,
crate::{
content_search::ContentMatch,
},
crate::content_search::ContentMatch,
bet::*,
smallvec::smallvec,
std::path::Path,
};

Expand All @@ -19,7 +16,10 @@ impl CompositePattern {
Self { expr }
}

pub fn score_of_string(&self, candidate: &str) -> Option<i32> {
pub fn score_of_string(
&self,
candidate: &str,
) -> Option<i32> {
use PatternOperator::*;
let composite_result: Option<Option<i32>> = self.expr.eval(
// score evaluation
Expand All @@ -44,14 +44,16 @@ impl CompositePattern {
_ => false,
},
);
composite_result
.unwrap_or_else(||{
warn!("unexpectedly missing result ");
None
})
composite_result.unwrap_or_else(|| {
warn!("unexpectedly missing result ");
None
})
}

pub fn score_of(&self, candidate: Candidate) -> Option<i32> {
pub fn score_of(
&self,
candidate: Candidate,
) -> Option<i32> {
use PatternOperator::*;
let composite_result: Option<Option<i32>> = self.expr.eval(
// score evaluation
Expand All @@ -76,14 +78,17 @@ impl CompositePattern {
_ => false,
},
);
composite_result
.unwrap_or_else(||{
warn!("unexpectedly missing result ");
None
})
composite_result.unwrap_or_else(|| {
warn!("unexpectedly missing result ");
None
})
}

pub fn search_string(&self, candidate: &str) -> Option<NameMatch> {
// Search for a string, trying to return a match
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::*;
Expand All @@ -92,36 +97,25 @@ impl CompositePattern {
|pat| pat.search_string(candidate),
// operator
|op, a, b| match (op, a, b) {
(And, None, _) => None, // normally not called due to short-circuit
(And, Some(sa), Some(Some(_))) => Some(sa), // we have to choose a match
(Or, None, Some(Some(sb))) => Some(sb),
(Or, Some(sa), Some(None)) => Some(sa),
(Or, Some(sa), Some(Some(_))) => Some(sa), // we have to choose
(Not, Some(_), _) => None,
(Not, None, _) => {
// this is quite arbitrary. Matching the whole string might be
// costly for some use, so we match only the start
Some(NameMatch {
score: 1,
pos: smallvec![0],
})
}
(_, Some(ma), _) => Some(ma),
(_, None, Some(omb)) => omb,
_ => None,
},
|op, a| match (op, a) {
(Or, Some(_)) => true,
(And, None) => true,
_ => false,
},
);
// it's possible we didn't find a result because the composition
composite_result
.unwrap_or_else(||{
warn!("unexpectedly missing result ");
None
})
composite_result.unwrap_or_else(|| {
warn!("unexpectedly missing result ");
None
})
}

// Search for a string in content, trying to return a match as soon as some
// part of the composite matches
pub fn search_content(
&self,
candidate: &Path,
Expand All @@ -133,34 +127,20 @@ impl CompositePattern {
|pat| pat.search_content(candidate, desired_len),
// operator
|op, a, b| match (op, a, b) {
(And, None, _) => None, // normally not called due to short-circuit
(And, Some(sa), Some(Some(_))) => Some(sa), // we have to choose
(Or, None, Some(Some(sb))) => Some(sb),
(Or, Some(sa), Some(None)) => Some(sa),
(Or, Some(sa), Some(Some(_))) => Some(sa), // we have to choose
(Not, Some(_), _) => None,
(Not, None, _) => {
// We can't generate a content match for a whole file
// content, so we build one of length 0.
Some(ContentMatch {
extract: "".to_string(),
needle_start: 0,
needle_end: 0,
})
}
(_, Some(ma), _) => Some(ma),
(_, None, Some(omb)) => omb,
_ => None,
},
|op, a| match (op, a) {
(Or, Some(_)) => true,
(And, None) => true,
_ => false,
},
);
composite_result
.unwrap_or_else(||{
warn!("unexpectedly missing result ");
None
})
composite_result.unwrap_or_else(|| {
warn!("unexpectedly missing result ");
None
})
}

pub fn get_match_line_count(
Expand All @@ -183,25 +163,21 @@ impl CompositePattern {
_ => false,
},
);
composite_result
.unwrap_or_else(||{
warn!("unexpectedly missing result ");
None
})
composite_result.unwrap_or_else(|| {
warn!("unexpectedly missing result ");
None
})
}

pub fn has_real_scores(&self) -> bool {
self.expr.iter_atoms()
.fold(false, |r, p| match p {
Pattern::NameFuzzy(_) | Pattern::PathFuzzy(_) => true,
_ => r,
})
self.expr.iter_atoms().fold(false, |r, p| match p {
Pattern::NameFuzzy(_) | Pattern::PathFuzzy(_) => true,
_ => r,
})
}

pub fn is_empty(&self) -> bool {
let is_not_empty = self.expr.iter_atoms()
.any(|p| p.is_some());
let is_not_empty = self.expr.iter_atoms().any(|p| p.is_some());
!is_not_empty
}

}

0 comments on commit 8c81e5a

Please sign in to comment.