@@ -251,7 +251,9 @@ MatchingLinkResult _getMatchingLinkElement(
251251
252252 // Try expensive not-scoped lookup.
253253 if (refElement == null ) {
254- refElement = _findRefElementInLibrary (codeRef, element, commentRefs);
254+ Class preferredClass = _getPreferredClass (element);
255+ refElement =
256+ _findRefElementInLibrary (codeRef, element, commentRefs, preferredClass);
255257 }
256258
257259 // This is faster but does not take canonicalization into account; try
@@ -281,7 +283,7 @@ MatchingLinkResult _getMatchingLinkElement(
281283 if (searchElement is Member )
282284 searchElement = Package .getBasestElement (refElement);
283285
284- Class preferredClass = _getPreferredClass (element);
286+ final Class preferredClass = _getPreferredClass (element);
285287 ModelElement refModelElement = element.package.findCanonicalModelElementFor (
286288 searchElement,
287289 preferredClass: preferredClass);
@@ -357,6 +359,8 @@ bool _ConsiderIfConstructor(String codeRef, ModelElement modelElement) {
357359 Constructor aConstructor = modelElement;
358360 List <String > codeRefParts = codeRef.split ('.' );
359361 if (codeRefParts.length > 1 ) {
362+ // Pick the last two parts, in case a specific library was part of the
363+ // codeRef.
360364 if (codeRefParts[codeRefParts.length - 1 ] ==
361365 codeRefParts[codeRefParts.length - 2 ]) {
362366 // Foobar.Foobar -- assume they really do mean the constructor for this class.
@@ -380,8 +384,8 @@ Map<String, Set<ModelElement>> _findRefElementCache;
380384// TODO(jcollins-g): Subcomponents of this function shouldn't be adding nulls to results, strip the
381385// removes out that are gratuitous and debug the individual pieces.
382386// TODO(jcollins-g): A complex package winds up spending a lot of cycles in here. Optimize.
383- Element _findRefElementInLibrary (
384- String codeRef, ModelElement element, List <CommentReference > commentRefs) {
387+ Element _findRefElementInLibrary (String codeRef, ModelElement element,
388+ List <CommentReference > commentRefs, Class preferredClass ) {
385389 assert (element != null );
386390 assert (element.package.allLibrariesAdded);
387391
@@ -394,21 +398,24 @@ Element _findRefElementInLibrary(
394398 // This might be an operator. Strip the operator prefix and try again.
395399 if (results.isEmpty && codeRef.startsWith ('operator' )) {
396400 String newCodeRef = codeRef.replaceFirst ('operator' , '' );
397- return _findRefElementInLibrary (newCodeRef, element, commentRefs);
401+ return _findRefElementInLibrary (
402+ newCodeRef, element, commentRefs, preferredClass);
398403 }
399404
400405 results.remove (null );
401406 // Oh, and someone might have some type parameters or other garbage.
402407 if (results.isEmpty && codeRef.contains (trailingIgnoreStuff)) {
403408 String newCodeRef = codeRef.replaceFirst (trailingIgnoreStuff, '' );
404- return _findRefElementInLibrary (newCodeRef, element, commentRefs);
409+ return _findRefElementInLibrary (
410+ newCodeRef, element, commentRefs, preferredClass);
405411 }
406412
407413 results.remove (null );
408414 // Oh, and someone might have thrown on a 'const' or 'final' in front.
409415 if (results.isEmpty && codeRef.contains (leadingIgnoreStuff)) {
410416 String newCodeRef = codeRef.replaceFirst (leadingIgnoreStuff, '' );
411- return _findRefElementInLibrary (newCodeRef, element, commentRefs);
417+ return _findRefElementInLibrary (
418+ newCodeRef, element, commentRefs, preferredClass);
412419 }
413420
414421 // Maybe this ModelElement has parameters, and this is one of them.
@@ -422,7 +429,7 @@ Element _findRefElementInLibrary(
422429 if (results.isEmpty) {
423430 // Maybe this is local to a class.
424431 // TODO(jcollins-g): tryClasses is a strict subset of the superclass chain. Optimize.
425- List <Class > tryClasses = [_getPreferredClass (element) ];
432+ List <Class > tryClasses = [preferredClass ];
426433 Class realClass = tryClasses.first;
427434 if (element is Inheritable ) {
428435 ModelElement overriddenElement = element.overriddenElement;
@@ -482,7 +489,8 @@ Element _findRefElementInLibrary(
482489 _findRefElementCache.containsKey (codeRefChomped)) {
483490 for (final modelElement in _findRefElementCache[codeRefChomped]) {
484491 if (! _ConsiderIfConstructor (codeRef, modelElement)) continue ;
485- results.add (package.findCanonicalModelElementFor (modelElement.element));
492+ results.add (package.findCanonicalModelElementFor (modelElement.element,
493+ preferredClass: preferredClass));
486494 }
487495 }
488496 results.remove (null );
@@ -492,7 +500,8 @@ Element _findRefElementInLibrary(
492500 for (final modelElement in library.allModelElements) {
493501 if (! _ConsiderIfConstructor (codeRef, modelElement)) continue ;
494502 if (codeRefChomped == modelElement.fullyQualifiedNameWithoutLibrary) {
495- results.add (package.findCanonicalModelElementFor (modelElement.element));
503+ results.add (package.findCanonicalModelElementFor (modelElement.element,
504+ preferredClass: preferredClass));
496505 }
497506 }
498507 }
0 commit comments