@@ -670,6 +670,60 @@ impl HomeView {
670670 }
671671 }
672672
673+ /// Re-score matches after a reload without moving the cursor.
674+ pub ( super ) fn refresh_search_matches ( & mut self ) {
675+ let query = self . search_query . value ( ) ;
676+ if query. is_empty ( ) {
677+ self . search_matches . clear ( ) ;
678+ self . search_match_index = 0 ;
679+ return ;
680+ }
681+
682+ use nucleo_matcher:: pattern:: { Atom , AtomKind , CaseMatching , Normalization } ;
683+ use nucleo_matcher:: { Config , Matcher , Utf32Str } ;
684+
685+ let mut matcher = Matcher :: new ( Config :: DEFAULT . match_paths ( ) ) ;
686+ let atom = Atom :: new (
687+ query,
688+ CaseMatching :: Ignore ,
689+ Normalization :: Smart ,
690+ AtomKind :: Fuzzy ,
691+ false ,
692+ ) ;
693+
694+ let mut scored: Vec < ( usize , u16 ) > = Vec :: new ( ) ;
695+ let mut buf = Vec :: new ( ) ;
696+
697+ for ( idx, item) in self . flat_items . iter ( ) . enumerate ( ) {
698+ let haystack = match item {
699+ Item :: Session { id, .. } => {
700+ if let Some ( inst) = self . instance_map . get ( id) {
701+ format ! ( "{} {}" , inst. title, inst. project_path)
702+ } else {
703+ continue ;
704+ }
705+ }
706+ Item :: Group { name, path, .. } => {
707+ format ! ( "{} {}" , name, path)
708+ }
709+ } ;
710+
711+ let haystack_utf32 = Utf32Str :: new ( & haystack, & mut buf) ;
712+ if let Some ( score) = atom. score ( haystack_utf32, & mut matcher) {
713+ scored. push ( ( idx, score) ) ;
714+ }
715+ }
716+
717+ scored. sort_by ( |a, b| b. 1 . cmp ( & a. 1 ) ) ;
718+ self . search_matches = scored. into_iter ( ) . map ( |( idx, _) | idx) . collect ( ) ;
719+ // Clamp match_index in case matches shrank
720+ if self . search_matches . is_empty ( ) {
721+ self . search_match_index = 0 ;
722+ } else if self . search_match_index >= self . search_matches . len ( ) {
723+ self . search_match_index = self . search_matches . len ( ) - 1 ;
724+ }
725+ }
726+
673727 pub ( super ) fn update_search ( & mut self ) {
674728 self . search_matches . clear ( ) ;
675729 self . search_match_index = 0 ;
0 commit comments