tui-lipan 0.2.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
Documentation
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
use std::sync::Arc;

#[cfg(not(target_arch = "wasm32"))]
use nucleo::Utf32String;
use nucleo::pattern::{CaseMatching, Normalization};

use super::{SearchItem, SearchMatchMode};
#[cfg(not(target_arch = "wasm32"))]
use crate::utils::nucleo::{MatchMode, NucleoMatcher};

#[derive(Clone, Debug, Default)]
pub(super) struct SearchResult {
    pub(super) item_index: usize,
    pub(super) score: u32,
    pub(super) label_hits: Vec<u32>,
    pub(super) description_hits: Vec<u32>,
    pub(super) description_right_hits: Vec<u32>,
}

#[derive(Clone, Debug)]
pub(super) struct SearchEntry {
    label: Arc<str>,
    description: Option<Arc<str>>,
    description_right: Option<Arc<str>>,
    aliases: Vec<Arc<str>>,
}

/// True when the hit indices form a contiguous run, meaning the query matched
/// the haystack as an exact substring. Nucleo's default scoring under-rewards
/// substring matches relative to scattered prefix matches; callers add a flat
/// boost when this returns true so an exact substring outranks a fuzzy one.
fn is_contiguous_run(hits: &[u32]) -> bool {
    hits.len() >= 2 && hits.windows(2).all(|w| w[1] == w[0] + 1)
}

pub(super) fn build_search_entries<T>(items: &[SearchItem<T>]) -> Vec<SearchEntry> {
    items
        .iter()
        .map(|item| SearchEntry {
            label: item.label.clone(),
            description: item.description.as_ref().and_then(|d| d.left.clone()),
            description_right: item.description.as_ref().and_then(|d| d.right.clone()),
            aliases: item.aliases.clone(),
        })
        .collect()
}

pub(super) fn all_item_results(len: usize) -> Vec<SearchResult> {
    (0..len)
        .map(|index| SearchResult {
            item_index: index,
            score: 0,
            label_hits: Vec::new(),
            description_hits: Vec::new(),
            description_right_hits: Vec::new(),
        })
        .collect()
}

pub(super) fn match_items(
    items: &[SearchEntry],
    query: &str,
    mode: SearchMatchMode,
    case_matching: CaseMatching,
    normalization: Normalization,
) -> Vec<SearchResult> {
    let query = query.trim();
    if query.is_empty() {
        return all_item_results(items.len());
    }

    match mode {
        SearchMatchMode::Fuzzy => match_items_fuzzy(items, query, case_matching, normalization),
        SearchMatchMode::Hybrid => hybrid::match_items_hybrid(items, query, case_matching),
    }
}

fn match_items_fuzzy(
    items: &[SearchEntry],
    query: &str,
    case_matching: CaseMatching,
    normalization: Normalization,
) -> Vec<SearchResult> {
    #[cfg(target_arch = "wasm32")]
    {
        return match_items_wasm_fallback(items, query, case_matching, normalization);
    }

    #[cfg(not(target_arch = "wasm32"))]
    {
        let mut matcher = NucleoMatcher::default();
        let mode = MatchMode::Fuzzy;

        let mut results = Vec::new();
        for (index, item) in items.iter().enumerate() {
            let mut label_hits = Vec::new();
            let mut desc_hits = Vec::new();
            let mut desc_right_hits = Vec::new();

            let label_utf32 = Utf32String::from(item.label.as_ref());
            let label_score = matcher.match_indices(
                &label_utf32,
                query,
                mode,
                case_matching,
                normalization,
                &mut label_hits,
            );

            let mut score = label_score.unwrap_or(0);
            let mut matched = label_score.is_some();
            let label_matched = matched;

            if matched && is_contiguous_run(&label_hits) {
                score = score.saturating_add(score / 2);
            }

            // Aliases compete with the label via max(), but label-matched rows
            // keep a large bonus so synonym-only hits cannot outrank them.
            for alias in &item.aliases {
                let alias_utf32 = Utf32String::from(alias.as_ref());
                let mut alias_hits = Vec::new();
                if let Some(mut alias_score) = matcher.match_indices(
                    &alias_utf32,
                    query,
                    mode,
                    case_matching,
                    normalization,
                    &mut alias_hits,
                ) {
                    if is_contiguous_run(&alias_hits) {
                        alias_score = alias_score.saturating_add(alias_score / 2);
                    }
                    if !label_matched && alias_score > score {
                        score = alias_score;
                    }
                    matched = true;
                }
            }

            if label_matched {
                // Keep label rows above synonym-only rows regardless of nucleo magnitude.
                score = score.saturating_add(1 << 28);
            }

            if let Some(desc) = &item.description {
                let desc_utf32 = Utf32String::from(desc.as_ref());
                if let Some(desc_score) = matcher.match_indices(
                    &desc_utf32,
                    query,
                    mode,
                    case_matching,
                    normalization,
                    &mut desc_hits,
                ) {
                    score = score.saturating_add(desc_score);
                    matched = true;
                }
            }

            if let Some(right) = &item.description_right {
                let right_utf32 = Utf32String::from(right.as_ref());
                if let Some(right_score) = matcher.match_indices(
                    &right_utf32,
                    query,
                    mode,
                    case_matching,
                    normalization,
                    &mut desc_right_hits,
                ) {
                    score = score.saturating_add(right_score);
                    matched = true;
                }
            }

            if matched {
                label_hits.sort_unstable();
                label_hits.dedup();
                desc_hits.sort_unstable();
                desc_hits.dedup();
                desc_right_hits.sort_unstable();
                desc_right_hits.dedup();

                results.push(SearchResult {
                    item_index: index,
                    score,
                    label_hits,
                    description_hits: desc_hits,
                    description_right_hits: desc_right_hits,
                });
            }
        }

        results.sort_by(|a, b| b.score.cmp(&a.score).then(a.item_index.cmp(&b.item_index)));
        results
    }
}

/// Hybrid matching: exact/prefix/word-prefix/substring/fuzzy tiers evaluated
/// together, per field, so a real substring/prefix match always outranks a
/// fuzzy one and weak scattered fuzzy matches are rejected.
mod hybrid {
    use nucleo::pattern::CaseMatching;

    use super::{SearchEntry, SearchResult};

    /// Priority tier a field match falls into. Ordered so that `Ord`
    /// comparison (and the numeric `rank`) directly encodes the required
    /// priority: Exact > Prefix > WordPrefix > Substring > Fuzzy.
    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
    enum MatchTier {
        Fuzzy,
        Substring,
        WordPrefix,
        Prefix,
        Exact,
    }

    impl MatchTier {
        fn rank(self) -> u32 {
            match self {
                MatchTier::Fuzzy => 0,
                MatchTier::Substring => 1,
                MatchTier::WordPrefix => 2,
                MatchTier::Prefix => 3,
                MatchTier::Exact => 4,
            }
        }
    }

    /// Which searchable field a candidate haystack belongs to. Drives both
    /// the field weight and which tiers are attempted: keybinding-style hints
    /// only use exact/substring matching.
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum FieldRole {
        /// Canonical item label: highest weight among primary fields.
        Label,
        /// Hidden synonyms. Same tier ladder as labels; label hits carry a
        /// bonus so any visible-label match outranks a synonym-only hit.
        Alias,
        /// Description text.
        Description,
        /// Right-hand hint (e.g. a keybinding). Exact/substring only.
        Hint,
    }

    impl FieldRole {
        fn weight(self) -> f64 {
            match self {
                FieldRole::Label => 3.0,
                FieldRole::Alias => 2.0,
                FieldRole::Description => 1.0,
                FieldRole::Hint => 1.0,
            }
        }

        fn allow_word_prefix_and_fuzzy(self) -> bool {
            !matches!(self, FieldRole::Hint)
        }
    }

    /// A single field's match result: the tier it was accepted at, a
    /// within-tier quality in roughly `0.0..=1.2`, and the matched character
    /// indices (used for highlighting).
    struct FieldMatch {
        tier: MatchTier,
        quality: f64,
        hits: Vec<u32>,
    }

    struct DistributedMatch {
        score: f64,
        label_hits: Vec<u32>,
        description_hits: Vec<u32>,
        description_right_hits: Vec<u32>,
    }

    /// Tier contribution dwarfs weight, which in turn dwarfs quality, so
    /// match-type priority always wins, field weight is the tie-breaker
    /// across tiers-equal fields, and quality only fine-tunes within that.
    const TIER_UNIT: f64 = 1_000_000.0;
    const WEIGHT_UNIT: f64 = 1_000.0;
    const QUALITY_UNIT: f64 = 500.0;

    /// Minimum composite quality (density, span, start position, and
    /// word-boundary cohesion) a fuzzy candidate must clear to be accepted.
    /// Tuned so that abbreviation-style matches like `prd` -> `production`
    /// pass while sparse, multi-word matches like `layo` -> `Enable pane
    /// synchronization` are rejected.
    const FUZZY_QUALITY_THRESHOLD: f64 = 0.45;

    /// Added to every label hit so a visible-label match always outranks a
    /// synonym-only alias hit, regardless of tier. Larger than the Exact→Fuzzy
    /// tier span (`4 * TIER_UNIT`) plus weight/quality headroom.
    const LABEL_MATCH_BONUS: f64 = 10.0 * TIER_UNIT;

    fn field_total(tier: MatchTier, quality: f64, role: FieldRole) -> f64 {
        tier.rank() as f64 * TIER_UNIT
            + role.weight() * WEIGHT_UNIT
            + quality.clamp(0.0, 1.5) * QUALITY_UNIT
    }

    fn respects_case(case_matching: CaseMatching, query_chars: &[char]) -> bool {
        match case_matching {
            CaseMatching::Respect => true,
            CaseMatching::Ignore => false,
            CaseMatching::Smart => query_chars.iter().any(|ch| ch.is_uppercase()),
            _ => query_chars.iter().any(|ch| ch.is_uppercase()),
        }
    }

    fn fold_char(c: char, respect_case: bool) -> char {
        if respect_case {
            c
        } else {
            c.to_lowercase().next().unwrap_or(c)
        }
    }

    /// Splits `chars` into `[start, end)` ranges of contiguous alphanumeric
    /// runs, treating any other character (space, `-`, `_`, `/`, punctuation)
    /// as a word boundary.
    fn split_words(chars: &[char]) -> Vec<(usize, usize)> {
        let mut words = Vec::new();
        let mut start = None;
        for (i, c) in chars.iter().enumerate() {
            if c.is_alphanumeric() {
                if start.is_none() {
                    start = Some(i);
                }
            } else if let Some(s) = start.take() {
                words.push((s, i));
            }
        }
        if let Some(s) = start {
            words.push((s, chars.len()));
        }
        words
    }

    /// Finds the first word whose prefix matches `query`, returning
    /// `(word_start, word_len, word_index, word_count)`.
    fn find_word_prefix(hay: &[char], query: &[char]) -> Option<(usize, usize, usize, usize)> {
        let words = split_words(hay);
        let count = words.len();
        for (idx, &(s, e)) in words.iter().enumerate() {
            let word_len = e - s;
            if word_len >= query.len() && hay[s..s + query.len()] == query[..] {
                return Some((s, word_len, idx, count));
            }
        }
        None
    }

    /// Finds the first contiguous occurrence of `query` anywhere in `hay`.
    fn find_contiguous(hay: &[char], query: &[char]) -> Option<usize> {
        if query.is_empty() || hay.len() < query.len() {
            return None;
        }
        (0..=hay.len() - query.len()).find(|&i| hay[i..i + query.len()] == query[..])
    }

    /// Finds a contiguous query after removing non-alphanumeric separators
    /// from the field, while retaining original indices for highlighting.
    fn find_separator_insensitive(
        hay: &[char],
        query: &[char],
    ) -> Option<(usize, usize, Vec<u32>)> {
        if query.is_empty() || query.iter().any(|c| !c.is_alphanumeric()) {
            return None;
        }

        let compact: Vec<(usize, char)> = hay
            .iter()
            .copied()
            .enumerate()
            .filter(|(_, c)| c.is_alphanumeric())
            .collect();
        if compact.len() == hay.len() {
            return None;
        }

        let compact_chars: Vec<char> = compact.iter().map(|(_, c)| *c).collect();
        let start = find_contiguous(&compact_chars, query)?;
        let hits = compact[start..start + query.len()]
            .iter()
            .map(|(original_index, _)| *original_index as u32)
            .collect();
        Some((start, compact.len(), hits))
    }

    /// Fraction of `hits` that fall within the single word most of them land
    /// in. `1.0` when every hit stays inside one word; lower as hits spread
    /// across more words.
    fn word_fraction_for_hits(hits: &[u32], words: &[(usize, usize)]) -> f64 {
        if hits.is_empty() || words.is_empty() {
            return 0.0;
        }
        let mut counts = vec![0usize; words.len()];
        for &h in hits {
            let h = h as usize;
            if let Some(word_idx) = words.iter().position(|&(s, e)| h >= s && h < e) {
                counts[word_idx] += 1;
            }
        }
        let max_count = counts.into_iter().max().unwrap_or(0);
        max_count as f64 / hits.len() as f64
    }

    /// Composite quality score (`0.0..~1.2`) for a fuzzy match: rewards tight
    /// density and short spans, an early start, a decent matcher score, and
    /// staying mostly within a single word; penalizes the opposite.
    fn fuzzy_quality(matcher_score: u32, hits: &[u32], query_len: usize, hay: &[char]) -> f64 {
        let Some(&first) = hits.first() else {
            return 0.0;
        };
        let last = *hits.last().unwrap();
        let span = (last - first + 1) as f64;
        let density = query_len as f64 / span;
        let start_score = 1.0 / (1.0 + first as f64 / 10.0);
        let span_score = 1.0 / (1.0 + (span - query_len as f64).max(0.0) / 8.0);
        let matcher_norm = (matcher_score as f64 / (query_len.max(1) as f64 * 48.0)).min(1.2);

        let words = split_words(hay);
        let word_fraction = word_fraction_for_hits(hits, &words);

        let base = 0.30 * density + 0.25 * span_score + 0.20 * start_score + 0.25 * matcher_norm;
        base * word_fraction.clamp(0.35, 1.0).powf(1.4)
    }

    /// Fuzzy-tier backend, unified behind a single type so `classify_field`
    /// has one signature on every target: `nucleo`'s matcher natively, or a
    /// zero-sized marker on wasm32 (which instead calls the subsequence
    /// fallback used by [`super::match_items_fuzzy`]).
    #[cfg(not(target_arch = "wasm32"))]
    type FuzzyMatcher = super::NucleoMatcher;
    #[cfg(target_arch = "wasm32")]
    type FuzzyMatcher = ();

    #[cfg(not(target_arch = "wasm32"))]
    fn fuzzy_field_hits(
        haystack: &str,
        query: &str,
        case_matching: CaseMatching,
        matcher: &mut FuzzyMatcher,
    ) -> Option<(u32, Vec<u32>)> {
        use nucleo::Utf32String;
        use nucleo::pattern::Normalization;

        let haystack_utf32 = Utf32String::from(haystack);
        let mut hits = Vec::new();
        let score = matcher.match_indices(
            &haystack_utf32,
            query,
            super::MatchMode::Fuzzy,
            case_matching,
            Normalization::Never,
            &mut hits,
        )?;
        hits.sort_unstable();
        hits.dedup();
        Some((score, hits))
    }

    #[cfg(target_arch = "wasm32")]
    fn fuzzy_field_hits(
        haystack: &str,
        query: &str,
        case_matching: CaseMatching,
        _matcher: &mut FuzzyMatcher,
    ) -> Option<(u32, Vec<u32>)> {
        super::simple_match(haystack, query, case_matching)
    }

    /// Classifies a single field independently against `query`, trying tiers
    /// in priority order and stopping at the first that matches (so a real
    /// prefix never falls through to a weaker fuzzy score). Exact/prefix/
    /// word-prefix/substring are plain character comparisons; only the fuzzy
    /// tier delegates to the shared matcher (`nucleo` natively, or the
    /// wasm32 subsequence fallback), and is quality-gated so weak scattered
    /// matches are rejected outright.
    fn classify_field(
        haystack: &str,
        query: &str,
        case_matching: CaseMatching,
        role: FieldRole,
        matcher: &mut FuzzyMatcher,
    ) -> Option<FieldMatch> {
        if haystack.is_empty() {
            return None;
        }

        let hay_chars: Vec<char> = haystack.chars().collect();
        let query_chars: Vec<char> = query.chars().collect();
        if query_chars.is_empty() {
            return None;
        }
        let respect_case = respects_case(case_matching, &query_chars);
        let hay_folded: Vec<char> = hay_chars
            .iter()
            .map(|&c| fold_char(c, respect_case))
            .collect();
        let query_folded: Vec<char> = query_chars
            .iter()
            .map(|&c| fold_char(c, respect_case))
            .collect();

        if hay_folded == query_folded {
            return Some(FieldMatch {
                tier: MatchTier::Exact,
                quality: 1.0,
                hits: (0..hay_chars.len() as u32).collect(),
            });
        }

        if hay_folded.len() > query_folded.len()
            && hay_folded[..query_folded.len()] == query_folded[..]
        {
            let hits: Vec<u32> = (0..query_chars.len() as u32).collect();
            let ratio = query_chars.len() as f64 / hay_chars.len() as f64;
            return Some(FieldMatch {
                tier: MatchTier::Prefix,
                quality: ratio.clamp(0.0, 1.0),
                hits,
            });
        }

        if role.allow_word_prefix_and_fuzzy()
            && let Some((word_start, word_len, word_index, word_count)) =
                find_word_prefix(&hay_folded, &query_folded)
        {
            let hits: Vec<u32> =
                (word_start as u32..(word_start + query_chars.len()) as u32).collect();
            let ratio = query_chars.len() as f64 / word_len as f64;
            let position_bonus = 1.0 - (word_index as f64 / word_count.max(1) as f64) * 0.3;
            let quality = (0.7 * ratio + 0.3 * position_bonus).clamp(0.0, 1.0);
            return Some(FieldMatch {
                tier: MatchTier::WordPrefix,
                quality,
                hits,
            });
        }

        if let Some(start) = find_contiguous(&hay_folded, &query_folded) {
            let hits: Vec<u32> = (start as u32..(start + query_chars.len()) as u32).collect();
            let start_score = 1.0 / (1.0 + start as f64 / 10.0);
            let compactness = query_chars.len() as f64 / hay_chars.len() as f64;
            let quality = (0.6 * start_score + 0.4 * compactness).clamp(0.0, 1.0);
            return Some(FieldMatch {
                tier: MatchTier::Substring,
                quality,
                hits,
            });
        }

        if let Some((start, compact_len, hits)) =
            find_separator_insensitive(&hay_folded, &query_folded)
        {
            let tier = if query_folded.len() == compact_len {
                MatchTier::Exact
            } else if start == 0 {
                MatchTier::Prefix
            } else {
                MatchTier::Substring
            };
            let quality = query_folded.len() as f64 / compact_len as f64;
            return Some(FieldMatch {
                tier,
                quality,
                hits,
            });
        }

        if role.allow_word_prefix_and_fuzzy()
            && let Some((score, hits)) = fuzzy_field_hits(haystack, query, case_matching, matcher)
        {
            let quality = fuzzy_quality(score, &hits, query_chars.len(), &hay_folded);
            if quality >= FUZZY_QUALITY_THRESHOLD {
                return Some(FieldMatch {
                    tier: MatchTier::Fuzzy,
                    quality,
                    hits,
                });
            }
        }

        None
    }

    /// Best field-total across label and aliases: aliases are never
    /// rendered, so they only compete for the score via `max()` and never
    /// contribute their own hits. Label hits receive [`LABEL_MATCH_BONUS`] so
    /// any visible-label match outranks a synonym-only alias hit.
    fn best_primary_total(
        item: &SearchEntry,
        query: &str,
        case_matching: CaseMatching,
        matcher: &mut FuzzyMatcher,
        label_match: &Option<FieldMatch>,
    ) -> Option<f64> {
        let label_total = label_match
            .as_ref()
            .map(|m| field_total(m.tier, m.quality, FieldRole::Label) + LABEL_MATCH_BONUS);
        let mut best_alias = None;
        for alias in &item.aliases {
            if let Some(m) = classify_field(
                alias.as_ref(),
                query,
                case_matching,
                FieldRole::Alias,
                matcher,
            ) {
                let total = field_total(m.tier, m.quality, FieldRole::Alias);
                if best_alias.is_none_or(|cur| total > cur) {
                    best_alias = Some(total);
                }
            }
        }
        match (label_total, best_alias) {
            (Some(label), Some(alias)) => Some(label.max(alias)),
            (Some(label), None) => Some(label),
            (None, Some(alias)) => Some(alias),
            (None, None) => None,
        }
    }

    fn match_terms_across_fields(
        item: &SearchEntry,
        query: &str,
        case_matching: CaseMatching,
        matcher: &mut FuzzyMatcher,
    ) -> Option<DistributedMatch> {
        let mut terms = query.split_whitespace();
        let first = terms.next()?;
        let second = terms.next()?;
        let mut score = 0.0;
        let mut term_count = 0usize;
        let mut label_hits = Vec::new();
        let mut description_hits = Vec::new();
        let mut hint_hits = Vec::new();

        for term in std::iter::once(first)
            .chain(std::iter::once(second))
            .chain(terms)
        {
            term_count += 1;
            let label_match = classify_field(
                item.label.as_ref(),
                term,
                case_matching,
                FieldRole::Label,
                matcher,
            );
            let best_primary = best_primary_total(item, term, case_matching, matcher, &label_match);
            let description_match = item.description.as_deref().and_then(|description| {
                classify_field(
                    description,
                    term,
                    case_matching,
                    FieldRole::Description,
                    matcher,
                )
            });
            let description_total = description_match
                .as_ref()
                .map(|m| field_total(m.tier, m.quality, FieldRole::Description));
            let hint_match = item.description_right.as_deref().and_then(|hint| {
                classify_field(hint, term, case_matching, FieldRole::Hint, matcher)
            });
            let hint_total = hint_match
                .as_ref()
                .map(|m| field_total(m.tier, m.quality, FieldRole::Hint));

            let term_score = [best_primary, description_total, hint_total]
                .into_iter()
                .flatten()
                .reduce(f64::max)?;
            score += term_score;

            if let Some(field_match) = label_match {
                label_hits.extend(field_match.hits);
            }
            if let Some(field_match) = description_match {
                description_hits.extend(field_match.hits);
            }
            if let Some(field_match) = hint_match {
                hint_hits.extend(field_match.hits);
            }
        }

        // Keep distributed matches on the same tier scale as whole-query matches. Summing lets
        // several weaker term matches outrank an exact phrase solely because the query has spaces.
        score /= term_count as f64;

        label_hits.sort_unstable();
        label_hits.dedup();
        description_hits.sort_unstable();
        description_hits.dedup();
        hint_hits.sort_unstable();
        hint_hits.dedup();

        Some(DistributedMatch {
            score,
            label_hits,
            description_hits,
            description_right_hits: hint_hits,
        })
    }

    pub(super) fn match_items_hybrid(
        items: &[SearchEntry],
        query: &str,
        case_matching: CaseMatching,
    ) -> Vec<SearchResult> {
        let mut matcher = FuzzyMatcher::default();

        let mut results = Vec::new();
        for (index, item) in items.iter().enumerate() {
            let label_match = classify_field(
                item.label.as_ref(),
                query,
                case_matching,
                FieldRole::Label,
                &mut matcher,
            );
            let label_hits = label_match
                .as_ref()
                .map(|m| m.hits.clone())
                .unwrap_or_default();
            let best_primary =
                best_primary_total(item, query, case_matching, &mut matcher, &label_match);

            let desc_match = item.description.as_deref().and_then(|desc| {
                classify_field(
                    desc,
                    query,
                    case_matching,
                    FieldRole::Description,
                    &mut matcher,
                )
            });
            let desc_hits = desc_match
                .as_ref()
                .map(|m| m.hits.clone())
                .unwrap_or_default();
            let desc_total =
                desc_match.map(|m| field_total(m.tier, m.quality, FieldRole::Description));

            let hint_match = item.description_right.as_deref().and_then(|hint| {
                classify_field(hint, query, case_matching, FieldRole::Hint, &mut matcher)
            });
            let hint_hits = hint_match
                .as_ref()
                .map(|m| m.hits.clone())
                .unwrap_or_default();
            let hint_total = hint_match.map(|m| field_total(m.tier, m.quality, FieldRole::Hint));

            let overall = [best_primary, desc_total, hint_total]
                .into_iter()
                .flatten()
                .fold(None, |acc: Option<f64>, v| {
                    Some(acc.map_or(v, |a| a.max(v)))
                });

            if let Some(score) = overall {
                results.push(SearchResult {
                    item_index: index,
                    score: score.round().clamp(0.0, u32::MAX as f64) as u32,
                    label_hits,
                    description_hits: desc_hits,
                    description_right_hits: hint_hits,
                });
            } else if let Some(distributed_match) =
                match_terms_across_fields(item, query, case_matching, &mut matcher)
            {
                results.push(SearchResult {
                    item_index: index,
                    score: distributed_match.score.round().clamp(0.0, u32::MAX as f64) as u32,
                    label_hits: distributed_match.label_hits,
                    description_hits: distributed_match.description_hits,
                    description_right_hits: distributed_match.description_right_hits,
                });
            }
        }

        results.sort_by(|a, b| b.score.cmp(&a.score).then(a.item_index.cmp(&b.item_index)));
        results
    }
}

#[cfg(test)]
mod tests {
    use nucleo::pattern::{CaseMatching, Normalization};

    use super::{SearchEntry, SearchItem, SearchMatchMode, all_item_results, match_items};

    fn entries(labels: &[&str]) -> Vec<SearchEntry> {
        let items: Vec<SearchItem<usize>> = labels
            .iter()
            .enumerate()
            .map(|(i, label)| SearchItem::new(*label, i))
            .collect();
        super::build_search_entries(&items)
    }

    fn hybrid_match(labels: &[&str], query: &str) -> Vec<super::SearchResult> {
        match_items(
            &entries(labels),
            query,
            SearchMatchMode::Hybrid,
            CaseMatching::Smart,
            Normalization::Smart,
        )
    }

    #[test]
    fn all_item_results_preserves_item_order_with_empty_hits() {
        let results = all_item_results(3);

        assert_eq!(results.len(), 3);
        assert_eq!(results[0].item_index, 0);
        assert_eq!(results[1].item_index, 1);
        assert_eq!(results[2].item_index, 2);
        assert!(results.iter().all(|result| result.score == 0));
        assert!(results.iter().all(|result| result.label_hits.is_empty()));
        assert!(
            results
                .iter()
                .all(|result| result.description_hits.is_empty())
        );
        assert!(
            results
                .iter()
                .all(|result| result.description_right_hits.is_empty())
        );
    }

    #[test]
    fn hybrid_substring_ranks_above_fuzzy() {
        // "concatenate" contains "cat" as a contiguous substring; "cabinet"
        // only matches "cat" via scattered fuzzy characters (c-a...-t).
        let results = hybrid_match(&["concatenate", "cabinet"], "cat");

        assert_eq!(results.len(), 2, "both items should match: {results:?}");
        assert_eq!(
            results[0].item_index, 0,
            "substring match must rank first: {results:?}"
        );
        assert_eq!(results[1].item_index, 1);
    }

    #[test]
    fn hybrid_rejects_weak_sparse_fuzzy_matches() {
        // "layo" only reaches "Enable pane synchronization" through four
        // scattered characters spread across three different words - too
        // weak to be a useful match.
        let results = hybrid_match(&["Enable pane synchronization"], "layo");

        assert!(
            results.is_empty(),
            "weak scattered fuzzy match should be rejected: {results:?}"
        );
    }

    #[test]
    fn hybrid_keeps_abbreviation_fuzzy_matches() {
        // "prd" -> "production" is a tight, single-word, early-starting
        // abbreviation and should still match.
        let results = hybrid_match(&["production"], "prd");

        assert_eq!(
            results.len(),
            1,
            "abbreviation match should pass: {results:?}"
        );
        assert_eq!(results[0].item_index, 0);
    }

    #[test]
    fn hybrid_matches_contiguous_query_across_label_separators() {
        for query in ["switchmod", "switchmodel"] {
            let results = hybrid_match(&["Switch model"], query);

            assert_eq!(results.len(), 1, "{query} should match: {results:?}");
            assert_eq!(results[0].item_index, 0);
            assert_eq!(results[0].label_hits.len(), query.len());
        }
    }

    #[test]
    fn hybrid_matches_cannot_span_multiple_fields() {
        let items = vec![
            SearchItem::new("fooa", 0).description("bc"),
            SearchItem::new("other", 1),
        ];
        let entries = super::build_search_entries(&items);

        // "abc" is not present in the label ("fooa") nor the description
        // ("bc") alone; it must not match by combining characters across
        // both fields.
        let results = match_items(
            &entries,
            "abc",
            SearchMatchMode::Hybrid,
            CaseMatching::Smart,
            Normalization::Smart,
        );

        assert!(
            results.is_empty(),
            "fields must not combine into a single match: {results:?}"
        );
    }

    #[test]
    fn hybrid_matches_separate_terms_across_label_and_description() {
        let items = vec![
            SearchItem::new("GPT-5.6 Sol", 0).description("OpenAI"),
            SearchItem::new("GPT-4.1", 1).description("OpenAI"),
            SearchItem::new("Claude 4.6", 2).description("Anthropic"),
        ];
        let entries = super::build_search_entries(&items);

        let results = match_items(
            &entries,
            "openai 5.6",
            SearchMatchMode::Hybrid,
            CaseMatching::Smart,
            Normalization::Smart,
        );

        assert_eq!(results.len(), 1, "all terms must match: {results:?}");
        assert_eq!(results[0].item_index, 0);
        assert!(!results[0].label_hits.is_empty());
        assert!(!results[0].description_hits.is_empty());
    }

    #[test]
    fn hybrid_exact_phrase_alias_outranks_distributed_terms() {
        let items = vec![
            SearchItem::new("Change appearance", 0).aliases(["focused background", "animations"]),
            SearchItem::new("Disable focus on hover", 1).aliases(["focus on hover"]),
        ];
        let entries = super::build_search_entries(&items);

        let results = match_items(
            &entries,
            "focus on",
            SearchMatchMode::Hybrid,
            CaseMatching::Smart,
            Normalization::Smart,
        );

        assert_eq!(results.len(), 2);
        assert_eq!(
            results[0].item_index, 1,
            "exact phrase should win: {results:?}"
        );
    }

    #[test]
    fn hybrid_label_match_outranks_same_query_alias() {
        // Exact alias "border" on a titlebar row must not beat a real label hit
        // on "Border mode" / "Focused pane border".
        let items = vec![
            SearchItem::new("Titlebar layout", 0).aliases(["border"]),
            SearchItem::new("Border mode", 1),
            SearchItem::new("Focused pane border", 2),
        ];
        let entries = super::build_search_entries(&items);

        let results = match_items(
            &entries,
            "border",
            SearchMatchMode::Hybrid,
            CaseMatching::Smart,
            Normalization::Smart,
        );

        assert_eq!(results.len(), 3, "{results:?}");
        assert_eq!(
            results[0].item_index, 1,
            "label prefix must outrank exact alias: {results:?}"
        );
        assert_eq!(
            results[1].item_index, 2,
            "label word-prefix must outrank exact alias: {results:?}"
        );
        assert_eq!(results[2].item_index, 0);
    }

    #[test]
    fn hybrid_prefix_ranks_above_inner_substring() {
        // "logger" starts with "log" (prefix); "catalog" only contains "log"
        // as an inner substring.
        let results = hybrid_match(&["logger", "catalog"], "log");

        assert_eq!(results.len(), 2);
        assert_eq!(
            results[0].item_index, 0,
            "prefix match must rank above inner substring: {results:?}"
        );
        assert_eq!(results[1].item_index, 1);
    }

    #[test]
    fn hybrid_empty_query_preserves_original_order() {
        let results = hybrid_match(&["zebra", "apple", "mango"], "");

        assert_eq!(
            results.iter().map(|r| r.item_index).collect::<Vec<_>>(),
            vec![0, 1, 2]
        );
        assert!(results.iter().all(|r| r.score == 0));
    }
}

#[cfg(target_arch = "wasm32")]
fn match_items_wasm_fallback(
    items: &[SearchEntry],
    query: &str,
    case_matching: CaseMatching,
    _normalization: Normalization,
) -> Vec<SearchResult> {
    let mut results = Vec::new();
    for (index, item) in items.iter().enumerate() {
        let (label_score, mut label_hits) =
            simple_match(item.label.as_ref(), query, case_matching).unwrap_or((0, Vec::new()));

        let mut score = label_score;
        let mut matched = !label_hits.is_empty();
        let label_matched = matched;

        if matched && is_contiguous_run(&label_hits) {
            score = score.saturating_add(score / 2);
        }

        for alias in &item.aliases {
            if let Some((mut alias_score, alias_hits)) =
                simple_match(alias.as_ref(), query, case_matching)
            {
                if is_contiguous_run(&alias_hits) {
                    alias_score = alias_score.saturating_add(alias_score / 2);
                }
                if !label_matched && alias_score > score {
                    score = alias_score;
                }
                matched = true;
            }
        }

        if label_matched {
            score = score.saturating_add(1 << 28);
        }

        let (desc_score, mut desc_hits) = item
            .description
            .as_deref()
            .and_then(|desc| simple_match(desc, query, case_matching))
            .unwrap_or((0, Vec::new()));
        if desc_score > 0 {
            score = score.saturating_add(desc_score);
            matched = true;
        }

        let (desc_right_score, mut desc_right_hits) = item
            .description_right
            .as_deref()
            .and_then(|desc| simple_match(desc, query, case_matching))
            .unwrap_or((0, Vec::new()));
        if desc_right_score > 0 {
            score = score.saturating_add(desc_right_score);
            matched = true;
        }

        if matched {
            label_hits.sort_unstable();
            label_hits.dedup();
            desc_hits.sort_unstable();
            desc_hits.dedup();
            desc_right_hits.sort_unstable();
            desc_right_hits.dedup();

            results.push(SearchResult {
                item_index: index,
                score,
                label_hits,
                description_hits: desc_hits,
                description_right_hits: desc_right_hits,
            });
        }
    }

    results.sort_by(|a, b| b.score.cmp(&a.score).then(a.item_index.cmp(&b.item_index)));
    results
}

#[cfg(target_arch = "wasm32")]
fn simple_match(
    haystack: &str,
    query: &str,
    case_matching: CaseMatching,
) -> Option<(u32, Vec<u32>)> {
    let haystack_chars: Vec<char> = haystack.chars().collect();
    let query_chars: Vec<char> = query.chars().collect();

    let respects_case = match case_matching {
        CaseMatching::Respect => true,
        CaseMatching::Ignore => false,
        CaseMatching::Smart => query_chars.iter().any(|ch| ch.is_uppercase()),
        _ => query_chars.iter().any(|ch| ch.is_uppercase()),
    };

    let chars_equal = |a: char, b: char| {
        if respects_case {
            a == b
        } else {
            a.to_lowercase().to_string() == b.to_lowercase().to_string()
        }
    };

    let mut hits = Vec::with_capacity(query_chars.len());
    let mut search_from = 0usize;
    for query_ch in query_chars {
        let Some(pos) = haystack_chars
            .iter()
            .enumerate()
            .skip(search_from)
            .find_map(|(idx, hay_ch)| chars_equal(*hay_ch, query_ch).then_some(idx))
        else {
            return None;
        };
        hits.push(pos as u32);
        search_from = pos.saturating_add(1);
    }

    let span = hits.last().copied().unwrap_or(0).saturating_sub(hits[0]);
    let contiguous_bonus = hits
        .windows(2)
        .filter(|pair| pair[1] == pair[0] + 1)
        .count() as u32
        * 8;
    let start_bonus = 64u32.saturating_sub(hits[0]);
    let compact_bonus = 32u32.saturating_sub(span);
    let length_bonus = (query.len() as u32).saturating_mul(16);
    let score = length_bonus
        .saturating_add(contiguous_bonus)
        .saturating_add(start_bonus)
        .saturating_add(compact_bonus);

    Some((score, hits))
}