@@ -368,9 +368,12 @@ class InMemoryPackageIndex {
368368 }
369369
370370 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+ }
374377 }
375378
376379 // Multiple words are scored separately, and then the individual scores
@@ -384,9 +387,12 @@ class InMemoryPackageIndex {
384387 final matchApi = textMatchExtent.shouldMatchApi ();
385388
386389 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+ }
390396 }
391397
392398 _scorePool.withScore (
@@ -567,12 +573,21 @@ class PackageNameIndex {
567573 final List <String > _packageNames;
568574 late final List <_PkgNameData > _data;
569575
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+
570579 PackageNameIndex (this ._packageNames) {
571580 _data = _packageNames.map ((package) {
572581 final lowercased = package.toLowerCase ();
573582 final collapsed = _removeUnderscores (lowercased);
574583 return _PkgNameData (lowercased, collapsed, trigrams (collapsed).toSet ());
575584 }).toList ();
585+ _collapsedNameResolvesToMap = {};
586+ for (var i = 0 ; i < _data.length; i++ ) {
587+ _collapsedNameResolvesToMap
588+ .putIfAbsent (_data[i].collapsed, () => [])
589+ .add (_packageNames[i]);
590+ }
576591 }
577592
578593 String _removeUnderscores (String text) => text.replaceAll ('_' , '' );
@@ -654,6 +669,11 @@ class PackageNameIndex {
654669 }
655670 }
656671 }
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+ }
657677}
658678
659679class _PkgNameData {
0 commit comments