@@ -368,9 +368,12 @@ class InMemoryPackageIndex {
368
368
}
369
369
370
370
Set <String >? nameMatches;
371
- if (includeNameMatches && _documentsByName.containsKey (text)) {
372
- nameMatches ?? = < String > {};
373
- nameMatches.add (text);
371
+ if (includeNameMatches) {
372
+ final matches = _packageNameIndex.lookupMatchingNames (text);
373
+ if (matches != null ) {
374
+ nameMatches ?? = < String > {};
375
+ nameMatches.addAll (matches);
376
+ }
374
377
}
375
378
376
379
// Multiple words are scored separately, and then the individual scores
@@ -384,9 +387,12 @@ class InMemoryPackageIndex {
384
387
final matchApi = textMatchExtent.shouldMatchApi ();
385
388
386
389
for (final word in words) {
387
- if (includeNameMatches && _documentsByName.containsKey (word)) {
388
- nameMatches ?? = < String > {};
389
- nameMatches.add (word);
390
+ if (includeNameMatches) {
391
+ final matches = _packageNameIndex.lookupMatchingNames (word);
392
+ if (matches != null ) {
393
+ nameMatches ?? = < String > {};
394
+ nameMatches.addAll (matches);
395
+ }
390
396
}
391
397
392
398
_scorePool.withScore (
@@ -567,12 +573,21 @@ class PackageNameIndex {
567
573
final List <String > _packageNames;
568
574
late final List <_PkgNameData > _data;
569
575
576
+ /// Maps the collapsed name to all the original names (e.g. `asyncmap` => [`async_map`, `as_y_n_cmaP`] ).
577
+ late final Map <String , List <String >> _collapsedNameResolvesToMap;
578
+
570
579
PackageNameIndex (this ._packageNames) {
571
580
_data = _packageNames.map ((package) {
572
581
final lowercased = package.toLowerCase ();
573
582
final collapsed = _removeUnderscores (lowercased);
574
583
return _PkgNameData (lowercased, collapsed, trigrams (collapsed).toSet ());
575
584
}).toList ();
585
+ _collapsedNameResolvesToMap = {};
586
+ for (var i = 0 ; i < _data.length; i++ ) {
587
+ _collapsedNameResolvesToMap
588
+ .putIfAbsent (_data[i].collapsed, () => [])
589
+ .add (_packageNames[i]);
590
+ }
576
591
}
577
592
578
593
String _removeUnderscores (String text) => text.replaceAll ('_' , '' );
@@ -654,6 +669,11 @@ class PackageNameIndex {
654
669
}
655
670
}
656
671
}
672
+
673
+ /// Returns the list of package names where the collapsed name matches.
674
+ List <String >? lookupMatchingNames (String text) {
675
+ return _collapsedNameResolvesToMap[_removeUnderscores (text.toLowerCase ())];
676
+ }
657
677
}
658
678
659
679
class _PkgNameData {
0 commit comments