| OLD | NEW |
| 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 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 class _PackageIndexRequester { | 334 class _PackageIndexRequester { |
| 335 final PackageIndex index; | 335 final PackageIndex index; |
| 336 | 336 |
| 337 _PackageIndexRequester(this.index); | 337 _PackageIndexRequester(this.index); |
| 338 | 338 |
| 339 /** | 339 /** |
| 340 * Return the [element]'s identifier in the [index] or `-1` if the | 340 * Return the [element]'s identifier in the [index] or `-1` if the |
| 341 * [element] is not referenced in the [index]. | 341 * [element] is not referenced in the [index]. |
| 342 */ | 342 */ |
| 343 int findElementId(Element element) { | 343 int findElementId(Element element) { |
| 344 IndexElementInfo info = new IndexElementInfo(element); |
| 345 element = info.element; |
| 344 // Find the id of the element's unit. | 346 // Find the id of the element's unit. |
| 345 int unitId = getUnitId(element); | 347 int unitId = getUnitId(element); |
| 346 if (unitId == -1) { | 348 if (unitId == -1) { |
| 347 return -1; | 349 return -1; |
| 348 } | 350 } |
| 349 // Prepare information about the element. | 351 // Prepare information about the element. |
| 350 ElementInfo info = PackageIndexAssembler.newElementInfo(unitId, element); | 352 int unitMemberId = getElementUnitMemberId(element); |
| 351 // Find the first occurrence of an element with the same offset. | 353 if (unitMemberId == -1) { |
| 352 int elementId = _findFirstOccurrence(index.elementOffsets, info.offset); | 354 return -1; |
| 355 } |
| 356 int classMemberId = getElementClassMemberId(element); |
| 357 if (classMemberId == -1) { |
| 358 return -1; |
| 359 } |
| 360 int parameterId = getElementParameterId(element); |
| 361 if (parameterId == -1) { |
| 362 return -1; |
| 363 } |
| 364 // Try to find the element id using classMemberId, parameterId, and kind. |
| 365 int elementId = |
| 366 _findFirstOccurrence(index.elementNameUnitMemberIds, unitMemberId); |
| 353 if (elementId == -1) { | 367 if (elementId == -1) { |
| 354 return -1; | 368 return -1; |
| 355 } | 369 } |
| 356 // Try to find the element id using offset, unit and kind. | |
| 357 for (; | 370 for (; |
| 358 elementId < index.elementOffsets.length && | 371 elementId < index.elementNameUnitMemberIds.length && |
| 359 index.elementOffsets[elementId] == info.offset; | 372 index.elementNameUnitMemberIds[elementId] == unitMemberId; |
| 360 elementId++) { | 373 elementId++) { |
| 361 if (index.elementUnits[elementId] == unitId && | 374 if (index.elementUnits[elementId] == unitId && |
| 375 index.elementNameClassMemberIds[elementId] == classMemberId && |
| 376 index.elementNameParameterIds[elementId] == parameterId && |
| 362 index.elementKinds[elementId] == info.kind) { | 377 index.elementKinds[elementId] == info.kind) { |
| 363 return elementId; | 378 return elementId; |
| 364 } | 379 } |
| 365 } | 380 } |
| 366 return -1; | 381 return -1; |
| 367 } | 382 } |
| 368 | 383 |
| 369 /** | 384 /** |
| 370 * Complete with a list of locations where elements of the given [kind] with | 385 * Complete with a list of locations where elements of the given [kind] with |
| 371 * names satisfying the given [regExp] are defined. | 386 * names satisfying the given [regExp] are defined. |
| 372 */ | 387 */ |
| 373 List<Location> getDefinedNames( | 388 List<Location> getDefinedNames( |
| 374 AnalysisContext context, RegExp regExp, IndexNameKind kind) { | 389 AnalysisContext context, RegExp regExp, IndexNameKind kind) { |
| 375 List<Location> locations = <Location>[]; | 390 List<Location> locations = <Location>[]; |
| 376 for (UnitIndex unitIndex in index.units) { | 391 for (UnitIndex unitIndex in index.units) { |
| 377 _UnitIndexRequester requester = new _UnitIndexRequester(this, unitIndex); | 392 _UnitIndexRequester requester = new _UnitIndexRequester(this, unitIndex); |
| 378 List<Location> unitLocations = | 393 List<Location> unitLocations = |
| 379 requester.getDefinedNames(context, regExp, kind); | 394 requester.getDefinedNames(context, regExp, kind); |
| 380 locations.addAll(unitLocations); | 395 locations.addAll(unitLocations); |
| 381 } | 396 } |
| 382 return locations; | 397 return locations; |
| 383 } | 398 } |
| 384 | 399 |
| 385 /** | 400 /** |
| 401 * Return the [element]'s class member name identifier, `null` is not a class |
| 402 * member, or `-1` if the [element] is not referenced in the [index]. |
| 403 */ |
| 404 int getElementClassMemberId(Element element) { |
| 405 for (; element != null; element = element.enclosingElement) { |
| 406 if (element.enclosingElement is ClassElement) { |
| 407 return getStringId(element.name); |
| 408 } |
| 409 } |
| 410 return getStringId(PackageIndexAssembler.NULL_STRING); |
| 411 } |
| 412 |
| 413 /** |
| 414 * Return the [element]'s class member name identifier, `null` is not a class |
| 415 * member, or `-1` if the [element] is not referenced in the [index]. |
| 416 */ |
| 417 int getElementParameterId(Element element) { |
| 418 for (; element != null; element = element.enclosingElement) { |
| 419 if (element is ParameterElement) { |
| 420 return getStringId(element.name); |
| 421 } |
| 422 } |
| 423 return getStringId(PackageIndexAssembler.NULL_STRING); |
| 424 } |
| 425 |
| 426 /** |
| 427 * Return the [element]'s top-level name identifier, `0` is the unit, or |
| 428 * `-1` if the [element] is not referenced in the [index]. |
| 429 */ |
| 430 int getElementUnitMemberId(Element element) { |
| 431 for (; element != null; element = element.enclosingElement) { |
| 432 if (element.enclosingElement is CompilationUnitElement) { |
| 433 return getStringId(element.name); |
| 434 } |
| 435 } |
| 436 return getStringId(PackageIndexAssembler.NULL_STRING); |
| 437 } |
| 438 |
| 439 /** |
| 386 * Complete with a list of locations where the given [element] has relation | 440 * Complete with a list of locations where the given [element] has relation |
| 387 * of the given [kind]. | 441 * of the given [kind]. |
| 388 */ | 442 */ |
| 389 List<Location> getRelations( | 443 List<Location> getRelations( |
| 390 AnalysisContext context, Element element, IndexRelationKind kind) { | 444 AnalysisContext context, Element element, IndexRelationKind kind) { |
| 391 int elementId = findElementId(element); | 445 int elementId = findElementId(element); |
| 392 if (elementId == -1) { | 446 if (elementId == -1) { |
| 393 return const <Location>[]; | 447 return const <Location>[]; |
| 394 } | 448 } |
| 395 List<Location> locations = <Location>[]; | 449 List<Location> locations = <Location>[]; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 unitUnitUri, | 630 unitUnitUri, |
| 577 unitIndex.usedNameKinds[i], | 631 unitIndex.usedNameKinds[i], |
| 578 unitIndex.usedNameOffsets[i], | 632 unitIndex.usedNameOffsets[i], |
| 579 name.length, | 633 name.length, |
| 580 unitIndex.usedNameIsQualifiedFlags[i], | 634 unitIndex.usedNameIsQualifiedFlags[i], |
| 581 false)); | 635 false)); |
| 582 } | 636 } |
| 583 return locations; | 637 return locations; |
| 584 } | 638 } |
| 585 } | 639 } |
| OLD | NEW |