44
55import 'dart:math' ;
66
7+ import 'package:gcloud/service_scope.dart' as ss;
8+ import 'package:logging/logging.dart' ;
79import 'package:meta/meta.dart' ;
810// ignore: implementation_imports
911import 'package:pana/src/dartdoc/dartdoc_index.dart' ;
1012import 'package:path/path.dart' as p;
11- import 'package:pub_dev/search/flutter_sdk_mem_index .dart' ;
13+ import 'package:pub_dev/search/backend .dart' ;
1214
1315import 'search_service.dart' ;
1416import 'token_index.dart' ;
1517
1618export 'package:pana/src/dartdoc/dartdoc_index.dart' ;
1719
20+ /// Sets the SDK in-memory index.
21+ void registerSdkMemIndex (SdkMemIndex ? index) {
22+ if (index != null ) {
23+ ss.register (#_sdkMemIndex, index);
24+ }
25+ }
26+
27+ /// The active SDK in-memory index.
28+ SdkMemIndex ? get sdkMemIndex => ss.lookup (#_sdkMemIndex) as SdkMemIndex ? ;
29+
1830/// Results from these libraries are ranked with lower score and
1931/// will be displayed only if the query has the library name, or
2032/// there are not other results that could match the query.
@@ -30,48 +42,54 @@ const _defaultApiPageDirWeights = {
3042 'material/Icons' : 0.25 ,
3143};
3244
45+ final _logger = Logger ('search.dart_sdk_mem_index' );
46+ final _dartUri = Uri .parse ('https://api.dart.dev/stable/latest/' );
47+ final _flutterUri = Uri .parse ('https://api.flutter.dev/flutter/' );
48+
49+ /// Tries to load Dart and Flutter SDK's dartdoc `index.json` and build
50+ /// a search index from it.
51+ ///
52+ /// Returns `null` when the loading of `index.json` failed, or when there
53+ /// was an error parsing the file or building the index.
54+ Future <SdkMemIndex ?> createSdkMemIndex () async {
55+ try {
56+ final dartSdkContent =
57+ await loadOrFetchSdkIndexJsonAsString (SdkMemIndex .dartSdkIndexJsonUri);
58+ final flutterSdkContent = await loadOrFetchSdkIndexJsonAsString (
59+ SdkMemIndex ._flutterSdkIndexJsonUri);
60+ return SdkMemIndex (
61+ dartIndex: DartdocIndex .parseJsonText (dartSdkContent),
62+ flutterIndex: DartdocIndex .parseJsonText (flutterSdkContent),
63+ );
64+ } catch (e, st) {
65+ _logger.warning ('Unable to load SDK index.' , e, st);
66+ return null ;
67+ }
68+ }
69+
3370/// In-memory index for SDK library search queries.
3471class SdkMemIndex {
3572 final _libraries = < String , _Library > {};
3673 final Map <String , double > _apiPageDirWeights;
3774
3875 SdkMemIndex ({
39- required String sdk,
40- required Uri baseUri,
41- required DartdocIndex index,
42- Set <String >? allowedLibraries,
76+ required DartdocIndex dartIndex,
77+ required DartdocIndex flutterIndex,
4378 Map <String , double >? apiPageDirWeights,
4479 }) : _apiPageDirWeights = apiPageDirWeights ?? _defaultApiPageDirWeights {
45- _addDartdocIndex (sdk, baseUri, index, allowedLibraries);
46- }
47-
48- static SdkMemIndex dart ({required DartdocIndex index}) {
49- return SdkMemIndex (
50- sdk: 'dart' ,
51- baseUri: Uri .parse ('https://api.dart.dev/stable/latest/' ),
52- index: index,
53- );
54- }
55-
56- factory SdkMemIndex .flutter ({required DartdocIndex index}) {
57- return SdkMemIndex (
58- sdk: 'flutter' ,
59- baseUri: Uri .parse ('https://api.flutter.dev/flutter/' ),
60- index: index,
61- allowedLibraries: flutterSdkAllowedLibraries,
62- );
80+ _addDartdocIndex ('dart' , _dartUri, dartIndex);
81+ _addDartdocIndex ('flutter' , _flutterUri, flutterIndex);
6382 }
6483
6584 static final dartSdkIndexJsonUri =
6685 Uri .parse ('https://api.dart.dev/stable/latest/index.json' );
67- static final flutterSdkIndexJsonUri =
86+ static final _flutterSdkIndexJsonUri =
6887 Uri .parse ('https://api.flutter.dev/flutter/index.json' );
6988
7089 void _addDartdocIndex (
7190 String sdk,
7291 Uri baseUri,
7392 DartdocIndex index,
74- Set <String >? allowedLibraries,
7593 ) {
7694 final textsPerLibrary = < String , Map <String , String >> {};
7795 final baseUris = < String , Uri > {};
@@ -81,11 +99,7 @@ class SdkMemIndex {
8199 final library = f.qualifiedName? .split ('.' ).first;
82100 if (library == null ) continue ;
83101 if (f.href == null ) continue ;
84- if (allowedLibraries != null &&
85- allowedLibraries.isNotEmpty &&
86- ! allowedLibraries.contains (library)) {
87- continue ;
88- }
102+ if (_libraries.containsKey (library)) continue ;
89103 if (f.isLibrary) {
90104 baseUris[library] = baseUri.resolve (f.href! );
91105
@@ -116,13 +130,15 @@ class SdkMemIndex {
116130 List <SdkLibraryHit > search (
117131 String query, {
118132 int ? limit,
133+ bool skipFlutter = false ,
119134 }) {
120135 limit ?? = 2 ;
121136 final words = query.split (' ' ).where ((e) => e.isNotEmpty).toList ();
122137 if (words.isEmpty) return < SdkLibraryHit > [];
123138
124139 final hits = < _Hit > [];
125140 for (final library in _libraries.values) {
141+ if (skipFlutter && library.isFlutter) continue ;
126142 // We may reduce the rank of certain libraries, except when their name is
127143 // also part of the query. E.g. `dart:html` with `query=cursor` may be
128144 // scored lower than `query=html cursor`.
@@ -204,6 +220,7 @@ class _Library {
204220 required this .tokenIndex,
205221 });
206222
223+ late final isFlutter = sdk == 'flutter' ;
207224 late final weight = _libraryWeights[name] ?? 1.0 ;
208225 late final lastNamePart = name.split (':' ).last;
209226}
0 commit comments