1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use {
super::*,
crate::content_search::ContentMatch,
bet::*,
smallvec::smallvec,
std::path::Path,
};
/// A pattern composing other ones with operators
#[derive(Debug, Clone)]
pub struct CompositePattern {
pub expr: BeTree<PatternOperator, Pattern>,
pub content_search: bool,
}
impl CompositePattern {
pub fn new(expr: BeTree<PatternOperator, Pattern>) -> Self {
let content_search = expr.iter_atoms().any(|p| p.is_content_search());
Self {
expr,
content_search,
}
}
pub fn is_content_search(&self) -> bool {
self.content_search
}
pub fn score_of_string(
&self,
candidate: &str,
) -> Option<i32> {
use PatternOperator as PO;
let composite_result: Option<Option<i32>> = self.expr.eval(
// score evaluation
|pat| pat.score_of_string(candidate),
// operator
|op, a, b| {
match (op, a, b) {
(PO::And, None, _) => None, // normally not called due to short-circuit
(PO::And, Some(sa), Some(Some(sb))) => Some(sa + sb),
(PO::Or, None, Some(Some(sb))) => Some(sb),
(PO::Or, Some(sa), Some(None)) => Some(sa),
(PO::Or, Some(sa), Some(Some(sb))) => Some(sa + sb),
(PO::Not, Some(_), _) => None,
(PO::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) {
(PO::And, None) => true,
_ => false,
},
);
composite_result.unwrap_or_else(|| {
warn!("unexpectedly missing result ");
None
})
}
pub fn score_of(
&self,
candidate: Candidate,
) -> Option<i32> {
use PatternOperator as PO;
if self.is_content_search() && candidate.path.is_dir() {
// we can't score content on a directory
return None;
}
let composite_result: Option<Option<i32>> = self.expr.eval(
// score evaluation
|pat| pat.score_of(candidate),
// operator
|op, a, b| {
match (op, a, b) {
(PO::And, None, _) => None, // normally not called due to short-circuit
(PO::And, Some(sa), Some(Some(sb))) => Some(sa + sb),
(PO::Or, None, Some(Some(sb))) => Some(sb),
(PO::Or, Some(sa), Some(None)) => Some(sa),
(PO::Or, Some(sa), Some(Some(sb))) => Some(sa + sb),
(PO::Not, Some(_), _) => None,
(PO::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) {
(PO::And, None) => true,
_ => false,
},
);
composite_result.unwrap_or_else(|| {
warn!("unexpectedly missing result ");
None
})
}
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::*;
let composite_result: Option<Option<NameMatch>> = self.expr.eval(
// score evaluation
|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],
})
}
_ => 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
})
}
pub fn search_content(
&self,
candidate: &Path,
desired_len: usize, // available space for content match display
) -> Option<ContentMatch> {
use PatternOperator::*;
let composite_result: Option<Option<ContentMatch>> = self.expr.eval(
// score evaluation
|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,
})
}
_ => None,
},
|op, a| match (op, a) {
(Or, Some(_)) => true,
(And, None) => true,
_ => false,
},
);
composite_result.unwrap_or_else(|| {
warn!("unexpectedly missing result ");
None
})
}
/// Search for a string, trying to return a match
///
/// It's used when a composite returns something but may be matching also on other parts
/// that we can't compute, like the content.
///
/// This function isn't called on all potential candidates, only on those that we want
/// to show so we're allowed to do more costly operations here.
pub fn find_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::*;
let composite_result: Option<Option<NameMatch>> = self.expr.eval(
// score evaluation
|pat| pat.search_string(candidate),
// operator
|op, a, b| match (op, a, b) {
(Not, Some(_), _) => None,
(Or | And, Some(ma), Some(Some(mb))) => {
Some(ma.merge_with(mb))
},
(_, Some(ma), _) => Some(ma),
(_, None, Some(omb)) => omb,
_ => None,
},
// short-circuit: we don't short circuit on 'or' because we want to use
// both matches.
|_op, _a| false,
);
// it's possible we didn't find a result because the composition matches
// on non name parts
composite_result.unwrap_or(None)
}
/// Search for a string in content, trying to return a match as soon as some
/// part of the composite matches
pub fn find_content(
&self,
candidate: &Path,
desired_len: usize, // available space for content match display
) -> Option<ContentMatch> {
use PatternOperator::*;
let composite_result: Option<Option<ContentMatch>> = self.expr.eval(
// score evaluation
|pat| pat.search_content(candidate, desired_len),
// operator
|op, a, b| match (op, a, b) {
(Not, Some(_), _) => None,
(_, Some(ma), _) => Some(ma),
(_, None, Some(omb)) => omb,
_ => None,
},
|op, a| match (op, a) {
(Or, Some(_)) => true,
_ => false,
},
);
// it's possible we didn't find a result because the composition matches
// on non content parts
composite_result.unwrap_or(None)
}
pub fn get_match_line_count(
&self,
candidate: &Path,
) -> Option<usize> {
use PatternOperator::*;
let composite_result: Option<Option<usize>> = self.expr.eval(
// score evaluation
|pat| pat.get_match_line_count(candidate),
// operator
|op, a, b| match (op, a, b) {
(Not, Some(_), _) => None,
(_, Some(ma), _) => Some(ma),
(_, None, Some(omb)) => omb,
_ => None,
},
|op, a| match (op, a) {
(Or, Some(_)) => true,
_ => false,
},
);
composite_result.unwrap_or(None)
}
pub fn has_real_scores(&self) -> bool {
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());
!is_not_empty
}
}