import '../../ui/theme/app_design.dart'; /// [designs] narrowed to [query] and ranked, best first, for the design /// switcher (Alt+L). /// /// Every whitespace-separated word of the query must occur, ignoring case, in /// the design's name or its one-line description. A word ranks by where it /// matched: the start of the name, anywhere in the name, the start of a word /// in the description, anywhere in it. A design's rank is the sum over the /// query's words; equal ranks keep [designs]' order (the registry's). List designQuickSwitchMatches( List designs, String query, ) { final words = query.toLowerCase().split(RegExp(r'\s+')) ..removeWhere((word) => word.isEmpty); if (words.isEmpty) return designs; final ranked = <({int rank, int index, AppDesign design})>[]; for (final (index, design) in designs.indexed) { final name = design.label.toLowerCase(); final description = design.description.toLowerCase(); var rank = 0; for (final word in words) { final tier = name.startsWith(word) ? 0 : name.contains(word) ? 1 : RegExp( '(^|[^\\p{L}\\p{N}])${RegExp.escape(word)}', unicode: true, ).hasMatch(description) ? 2 : description.contains(word) ? 3 : null; if (tier == null) { rank = -1; break; } rank += tier; } if (rank >= 0) ranked.add((rank: rank, index: index, design: design)); } ranked.sort((a, b) => a.rank != b.rank ? a.rank - b.rank : a.index - b.index); return [for (final hit in ranked) hit.design]; }