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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
use std::ops::Range;
use egui::{Color32, NumExt as _, Widget as _};
use itertools::Itertools;
use smallvec::SmallVec;
use crate::{list_item, UiExt as _};
/// State for the filter widget when it is toggled on.
#[derive(Debug, Clone)]
struct InnerState {
/// The filter query string.
filter_query: String,
/// This ID is recreated every time the filter is toggled and tracks the current filtering
/// session.
///
/// This can be useful for client code to store session-specific state (e.g., the state of tree
/// collapsed-ness).
session_id: egui::Id,
}
impl Default for InnerState {
fn default() -> Self {
let mut random_bytes = [0u8; 8];
getrandom::getrandom(&mut random_bytes).expect("Couldn't get random bytes");
Self {
filter_query: String::new(),
// create a new session id each time the filter is toggled
session_id: egui::Id::new(random_bytes),
}
}
}
/// State and UI for the filter widget.
///
/// The filter widget is designed as a toggle between a title widget and the filter text field.
/// [`Self`] is responsible for storing the widget state as well as the query text typed by the
/// user. [`FilterMatcher`] performs the actual filtering.
#[derive(Debug, Clone, Default)]
pub struct FilterState {
/// The current state of the filter widget.
///
/// This is `None` when the filter is not active.
inner_state: Option<InnerState>,
/// Should the text field be focused?
///
/// Set to `true` upon clicking on the search button.
request_focus: bool,
}
impl FilterState {
/// Activate the filter.
///
/// This is the same as clicking the "loupe" icon button.
pub fn activate(&mut self, query: &str) {
self.inner_state = Some(InnerState {
filter_query: query.to_owned(),
..Default::default()
});
self.request_focus = true;
}
/// Is the filter currently active?
pub fn is_active(&self) -> bool {
self.inner_state.is_some()
}
/// Return the filter if any.
///
/// Returns `None` if the filter is disabled. Returns `Some(query)` if the filter is enabled
/// (even if the query string is empty, in which case it should match nothing).
pub fn query(&self) -> Option<&str> {
self.inner_state
.as_ref()
.map(|state| state.filter_query.as_str())
}
/// Return the current session ID of the filter widget, if active.
pub fn session_id(&self) -> Option<egui::Id> {
self.inner_state.as_ref().map(|state| state.session_id)
}
/// Return a filter matcher for the current query.
pub fn filter(&self) -> FilterMatcher {
FilterMatcher::new(self.query())
}
/// Display the filter widget.
///
/// Note: this uses [`egui::Ui::available_width`] to determine the location of the right-aligned
/// search button, as usual for [`list_item::ListItem`]-based widgets.
pub fn ui(
&mut self,
ui: &mut egui::Ui,
section_title: impl Into<egui::WidgetText>,
) -> Option<egui::Response> {
let mut button_clicked = false;
let icon = if self.inner_state.is_none() {
&crate::icons::SEARCH
} else {
&crate::icons::CLOSE
};
// precompute the title layout such that we know the size we need for the list item content
let section_title = section_title.into();
let galley = section_title.into_galley(
ui,
Some(egui::TextWrapMode::Extend),
f32::INFINITY,
egui::FontSelection::default(),
);
let text_width = galley.size().x;
let mut title_response = None;
list_item::list_item_scope(ui, ui.next_auto_id(), |ui| {
ui.list_item().interactive(false).show_flat(
ui,
list_item::CustomContent::new(|ui, _| {
if self.inner_state.is_some()
&& ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape))
{
self.inner_state = None;
}
if let Some(inner_state) = self.inner_state.as_mut() {
// we add additional spacing for aesthetic reasons (active text edits have a
// fat border)
ui.spacing_mut().text_edit_width =
(ui.max_rect().width() - 10.0).at_least(0.0);
// TODO(ab): ideally _all_ text edits would be styled this way, but we
// require egui support for that (https://github.com/emilk/egui/issues/3284)
ui.visuals_mut().widgets.hovered.expansion = 0.0;
ui.visuals_mut().widgets.active.expansion = 0.0;
ui.visuals_mut().widgets.open.expansion = 0.0;
ui.visuals_mut().widgets.active.fg_stroke.width = 1.0;
ui.visuals_mut().selection.stroke.width = 1.0;
let response = egui::TextEdit::singleline(&mut inner_state.filter_query)
.lock_focus(true)
.ui(ui);
if self.request_focus {
self.request_focus = false;
response.request_focus();
}
} else {
title_response = Some(ui.label(galley));
}
})
.with_content_width(text_width)
.action_button(icon, || {
button_clicked = true;
}),
);
});
// defer button handling because we can't mutably borrow `self` in both closures above
if button_clicked {
if self.inner_state.is_none() {
self.activate("");
} else {
self.inner_state = None;
}
}
title_response
}
}
// --
/// Full-text, case-insensitive matcher.
///
/// All keywords must match for the filter to match (`AND` semantics).
pub struct FilterMatcher {
/// Lowercase keywords.
///
/// If this is `None`, the filter is inactive and the matcher will accept everything. If this
/// is `Some([])`, the matcher will reject any input.
keywords: Option<Vec<Keyword>>,
}
impl FilterMatcher {
fn new(query: Option<&str>) -> Self {
Self {
keywords: query.map(|s| s.split_whitespace().map(Keyword::new).collect()),
}
}
/// Is the filter currently active?
pub fn is_active(&self) -> bool {
self.keywords.is_some()
}
/// Is the filter set to match everything?
///
/// Can be used by client code to short-circuit more expansive matching logic.
pub fn matches_everything(&self) -> bool {
self.keywords.is_none()
}
/// Is the filter set to match nothing?
///
/// Can be used by client code to short-circuit more expansive matching logic.
pub fn matches_nothing(&self) -> bool {
self.keywords
.as_ref()
.is_some_and(|keywords| keywords.is_empty())
}
/// Match a path and return the highlight ranges if any.
///
/// `None`: the filter is active, but the path didn't match the keyword
/// `Some(ranges)`: either the filter is inactive (i.e., it matches everything), or it is active
/// and all keywords matched at least once.
pub fn match_path<'a>(&self, path: impl IntoIterator<Item = &'a str>) -> Option<PathRanges> {
match self.keywords.as_deref() {
None => Some(PathRanges::default()),
Some([]) => None,
Some(keywords) => {
let path = path.into_iter().map(str::to_lowercase).collect_vec();
let all_ranges = keywords
.iter()
.map(|keyword| keyword.match_path(path.iter().map(String::as_str)))
.collect_vec();
// all keywords must match!
if all_ranges.iter().any(|ranges| ranges.is_empty()) {
None
} else {
let mut result = PathRanges::default();
for ranges in all_ranges {
result.merge(ranges);
}
Some(result)
}
}
}
}
}
/// A single keyword from a query.
///
/// ## Semantics
///
/// If the keyword has a single part, it can match anywhere in any part of the tested path, unless
/// `match_from_first_part_start` and/or `match_to_last_part_end`, which have the same behavior as
/// regex's `^` and `$`.
///
/// If the keyword has multiple parts, e.g. "first/second", the tested path must have at least one instance of contiguous
/// parts which match the corresponding keyword parts. In that context, the keyword parts have the
/// following behavior:
/// - First keyword part: `^part$` if `match_from_first_part_start`, `part$` otherwise
/// - Last keyword part: `^part$` if `match_to_last_part_end`, `^part` otherwise
/// - Other keyword parts: `^part$`
#[derive(Debug, Clone, PartialEq)]
struct Keyword {
/// The parts of a keyword.
///
/// To match, a path needs to have some contiguous parts which each match the corresponding
/// keyword parts.
parts: Vec<String>,
match_from_first_part_start: bool,
match_to_last_part_end: bool,
}
impl Keyword {
/// Create a [`Self`] based on a keyword string.
///
/// The string must not contain any whitespace!
fn new(mut keyword: &str) -> Self {
// Invariant: keywords are not empty
debug_assert!(!keyword.is_empty());
debug_assert!(!keyword.contains(char::is_whitespace));
let match_from_first_part_start = if let Some(k) = keyword.strip_prefix('/') {
keyword = k;
true
} else {
false
};
let match_to_last_part_end = if let Some(k) = keyword.strip_suffix('/') {
keyword = k;
true
} else {
false
};
let parts = keyword.split('/').map(str::to_lowercase).collect();
Self {
parts,
match_from_first_part_start,
match_to_last_part_end,
}
}
/// Match the keyword against the provided path.
///
/// An empty [`PathRanges`] means that the keyword didn't match the path.
///
/// Implementation notes:
/// - This function is akin to a "sliding window" of the keyword parts against the path parts,
/// trying to find some "alignment" yielding a match.
/// - We must be thorough as we want to find _all_ match highlights (i.e., we don't early out as
/// soon as we find a match).
fn match_path<'a>(&self, lowercase_path: impl ExactSizeIterator<Item = &'a str>) -> PathRanges {
let mut state_machines = vec![];
let path_length = lowercase_path.len();
for (path_part_index, path_part) in lowercase_path.into_iter().enumerate() {
// Only start a new state machine if it has a chance to be matched entirely.
if self.parts.len() <= (path_length - path_part_index) {
state_machines.push(MatchStateMachine::new(self));
}
for state_machine in &mut state_machines {
state_machine.process_next_path_part(path_part, path_part_index);
}
}
state_machines
.into_iter()
.filter_map(|state_machine| {
if state_machine.did_match() {
Some(state_machine.ranges)
} else {
None
}
})
.fold(PathRanges::default(), |mut acc, ranges| {
acc.merge(ranges);
acc
})
}
}
/// Accumulates highlight ranges for the various parts of a path.
///
/// The ranges are accumulated and stored unmerged and unordered, but are _always_ ordered and
/// merged when read, which only happens with [`Self::remove`].
#[derive(Debug, Default)]
pub struct PathRanges {
ranges: ahash::HashMap<usize, Vec<Range<usize>>>,
}
impl PathRanges {
/// Merge another [`Self`].
pub fn merge(&mut self, other: Self) {
for (part_index, part_ranges) in other.ranges {
self.ranges
.entry(part_index)
.or_default()
.extend(part_ranges);
}
}
/// Add ranges to a given part index.
pub fn extend(&mut self, part_index: usize, ranges: impl IntoIterator<Item = Range<usize>>) {
self.ranges.entry(part_index).or_default().extend(ranges);
}
/// Add a single range to a given part index.
pub fn push(&mut self, part_index: usize, range: Range<usize>) {
self.ranges.entry(part_index).or_default().push(range);
}
/// Remove the ranges for the given part and (if any) return them sorted and merged.
pub fn remove(&mut self, part_index: usize) -> Option<impl Iterator<Item = Range<usize>>> {
self.ranges.remove(&part_index).map(MergeRanges::new)
}
pub fn is_empty(&self) -> bool {
self.ranges.is_empty()
}
pub fn clear(&mut self) {
self.ranges.clear();
}
/// Convert to a `Vec` based structure.
#[cfg(test)]
fn into_vec(mut self, length: usize) -> Vec<Vec<Range<usize>>> {
let result = (0..length)
.map(|i| {
self.remove(i)
.map(|iter| iter.collect_vec())
.unwrap_or_default()
})
.collect();
debug_assert!(self.is_empty());
result
}
}
// ---
#[derive(Debug)]
enum MatchState {
InProgress,
Match,
NoMatch,
}
/// State machine used to test a given keyword against a given sequence of path parts.
#[derive(Debug)]
struct MatchStateMachine<'a> {
/// The keyword we're matching with.
keyword: &'a Keyword,
/// Which part of the keyword are we currently matching?
current_keyword_part: usize,
/// Our current state.
state: MatchState,
/// The highlight ranges we've gathered so far.
ranges: PathRanges,
}
impl<'a> MatchStateMachine<'a> {
fn new(keyword: &'a Keyword) -> Self {
Self {
keyword,
current_keyword_part: 0,
state: MatchState::InProgress,
ranges: Default::default(),
}
}
fn did_match(&self) -> bool {
matches!(self.state, MatchState::Match)
}
fn process_next_path_part(&mut self, part: &str, part_index: usize) {
if matches!(self.state, MatchState::Match | MatchState::NoMatch) {
return;
}
let keyword_part = &self.keyword.parts[self.current_keyword_part];
let has_part_after = self.current_keyword_part < self.keyword.parts.len() - 1;
let has_part_before = 0 < self.current_keyword_part;
let must_match_from_start = has_part_before || self.keyword.match_from_first_part_start;
let must_match_to_end = has_part_after || self.keyword.match_to_last_part_end;
let mut ranges = SmallVec::<[Range<usize>; 2]>::new();
match (must_match_from_start, must_match_to_end) {
(false, false) => {
ranges.extend(single_keyword_matches(part, keyword_part));
}
(true, false) => {
if part.starts_with(keyword_part) {
ranges.push(0..keyword_part.len());
}
}
(false, true) => {
if part.ends_with(keyword_part) {
ranges.push(part.len() - keyword_part.len()..part.len());
}
}
(true, true) => {
if part == keyword_part {
ranges.push(0..part.len());
}
}
}
if ranges.is_empty() {
self.state = MatchState::NoMatch;
} else {
self.ranges.extend(part_index, ranges);
self.current_keyword_part += 1;
}
if self.current_keyword_part == self.keyword.parts.len() {
self.state = MatchState::Match;
}
}
}
// ---
/// Given a list of highlight sections defined by start/end indices and a string, produce a properly
/// highlighted [`egui::WidgetText`].
pub fn format_matching_text(
ctx: &egui::Context,
text: &str,
match_iter: impl Iterator<Item = Range<usize>>,
text_color: Option<egui::Color32>,
) -> egui::WidgetText {
let mut current = 0;
let mut job = egui::text::LayoutJob::default();
let color = text_color.unwrap_or(Color32::PLACEHOLDER);
for Range { start, end } in match_iter {
if current < start {
job.append(
&text[current..start],
0.0,
egui::TextFormat {
font_id: egui::TextStyle::Body.resolve(&ctx.style()),
color,
..Default::default()
},
);
}
job.append(
&text[start..end],
0.0,
egui::TextFormat {
font_id: egui::TextStyle::Body.resolve(&ctx.style()),
color,
background: ctx.style().visuals.selection.bg_fill,
..Default::default()
},
);
current = end;
}
if current < text.len() {
job.append(
&text[current..],
0.0,
egui::TextFormat {
font_id: egui::TextStyle::Body.resolve(&ctx.style()),
color,
..Default::default()
},
);
}
job.into()
}
/// Helper function to extract all matches of a given keyword in the given text.
fn single_keyword_matches<'a>(
lower_case_text: &'a str,
keyword: &'a str,
) -> impl Iterator<Item = Range<usize>> + 'a {
let keyword_len = keyword.len();
let mut start = 0;
std::iter::from_fn(move || {
let index = lower_case_text[start..].find(keyword)?;
let start_index = start + index;
start = start_index + keyword_len;
Some(start_index..(start_index + keyword_len))
})
}
// ---
/// Given a vector of ranges, iterate over the sorted, merged ranges.
struct MergeRanges {
ranges: Vec<Range<usize>>,
current: Option<Range<usize>>,
}
impl MergeRanges {
fn new(mut ranges: Vec<Range<usize>>) -> Self {
ranges.sort_by_key(|r| usize::MAX - r.start);
let current = ranges.pop();
Self { ranges, current }
}
}
impl Iterator for MergeRanges {
type Item = Range<usize>;
fn next(&mut self) -> Option<Self::Item> {
let mut current = self.current.take()?;
while let Some(next) = self.ranges.pop() {
if next.start <= current.end {
current.end = current.end.max(next.end);
} else {
self.current = Some(next);
return Some(current);
}
}
Some(current)
}
}
#[cfg(test)]
mod test {
#![expect(clippy::single_range_in_vec_init)]
use super::*;
#[test]
fn test_merge_range() {
// merge to one
assert_eq!(MergeRanges::new(vec![0..10, 5..15]).collect_vec(), [0..15]);
assert_eq!(MergeRanges::new(vec![5..15, 0..10]).collect_vec(), [0..15]);
assert_eq!(
MergeRanges::new(vec![0..4, 3..4, 1..2, 5..15, 5..15, 0..10]).collect_vec(),
[0..15]
);
assert_eq!(MergeRanges::new(vec![0..11, 11..15]).collect_vec(), [0..15]);
// independent
assert_eq!(
MergeRanges::new(vec![0..5, 11..15]).collect_vec(),
[0..5, 11..15]
);
assert_eq!(
MergeRanges::new(vec![11..15, 0..5]).collect_vec(),
[0..5, 11..15]
);
// mixed
assert_eq!(
MergeRanges::new(vec![0..5, 20..30, 3..6, 25..27, 30..35]).collect_vec(),
[0..6, 20..35]
);
}
#[test]
fn test_keyword() {
assert_eq!(
Keyword::new("a"),
Keyword {
parts: vec!["a".into()],
match_from_first_part_start: false,
match_to_last_part_end: false
}
);
assert_eq!(
Keyword::new("/a"),
Keyword {
parts: vec!["a".into()],
match_from_first_part_start: true,
match_to_last_part_end: false
}
);
assert_eq!(
Keyword::new("a/"),
Keyword {
parts: vec!["a".into()],
match_from_first_part_start: false,
match_to_last_part_end: true
}
);
assert_eq!(
Keyword::new("/a/"),
Keyword {
parts: vec!["a".into()],
match_from_first_part_start: true,
match_to_last_part_end: true
}
);
assert_eq!(
Keyword::new("a/b"),
Keyword {
parts: vec!["a".into(), "b".into()],
match_from_first_part_start: false,
match_to_last_part_end: false
}
);
assert_eq!(
Keyword::new("a/b/"),
Keyword {
parts: vec!["a".into(), "b".into()],
match_from_first_part_start: false,
match_to_last_part_end: true
}
);
assert_eq!(
Keyword::new("/a/b/c/d"),
Keyword {
parts: vec!["a".into(), "b".into(), "c".into(), "d".into()],
match_from_first_part_start: true,
match_to_last_part_end: false
}
);
}
#[test]
fn test_keyword_match_path() {
fn match_and_normalize(query: &str, lowercase_path: &[&str]) -> Vec<Vec<Range<usize>>> {
Keyword::new(query)
.match_path(lowercase_path.iter().copied())
.into_vec(lowercase_path.len())
}
assert_eq!(match_and_normalize("a", &["a"]), vec![vec![0..1]]);
assert_eq!(match_and_normalize("a", &["aaa"]), vec![vec![0..3]]);
assert_eq!(
match_and_normalize("a/", &["aaa", "aaa"]),
vec![vec![2..3], vec![2..3]]
);
assert_eq!(
match_and_normalize("/a", &["aaa", "aaa"]),
vec![vec![0..1], vec![0..1]]
);
assert_eq!(
match_and_normalize("/a", &["aaa", "bbb"]),
vec![vec![0..1], vec![]]
);
assert_eq!(
match_and_normalize("a/b", &["aaa", "bbb"]),
vec![vec![2..3], vec![0..1]]
);
assert_eq!(
match_and_normalize("a/b/c", &["aaa", "b", "cccc"]),
vec![vec![2..3], vec![0..1], vec![0..1]]
);
assert!(
match_and_normalize("/a/b/c", &["aaa", "b", "cccc"])
.into_iter()
.flatten()
.count()
== 0,
);
assert!(
match_and_normalize("a/b/c/", &["aaa", "b", "cccc"])
.into_iter()
.flatten()
.count()
== 0,
);
assert_eq!(
match_and_normalize("ab/cd", &["xxxab", "cdab", "cdxxx"]),
vec![vec![3..5], vec![0..4], vec![0..2]]
);
assert_eq!(
match_and_normalize("ab/ab", &["xxxab", "ab", "abxxx"]),
vec![vec![3..5], vec![0..2], vec![0..2]]
);
}
#[test]
fn test_match_path() {
fn match_and_normalize(query: &str, path: &[&str]) -> Option<Vec<Vec<Range<usize>>>> {
FilterMatcher::new(Some(query))
.match_path(path.iter().copied())
.map(|ranges| ranges.into_vec(path.len()))
}
assert_eq!(
match_and_normalize("ab/cd", &["xxxAb", "cDaB", "Cdxxx"]),
Some(vec![vec![3..5], vec![0..4], vec![0..2]])
);
assert_eq!(
match_and_normalize("ab/cd xx/", &["xxxAb", "cDaB", "Cdxxx"]),
Some(vec![vec![3..5], vec![0..4], vec![0..2, 3..5]])
);
assert_eq!(
match_and_normalize("ab/cd bla", &["xxxAb", "cDaB", "Cdxxx"]),
None
);
}
}