Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: pkg/analysis_server/lib/src/services/index2/index2.dart

Issue 1783503004: Listen for context add/remove and unit invalidation to update the index. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'dart:async'; 5 import 'dart:async';
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/element/element.dart'; 8 import 'package:analyzer/dart/element/element.dart';
9 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; 9 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext;
10 import 'package:analyzer/src/generated/source.dart'; 10 import 'package:analyzer/src/generated/source.dart';
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 } 88 }
89 89
90 /** 90 /**
91 * Remove all index information for the given [context]. 91 * Remove all index information for the given [context].
92 */ 92 */
93 void removeContext(AnalysisContext context) { 93 void removeContext(AnalysisContext context) {
94 _contextIndexMap.remove(context); 94 _contextIndexMap.remove(context);
95 } 95 }
96 96
97 /** 97 /**
98 * Remove index information about the unit in the given [context].
99 */
100 void removeUnit(
101 AnalysisContext context, Source librarySource, Source unitSource) {
102 _contextIndexMap[context]?.removeUnit(librarySource, unitSource);
103 }
104
105 /**
98 * Return the [_ContextIndex] instance for the given [context]. 106 * Return the [_ContextIndex] instance for the given [context].
99 */ 107 */
100 _ContextIndex _getContextIndex(AnalysisContext context) { 108 _ContextIndex _getContextIndex(AnalysisContext context) {
101 return _contextIndexMap.putIfAbsent(context, () { 109 return _contextIndexMap.putIfAbsent(context, () {
102 return new _ContextIndex(context); 110 return new _ContextIndex(context);
103 }); 111 });
104 } 112 }
105 113
106 /** 114 /**
107 * Complete with a list of all results returned by the [callback] for every 115 * Complete with a list of all results returned by the [callback] for every
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 final Map<String, PackageIndex> indexMap = <String, PackageIndex>{}; 208 final Map<String, PackageIndex> indexMap = <String, PackageIndex>{};
201 209
202 _ContextIndex(this.context); 210 _ContextIndex(this.context);
203 211
204 /** 212 /**
205 * Complete with a list of locations where elements of the given [kind] with 213 * Complete with a list of locations where elements of the given [kind] with
206 * names satisfying the given [regExp] are defined. 214 * names satisfying the given [regExp] are defined.
207 */ 215 */
208 Future<List<Location>> getDefinedNames( 216 Future<List<Location>> getDefinedNames(
209 RegExp regExp, IndexNameKind kind) async { 217 RegExp regExp, IndexNameKind kind) async {
210 return _mergeLocations((PackageIndex index) { 218 return _mergeLocations((_PackageIndexRequester requester) {
211 _PackageIndexRequester requester = new _PackageIndexRequester(index);
212 return requester.getDefinedNames(context, regExp, kind); 219 return requester.getDefinedNames(context, regExp, kind);
213 }); 220 });
214 } 221 }
215 222
216 /** 223 /**
217 * Complete with a list of locations where the given [element] has relation 224 * Complete with a list of locations where the given [element] has relation
218 * of the given [kind]. 225 * of the given [kind].
219 */ 226 */
220 Future<List<Location>> getRelations(Element element, IndexRelationKind kind) { 227 Future<List<Location>> getRelations(Element element, IndexRelationKind kind) {
221 return _mergeLocations((PackageIndex index) { 228 return _mergeLocations((_PackageIndexRequester requester) {
222 _PackageIndexRequester requester = new _PackageIndexRequester(index);
223 return requester.getRelations(context, element, kind); 229 return requester.getRelations(context, element, kind);
224 }); 230 });
225 } 231 }
226 232
227 /** 233 /**
228 * Complete with a list of locations where a class members with the given 234 * Complete with a list of locations where a class members with the given
229 * [name] is referenced with a qualifier, but is not resolved. 235 * [name] is referenced with a qualifier, but is not resolved.
230 */ 236 */
231 Future<List<Location>> getUnresolvedMemberReferences(String name) async { 237 Future<List<Location>> getUnresolvedMemberReferences(String name) async {
232 return _mergeLocations((PackageIndex index) { 238 return _mergeLocations((_PackageIndexRequester requester) {
233 _PackageIndexRequester requester = new _PackageIndexRequester(index);
234 return requester.getUnresolvedMemberReferences(context, name); 239 return requester.getUnresolvedMemberReferences(context, name);
235 }); 240 });
236 } 241 }
237 242
238 /** 243 /**
239 * Index the given fully resolved [unit]. 244 * Index the given fully resolved [unit].
240 */ 245 */
241 void indexUnit(CompilationUnit unit) { 246 void indexUnit(CompilationUnit unit) {
242 // Index the unit. 247 // Index the unit.
243 PackageIndexAssembler assembler = new PackageIndexAssembler(); 248 PackageIndexAssembler assembler = new PackageIndexAssembler();
244 assembler.index(unit); 249 assembler.index(unit);
245 PackageIndexBuilder indexBuilder = assembler.assemble(); 250 PackageIndexBuilder indexBuilder = assembler.assemble();
246 // Put the index into the map. 251 // Put the index into the map.
247 List<int> indexBytes = indexBuilder.toBuffer(); 252 List<int> indexBytes = indexBuilder.toBuffer();
248 PackageIndex index = new PackageIndex.fromBuffer(indexBytes); 253 PackageIndex index = new PackageIndex.fromBuffer(indexBytes);
249 String key = _getUnitKeyForElement(unit.element); 254 String key = _getUnitKeyForElement(unit.element);
250 indexMap[key] = index; 255 indexMap[key] = index;
251 } 256 }
252 257
258 /**
259 * Remove index information about the unit.
260 */
261 void removeUnit(Source librarySource, Source unitSource) {
262 String key = _getUnitKeyForSource(librarySource, unitSource);
263 indexMap.remove(key);
264 }
265
253 String _getUnitKeyForElement(CompilationUnitElement unitElement) { 266 String _getUnitKeyForElement(CompilationUnitElement unitElement) {
254 Source librarySource = unitElement.library.source; 267 Source librarySource = unitElement.library.source;
255 Source unitSource = unitElement.source; 268 Source unitSource = unitElement.source;
256 return _getUnitKeyForSource(librarySource, unitSource); 269 return _getUnitKeyForSource(librarySource, unitSource);
257 } 270 }
258 271
259 String _getUnitKeyForSource(Source librarySource, Source unitSource) { 272 String _getUnitKeyForSource(Source librarySource, Source unitSource) {
260 String unitLibraryUri = librarySource.uri.toString(); 273 String unitLibraryUri = librarySource.uri.toString();
261 String unitUnitUri = unitSource.uri.toString(); 274 String unitUnitUri = unitSource.uri.toString();
262 return '$unitLibraryUri;$unitUnitUri'; 275 return '$unitLibraryUri;$unitUnitUri';
263 } 276 }
264 277
265 Future<List<Location>> _mergeLocations( 278 Future<List<Location>> _mergeLocations(
266 List<Location> callback(PackageIndex index)) async { 279 List<Location> callback(_PackageIndexRequester requester)) async {
267 List<Location> locations = <Location>[]; 280 List<Location> locations = <Location>[];
268 for (PackageIndex index in indexMap.values) { 281 for (PackageIndex index in indexMap.values) {
269 List<Location> indexLocations = callback(index); 282 _PackageIndexRequester requester = new _PackageIndexRequester(index);
283 List<Location> indexLocations = callback(requester);
270 locations.addAll(indexLocations); 284 locations.addAll(indexLocations);
271 } 285 }
272 return locations; 286 return locations;
273 } 287 }
274 } 288 }
275 289
276 /** 290 /**
277 * Helper for requesting information from a single [PackageIndex]. 291 * Helper for requesting information from a single [PackageIndex].
278 */ 292 */
279 class _PackageIndexRequester { 293 class _PackageIndexRequester {
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 i < unitIndex.usedNames.length && unitIndex.usedNames[i] == nameId; 531 i < unitIndex.usedNames.length && unitIndex.usedNames[i] == nameId;
518 i++) { 532 i++) {
519 unitLibraryUri ??= packageRequester.getUnitLibraryUri(unitIndex.unit); 533 unitLibraryUri ??= packageRequester.getUnitLibraryUri(unitIndex.unit);
520 unitUnitUri ??= packageRequester.getUnitUnitUri(unitIndex.unit); 534 unitUnitUri ??= packageRequester.getUnitUnitUri(unitIndex.unit);
521 locations.add(new Location(context, unitLibraryUri, unitUnitUri, 535 locations.add(new Location(context, unitLibraryUri, unitUnitUri,
522 unitIndex.usedNameOffsets[i], name.length, true)); 536 unitIndex.usedNameOffsets[i], name.length, true));
523 } 537 }
524 return locations; 538 return locations;
525 } 539 }
526 } 540 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698