Skip to content

Commit 323039c

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate dart/analysis/index.dart
Change-Id: I7e351ea91d16a08782bd64b699bde6b81630413e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412403 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 1e5a270 commit 323039c

File tree

11 files changed

+571
-317
lines changed

11 files changed

+571
-317
lines changed

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ import 'package:meta/meta.dart';
100100
// TODO(scheglov): Clean up the list of implicitly analyzed files.
101101
class AnalysisDriver {
102102
/// The version of data format, should be incremented on every format change.
103-
static const int DATA_VERSION = 433;
103+
static const int DATA_VERSION = 434;
104104

105105
/// The number of exception contexts allowed to write. Once this field is
106106
/// zero, we stop writing any new exception contexts in this process.

pkg/analyzer/lib/src/dart/analysis/index.dart

Lines changed: 294 additions & 224 deletions
Large diffs are not rendered by default.

pkg/analyzer/lib/src/dart/analysis/search.dart

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ class Search {
381381
return _searchReferences(element, searchedFiles);
382382
} else if (element is ConstructorElement) {
383383
return await _searchReferences_Constructor(element, searchedFiles);
384-
} else if (element is CompilationUnitElement) {
384+
} else if (element is CompilationUnitElementImpl) {
385385
return _searchReferences_CompilationUnit(element);
386386
} else if (element is PropertyAccessorElement && element.isGetter) {
387387
return _searchReferences_Getter(element, searchedFiles);
@@ -692,7 +692,7 @@ class Search {
692692
}
693693

694694
Future<List<SearchResult>> _searchReferences_CompilationUnit(
695-
CompilationUnitElement element) async {
695+
CompilationUnitElementImpl element) async {
696696
String path = element.source.fullName;
697697

698698
var file = _driver.resourceProvider.getFile(path);
@@ -704,8 +704,28 @@ class Search {
704704
}
705705

706706
// Check files that reference the given file.
707-
List<SearchResult> results = <SearchResult>[];
707+
var results = <SearchResult>[];
708708
for (var reference in fileState.referencingFiles) {
709+
var index = await _driver.getIndex(reference.path);
710+
if (index != null) {
711+
var targetId = index.getLibraryFragmentId(element);
712+
for (var i = 0; i < index.libFragmentRefTargets.length; i++) {
713+
if (index.libFragmentRefTargets[i] == targetId) {
714+
var refUnit = await _getUnitElement(reference.path);
715+
results.add(
716+
SearchResult._(
717+
refUnit!,
718+
SearchResultKind.REFERENCE,
719+
index.libFragmentRefUriOffsets[i],
720+
index.libFragmentRefUriLengths[i],
721+
true,
722+
true,
723+
),
724+
);
725+
}
726+
}
727+
}
728+
709729
await _addResultsInFile(
710730
results,
711731
element,
@@ -1391,7 +1411,7 @@ class _IndexRequest {
13911411

13921412
void addSubtypes(
13931413
String superIdString, List<SubtypeResult> results, FileState file) {
1394-
var superId = getStringId(superIdString);
1414+
var superId = index.getStringId(superIdString);
13951415
if (superId == -1) {
13961416
return;
13971417
}
@@ -1425,24 +1445,24 @@ class _IndexRequest {
14251445
/// Return the [element]'s identifier in the [index] or `-1` if the
14261446
/// [element] is not referenced in the [index].
14271447
int findElementId(Element element) {
1428-
IndexElementInfo info = IndexElementInfo(element);
1429-
element = info.element;
1448+
IndexElementInfo info = IndexElementInfo(element.asElement2!);
1449+
element = info.element.asElement!;
14301450
// Find the id of the element's unit.
14311451
int unitId = getUnitId(element);
14321452
if (unitId == -1) {
14331453
return -1;
14341454
}
14351455
// Prepare information about the element.
1436-
var components = ElementNameComponents(element);
1437-
int unitMemberId = getStringId(components.unitMemberName);
1456+
var components = ElementNameComponents(element.asElement2!);
1457+
int unitMemberId = index.getStringId(components.unitMemberName);
14381458
if (unitMemberId == -1) {
14391459
return -1;
14401460
}
1441-
int classMemberId = getStringId(components.classMemberName);
1461+
int classMemberId = index.getStringId(components.classMemberName);
14421462
if (classMemberId == -1) {
14431463
return -1;
14441464
}
1445-
int parameterId = getStringId(components.parameterName);
1465+
int parameterId = index.getStringId(components.parameterName);
14461466
if (parameterId == -1) {
14471467
return -1;
14481468
}
@@ -1511,35 +1531,11 @@ class _IndexRequest {
15111531
return results;
15121532
}
15131533

1514-
/// Return the identifier of [str] in the [index] or `-1` if [str] is not
1515-
/// used in the [index].
1516-
int getStringId(String? str) {
1517-
if (str == null) {
1518-
return index.nullStringId;
1519-
}
1520-
1521-
return binarySearch(index.strings, str);
1522-
}
1523-
15241534
/// Return the identifier of the [CompilationUnitElement] containing the
15251535
/// [element] in the [index] or `-1` if not found.
15261536
int getUnitId(Element element) {
1527-
CompilationUnitElement unitElement = getUnitElement(element);
1528-
int libraryUriId = getUriId(unitElement.library.source.uri);
1529-
if (libraryUriId == -1) {
1530-
return -1;
1531-
}
1532-
int unitUriId = getUriId(unitElement.source.uri);
1533-
if (unitUriId == -1) {
1534-
return -1;
1535-
}
1536-
for (int i = 0; i < index.unitLibraryUris.length; i++) {
1537-
if (index.unitLibraryUris[i] == libraryUriId &&
1538-
index.unitUnitUris[i] == unitUriId) {
1539-
return i;
1540-
}
1541-
}
1542-
return -1;
1537+
var unitElement = getUnitElement(element.asElement2!);
1538+
return index.getLibraryFragmentId(unitElement);
15431539
}
15441540

15451541
/// Return a list of results where a class members with the given [name] is
@@ -1550,7 +1546,7 @@ class _IndexRequest {
15501546
Future<CompilationUnitElement?> Function()
15511547
getEnclosingUnitElement) async {
15521548
// Find the name identifier.
1553-
int nameId = getStringId(name);
1549+
int nameId = index.getStringId(name);
15541550
if (nameId == -1) {
15551551
return const <SearchResult>[];
15561552
}
@@ -1582,13 +1578,6 @@ class _IndexRequest {
15821578
return results;
15831579
}
15841580

1585-
/// Return the identifier of the [uri] in the [index] or `-1` if the [uri] is
1586-
/// not used in the [index].
1587-
int getUriId(Uri uri) {
1588-
String str = uri.toString();
1589-
return getStringId(str);
1590-
}
1591-
15921581
/// Return the index of the first occurrence of the [value] in the
15931582
/// [sortedList], or `-1` if the [value] is not in the list.
15941583
int _findFirstOccurrence(List<int> sortedList, int value) {

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13812,7 +13812,7 @@ final class PartDirectiveImpl extends UriBasedDirectiveImpl
1381213812

1381313813
@experimental
1381413814
@override
13815-
PartInclude? get partInclude => element as PartInclude?;
13815+
PartElementImpl? get partInclude => element;
1381613816

1381713817
@override
1381813818
ChildEntities get _childEntities => super._childEntities

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,7 +2079,7 @@ class DirectiveUriWithUnitImpl extends DirectiveUriWithRelativeUriImpl
20792079
});
20802080

20812081
@override
2082-
LibraryFragment get libraryFragment => unit;
2082+
CompilationUnitElementImpl get libraryFragment => unit;
20832083

20842084
@override
20852085
Source get source => unit.source;
@@ -7432,8 +7432,7 @@ class LibraryElementImpl extends ElementImpl
74327432
}
74337433

74347434
@override
7435-
LibraryFragment get firstFragment =>
7436-
definingCompilationUnit as LibraryFragment;
7435+
CompilationUnitElementImpl get firstFragment => definingCompilationUnit;
74377436

74387437
@override
74397438
List<CompilationUnitElementImpl> get fragments {
@@ -9856,8 +9855,8 @@ class PartElementImpl extends _ExistingElementImpl
98569855
String get identifier => 'part';
98579856

98589857
@override
9859-
LibraryFragment? get includedFragment {
9860-
if (uri case DirectiveUriWithUnit uri) {
9858+
CompilationUnitElementImpl? get includedFragment {
9859+
if (uri case DirectiveUriWithUnitImpl uri) {
98619860
return uri.libraryFragment;
98629861
}
98639862
return null;
@@ -9867,7 +9866,7 @@ class PartElementImpl extends _ExistingElementImpl
98679866
ElementKind get kind => ElementKind.PART;
98689867

98699868
@override
9870-
LibraryFragment get libraryFragment => enclosingUnit;
9869+
CompilationUnitElementImpl get libraryFragment => enclosingUnit;
98719870

98729871
@override
98739872
T? accept<T>(ElementVisitor<T> visitor) => visitor.visitPartElement(this);

0 commit comments

Comments
 (0)