@@ -34,9 +34,7 @@ const _defaultApiPageDirWeights = {
34
34
class SdkMemIndex {
35
35
final String _sdk;
36
36
final Uri _baseUri;
37
- final _tokensPerLibrary = < String , TokenIndex <String >> {};
38
- final _baseUriPerLibrary = < String , String > {};
39
- final _descriptionPerLibrary = < String , String > {};
37
+ final _libraries = < String , _Library > {};
40
38
final Map <String , double > _apiPageDirWeights;
41
39
42
40
SdkMemIndex ({
@@ -78,6 +76,9 @@ class SdkMemIndex {
78
76
Set <String >? allowedLibraries,
79
77
) {
80
78
final textsPerLibrary = < String , Map <String , String >> {};
79
+ final baseUrls = < String , String > {};
80
+ final descriptions = < String , String > {};
81
+
81
82
for (final f in index.entries) {
82
83
final library = f.qualifiedName? .split ('.' ).first;
83
84
if (library == null ) continue ;
@@ -88,11 +89,11 @@ class SdkMemIndex {
88
89
continue ;
89
90
}
90
91
if (f.isLibrary) {
91
- _baseUriPerLibrary [library] = _baseUri.resolve (f.href! ).toString ();
92
+ baseUrls [library] = _baseUri.resolve (f.href! ).toString ();
92
93
93
94
final desc = f.desc? .replaceAll (RegExp (r'\s+' ), ' ' ).trim ();
94
95
if (desc != null && desc.isNotEmpty) {
95
- _descriptionPerLibrary [library] = desc;
96
+ descriptions [library] = desc;
96
97
}
97
98
}
98
99
@@ -103,7 +104,12 @@ class SdkMemIndex {
103
104
}
104
105
}
105
106
for (final e in textsPerLibrary.entries) {
106
- _tokensPerLibrary[e.key] = TokenIndex .fromMap (e.value);
107
+ _libraries[e.key] = _Library (
108
+ name: e.key,
109
+ baseUrl: baseUrls[e.key],
110
+ description: descriptions[e.key],
111
+ tokenIndex: TokenIndex .fromMap (e.value),
112
+ );
107
113
}
108
114
}
109
115
@@ -116,24 +122,22 @@ class SdkMemIndex {
116
122
if (words.isEmpty) return < SdkLibraryHit > [];
117
123
118
124
final hits = < _Hit > [];
119
- for (final library in _tokensPerLibrary.keys ) {
125
+ for (final library in _libraries.values ) {
120
126
// We may reduce the rank of certain libraries, except when their name is
121
127
// also part of the query. E.g. `dart:html` with `query=cursor` may be
122
128
// scored lower than `query=html cursor`.
123
- final isQualifiedQuery = query.contains (library.split ( ':' ).last );
129
+ final isQualifiedQuery = query.contains (library.lastNamePart );
124
130
125
- final tokens = _tokensPerLibrary[library]! ;
126
- final plainResults = tokens.withSearchWords (
127
- words, (score) => score.top (3 , minValue: 0.05 ));
131
+ final plainResults = library.tokenIndex
132
+ .withSearchWords (words, (score) => score.top (3 , minValue: 0.05 ));
128
133
if (plainResults.isEmpty) continue ;
129
134
130
- final libraryWeight = _libraryWeights[library] ?? 1.0 ;
131
135
final weightedResults = isQualifiedQuery
132
136
? plainResults
133
137
: plainResults.map (
134
138
(key, value) {
135
139
final dir = p.dirname (key);
136
- final w = (_apiPageDirWeights[dir] ?? 1.0 ) * libraryWeight ;
140
+ final w = (_apiPageDirWeights[dir] ?? 1.0 ) * library.weight ;
137
141
return MapEntry (key, w * value);
138
142
},
139
143
);
@@ -153,9 +157,9 @@ class SdkMemIndex {
153
157
.where ((h) => h.score >= minScore)
154
158
.map ((hit) => SdkLibraryHit (
155
159
sdk: _sdk,
156
- library: hit.library,
157
- description: _descriptionPerLibrary[ hit.library] ,
158
- url: _baseUriPerLibrary[ hit.library] ?? _baseUri.toString (),
160
+ library: hit.library.name ,
161
+ description: hit.library.description ,
162
+ url: hit.library.baseUrl ?? _baseUri.toString (),
159
163
score: hit.score,
160
164
apiPages: hit.top.entries
161
165
.map (
@@ -171,14 +175,31 @@ class SdkMemIndex {
171
175
172
176
@visibleForTesting
173
177
String ? getLibraryDescription (String library) =>
174
- _descriptionPerLibrary [library];
178
+ _libraries [library]? .description ;
175
179
}
176
180
177
181
class _Hit {
178
- final String library;
182
+ final _Library library;
179
183
final Map <String , double > top;
180
184
181
185
_Hit (this .library, this .top);
182
186
183
187
late final score = top.values.fold (0.0 , (a, b) => max (a, b));
184
188
}
189
+
190
+ class _Library {
191
+ final String name;
192
+ final String ? baseUrl;
193
+ final String ? description;
194
+ final TokenIndex <String > tokenIndex;
195
+
196
+ _Library ({
197
+ required this .name,
198
+ required this .baseUrl,
199
+ required this .description,
200
+ required this .tokenIndex,
201
+ });
202
+
203
+ late final weight = _libraryWeights[name] ?? 1.0 ;
204
+ late final lastNamePart = name.split (':' ).last;
205
+ }
0 commit comments