Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions app/lib/search/mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,12 @@ class InMemoryPackageIndex {
}

Set<String>? nameMatches;
if (includeNameMatches && _documentsByName.containsKey(text)) {
nameMatches ??= <String>{};
nameMatches.add(text);
if (includeNameMatches) {
final matches = _packageNameIndex.lookupMatchingNames(text);
if (matches != null) {
nameMatches ??= <String>{};
nameMatches.addAll(matches);
}
}

// Multiple words are scored separately, and then the individual scores
Expand All @@ -384,9 +387,12 @@ class InMemoryPackageIndex {
final matchApi = textMatchExtent.shouldMatchApi();

for (final word in words) {
if (includeNameMatches && _documentsByName.containsKey(word)) {
nameMatches ??= <String>{};
nameMatches.add(word);
if (includeNameMatches) {
final matches = _packageNameIndex.lookupMatchingNames(word);
if (matches != null) {
nameMatches ??= <String>{};
nameMatches.addAll(matches);
}
}

_scorePool.withScore(
Expand Down Expand Up @@ -567,12 +573,21 @@ class PackageNameIndex {
final List<String> _packageNames;
late final List<_PkgNameData> _data;

/// Maps the collapsed name to all the original names (e.g. `asyncmap`=> [`async_map`, `as_y_n_cmaP`]).
late final Map<String, List<String>> _collapsedNameResolvesToMap;

PackageNameIndex(this._packageNames) {
_data = _packageNames.map((package) {
final lowercased = package.toLowerCase();
final collapsed = _removeUnderscores(lowercased);
return _PkgNameData(lowercased, collapsed, trigrams(collapsed).toSet());
}).toList();
_collapsedNameResolvesToMap = {};
for (var i = 0; i < _data.length; i++) {
_collapsedNameResolvesToMap
.putIfAbsent(_data[i].collapsed, () => [])
.add(_packageNames[i]);
}
}

String _removeUnderscores(String text) => text.replaceAll('_', '');
Expand Down Expand Up @@ -654,6 +669,11 @@ class PackageNameIndex {
}
}
}

/// Returns the list of package names where the collapsed name matches.
List<String>? lookupMatchingNames(String text) {
return _collapsedNameResolvesToMap[_removeUnderscores(text.toLowerCase())];
}
}

class _PkgNameData {
Expand Down
2 changes: 2 additions & 0 deletions app/test/search/json_tool_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 1,
'nameMatches': ['jsontool'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'jsontool', 'score': 1.0},
Expand Down Expand Up @@ -66,6 +67,7 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 3,
'nameMatches': ['jsontool'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'json2entity', 'score': 1.0},
Expand Down
2 changes: 1 addition & 1 deletion app/test/search/mem_index_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure

test('package name match: async', () async {
final PackageSearchResult result =
index.search(ServiceSearchQuery.parse(query: 'async'));
index.search(ServiceSearchQuery.parse(query: 'AsynC'));
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 1,
Expand Down
1 change: 1 addition & 0 deletions app/test/search/stack_trace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 1,
'nameMatches': ['stack_trace'],
'sdkLibraryHits': [],
'packageHits': [
{
Expand Down