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
33 changes: 15 additions & 18 deletions app/lib/search/mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final _textSearchTimeout = Duration(milliseconds: 500);
class InMemoryPackageIndex {
final List<PackageDocument> _documents;
final _documentsByName = <String, PackageDocument>{};
final _packageNameIndex = PackageNameIndex();
late final PackageNameIndex _packageNameIndex;
late final TokenIndex _descrIndex;
late final TokenIndex _readmeIndex;
late final TokenIndex _apiSymbolIndex;
Expand Down Expand Up @@ -54,7 +54,6 @@ class InMemoryPackageIndex {
final apiDocPageValues = <String>[];
for (final doc in _documents) {
_documentsByName[doc.package] = doc;
_packageNameIndex.add(doc.package);

final apiDocPages = doc.apiDocPages;
if (apiDocPages != null) {
Expand All @@ -74,6 +73,7 @@ class InMemoryPackageIndex {
}

final packageKeys = _documents.map((d) => d.package).toList();
_packageNameIndex = PackageNameIndex(packageKeys);
_descrIndex = TokenIndex(
packageKeys,
_documents.map((d) => d.description).toList(),
Expand Down Expand Up @@ -501,35 +501,32 @@ class _TextResults {
/// A simple (non-inverted) index designed for package name lookup.
@visibleForTesting
class PackageNameIndex {
final _data = <String, _PkgNameData>{};
final List<String> _packageNames;
late final Map<String, _PkgNameData> _data;

PackageNameIndex(this._packageNames) {
_data = Map.fromEntries(_packageNames.map((package) {
final collapsed = _collapseName(package);
return MapEntry(
package,
_PkgNameData(collapsed, trigrams(collapsed).toSet()),
);
}));
}

/// Maps package name to a reduced form of the name:
/// the same character parts, but without `-`.
String _collapseName(String package) =>
package.replaceAll('_', '').toLowerCase();

void addAll(Iterable<String> packages) {
for (final package in packages) {
add(package);
}
}

/// Add a new [package] to the index.
void add(String package) {
_data.putIfAbsent(package, () {
final collapsed = _collapseName(package);
return _PkgNameData(collapsed, trigrams(collapsed).toSet());
});
}

/// Search [text] and return the matching packages with scores.
Score search(String text) {
return Score.multiply(splitForQuery(text).map(searchWord).toList());
}

/// Search using the parsed [word] and return the match packages with scores.
Score searchWord(String word, {Set<String>? packages}) {
final pkgNamesToCheck = packages ?? _data.keys;
final pkgNamesToCheck = packages ?? _packageNames;
final values = <String, double>{};
final singularWord = word.length <= 3 || !word.endsWith('s')
? word
Expand Down
17 changes: 8 additions & 9 deletions app/test/search/package_name_index_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import 'package:test/test.dart';

void main() {
group('PackageNameIndex', () {
final index = PackageNameIndex()
..addAll([
'fluent',
'fluent_ui',
'get',
'get_it',
'modular',
'modular_flutter',
]);
final index = PackageNameIndex([
'fluent',
'fluent_ui',
'get',
'get_it',
'modular',
'modular_flutter',
]);

test('fluent vs fluent_ui', () {
expect(
Expand Down
Loading