@@ -49,36 +49,16 @@ class DartLazyTypeHierarchyComputer {
4949 }
5050
5151 /// Finds supertypes for the [Element2] at [location] .
52- ///
53- /// If [anchor] is provided, it will be used to navigate to the element at
54- /// [location] to preserve type arguments that have been provided along the
55- /// way.
56- ///
57- /// Anchors are included in returned types (where necessary to preserve type
58- /// arguments) that can be used when calling for the next level of types.
5952 Future <List <TypeHierarchyRelatedItem >?> findSupertypes (
60- ElementLocation location, {
61- TypeHierarchyAnchor ? anchor,
62- }) async {
53+ ElementLocation location,
54+ ) async {
6355 var targetElement = await _findTargetElement (location);
6456 if (targetElement == null ) {
6557 return null ;
6658 }
6759 var targetType = targetElement.thisType;
6860
69- // If we were provided an anchor, use it to re-locate the target type so
70- // that any type arguments supplied along the way will be preserved in the
71- // new node.
72- if (anchor != null ) {
73- targetType =
74- await _locateTargetFromAnchor (anchor, targetType) ?? targetType;
75- }
76-
77- // If we had no existing anchor, create one that starts from this target as
78- // the starting point for the new supertypes.
79- anchor ?? = TypeHierarchyAnchor (location: location, path: []);
80-
81- return _getSupertypes (targetType, anchor: anchor);
61+ return _getSupertypes (targetType);
8262 }
8363
8464 /// Finds a target for starting type hierarchy navigation at [offset] .
@@ -101,7 +81,9 @@ class DartLazyTypeHierarchyComputer {
10181 }
10282 }
10383
104- return type is InterfaceType ? TypeHierarchyItem .forType (type) : null ;
84+ return type is InterfaceType
85+ ? TypeHierarchyItem .forElement (type.element3)
86+ : null ;
10587 }
10688
10789 /// Locate the [Element2] referenced by [location] .
@@ -153,10 +135,7 @@ class DartLazyTypeHierarchyComputer {
153135 /// Includes all elements that contribute implementation to the type
154136 /// such as supertypes and mixins, but not interfaces, constraints or
155137 /// extended types.
156- List <TypeHierarchyRelatedItem > _getSupertypes (
157- InterfaceType type, {
158- TypeHierarchyAnchor ? anchor,
159- }) {
138+ List <TypeHierarchyRelatedItem > _getSupertypes (InterfaceType type) {
160139 var supertype = type.superclass;
161140 var interfaces = type.interfaces;
162141 var mixins = type.mixins;
@@ -170,19 +149,6 @@ class DartLazyTypeHierarchyComputer {
170149 ...mixins.map (TypeHierarchyRelatedItem .mixesIn),
171150 ].nonNulls.toList ();
172151
173- if (anchor != null ) {
174- for (var (index, item) in supertypes.indexed) {
175- // We only need to carry the anchor along if the supertype has type
176- // arguments that we may be populating.
177- if (item._type.typeArguments.isNotEmpty) {
178- item._anchor = TypeHierarchyAnchor (
179- location: anchor.location,
180- path: [...anchor.path, index],
181- );
182- }
183- }
184- }
185-
186152 return supertypes;
187153 }
188154
@@ -193,29 +159,6 @@ class DartLazyTypeHierarchyComputer {
193159 declaration is MixinDeclaration ||
194160 declaration is ExtensionTypeDeclaration ||
195161 declaration is EnumDeclaration ;
196-
197- /// Navigate to [target] from [anchor] , preserving type arguments supplied
198- /// along the way.
199- Future <InterfaceType ?> _locateTargetFromAnchor (
200- TypeHierarchyAnchor anchor,
201- InterfaceType target,
202- ) async {
203- // Start from the anchor.
204- var anchorElement = await _findTargetElement (anchor.location);
205- var anchorPath = anchor.path;
206-
207- // Follow the provided path.
208- var type = anchorElement? .thisType;
209- for (int i = 0 ; i < anchorPath.length && type != null ; i++ ) {
210- var index = anchorPath[i];
211- var supertypes = _getSupertypes (type);
212- type = supertypes.length >= index + 1 ? supertypes[index]._type : null ;
213- }
214-
215- // Verify the element we arrived at matches the targetElement to guard
216- // against code changes that made the path from the anchor invalid.
217- return type != null && type.element3 == target.element3 ? type : null ;
218- }
219162}
220163
221164class TypeHierarchyAnchor {
@@ -241,9 +184,6 @@ class TypeHierarchyItem {
241184 /// names/identifiers have not changed).
242185 final ElementLocation location;
243186
244- /// The type being displayed.
245- final InterfaceType _type;
246-
247187 /// The file that contains the declaration of this item.
248188 final String file;
249189
@@ -254,28 +194,26 @@ class TypeHierarchyItem {
254194 final SourceRange codeRange;
255195
256196 TypeHierarchyItem ({
257- required InterfaceType type,
258197 required this .displayName,
259198 required this .location,
260199 required this .file,
261200 required this .nameRange,
262201 required this .codeRange,
263- }) : _type = type ;
202+ });
264203
265- TypeHierarchyItem ._forType ({
266- required InterfaceType type ,
204+ TypeHierarchyItem ._forElement ({
205+ required InterfaceElement2 element ,
267206 required this .location,
268- }) : _type = type,
269- displayName = _displayNameForType (type),
270- nameRange = _nameRangeForElement (type.element3),
271- codeRange = _codeRangeForElement (type.element3),
272- file = type.element3.firstFragment.libraryFragment.source.fullName;
273-
274- static TypeHierarchyItem ? forType (InterfaceType type) {
275- var location = ElementLocation .forElement (type.element3);
207+ }) : displayName = _displayNameForElement (element),
208+ nameRange = _nameRangeForElement (element),
209+ codeRange = _codeRangeForElement (element),
210+ file = element.firstFragment.libraryFragment.source.fullName;
211+
212+ static TypeHierarchyItem ? forElement (InterfaceElement2 element) {
213+ var location = ElementLocation .forElement (element);
276214 if (location == null ) return null ;
277215
278- return TypeHierarchyItem ._forType (type : type , location: location);
216+ return TypeHierarchyItem ._forElement (element : element , location: location);
279217 }
280218
281219 /// Returns the [SourceRange] of the code for [element] .
@@ -285,9 +223,9 @@ class TypeHierarchyItem {
285223 return SourceRange (firstFragment.codeOffset! , firstFragment.codeLength! );
286224 }
287225
288- /// Returns a name to display in the hierarchy for [type ] .
289- static String _displayNameForType ( InterfaceType type ) {
290- return type .getDisplayString ();
226+ /// Returns a name to display in the hierarchy for [element ] .
227+ static String _displayNameForElement ( InterfaceElement2 element ) {
228+ return element.baseElement.thisType .getDisplayString ();
291229 }
292230
293231 /// Returns the [SourceRange] of the name for [element] .
@@ -316,51 +254,48 @@ class TypeHierarchyRelatedItem extends TypeHierarchyItem {
316254 /// The relationship this item has with the target item.
317255 final TypeHierarchyItemRelationship relationship;
318256
319- TypeHierarchyAnchor ? _anchor;
320-
321- TypeHierarchyRelatedItem ({
322- required super .type,
257+ TypeHierarchyRelatedItem .forElement ({
258+ required super .element,
323259 required this .relationship,
324- required super .displayName,
325260 required super .location,
326- required super .file,
327- required super .nameRange,
328- required super .codeRange,
329- });
330-
331- TypeHierarchyRelatedItem .forType ({
332- required super .type,
333- required this .relationship,
334- required super .location,
335- }) : super ._forType ();
336-
337- /// An optional anchor element used to preserve type args.
338- TypeHierarchyAnchor ? get anchor => _anchor;
261+ }) : super ._forElement ();
339262
340263 static TypeHierarchyRelatedItem ? constrainedTo (InterfaceType type) =>
341- _forType (type, relationship: TypeHierarchyItemRelationship .constrainedTo);
264+ _forElement (
265+ type.element3,
266+ relationship: TypeHierarchyItemRelationship .constrainedTo,
267+ );
342268
343- static TypeHierarchyRelatedItem ? extends_ (InterfaceType type) =>
344- _forType (type, relationship: TypeHierarchyItemRelationship .extends_);
269+ static TypeHierarchyRelatedItem ? extends_ (InterfaceType type) => _forElement (
270+ type.element3,
271+ relationship: TypeHierarchyItemRelationship .extends_,
272+ );
345273
346274 static TypeHierarchyRelatedItem ? implements (InterfaceType type) =>
347- _forType (type, relationship: TypeHierarchyItemRelationship .implements );
275+ _forElement (
276+ type.element3,
277+ relationship: TypeHierarchyItemRelationship .implements ,
278+ );
348279
349- static TypeHierarchyRelatedItem ? mixesIn (InterfaceType type) =>
350- _forType (type, relationship: TypeHierarchyItemRelationship .mixesIn);
280+ static TypeHierarchyRelatedItem ? mixesIn (InterfaceType type) => _forElement (
281+ type.element3,
282+ relationship: TypeHierarchyItemRelationship .mixesIn,
283+ );
351284
352- static TypeHierarchyRelatedItem ? unknown (InterfaceType type) =>
353- _forType (type, relationship: TypeHierarchyItemRelationship .unknown);
285+ static TypeHierarchyRelatedItem ? unknown (InterfaceType type) => _forElement (
286+ type.element3,
287+ relationship: TypeHierarchyItemRelationship .unknown,
288+ );
354289
355- static TypeHierarchyRelatedItem ? _forType (
356- InterfaceType type , {
290+ static TypeHierarchyRelatedItem ? _forElement (
291+ InterfaceElement2 element , {
357292 required TypeHierarchyItemRelationship relationship,
358293 }) {
359- var location = ElementLocation .forElement (type.element3 );
294+ var location = ElementLocation .forElement (element );
360295 if (location == null ) return null ;
361296
362- return TypeHierarchyRelatedItem .forType (
363- type : type ,
297+ return TypeHierarchyRelatedItem .forElement (
298+ element : element ,
364299 relationship: relationship,
365300 location: location,
366301 );
0 commit comments