@@ -251,7 +251,9 @@ MatchingLinkResult _getMatchingLinkElement(
251
251
252
252
// Try expensive not-scoped lookup.
253
253
if (refElement == null ) {
254
- refElement = _findRefElementInLibrary (codeRef, element, commentRefs);
254
+ Class preferredClass = _getPreferredClass (element);
255
+ refElement =
256
+ _findRefElementInLibrary (codeRef, element, commentRefs, preferredClass);
255
257
}
256
258
257
259
// This is faster but does not take canonicalization into account; try
@@ -281,7 +283,7 @@ MatchingLinkResult _getMatchingLinkElement(
281
283
if (searchElement is Member )
282
284
searchElement = Package .getBasestElement (refElement);
283
285
284
- Class preferredClass = _getPreferredClass (element);
286
+ final Class preferredClass = _getPreferredClass (element);
285
287
ModelElement refModelElement = element.package.findCanonicalModelElementFor (
286
288
searchElement,
287
289
preferredClass: preferredClass);
@@ -357,6 +359,8 @@ bool _ConsiderIfConstructor(String codeRef, ModelElement modelElement) {
357
359
Constructor aConstructor = modelElement;
358
360
List <String > codeRefParts = codeRef.split ('.' );
359
361
if (codeRefParts.length > 1 ) {
362
+ // Pick the last two parts, in case a specific library was part of the
363
+ // codeRef.
360
364
if (codeRefParts[codeRefParts.length - 1 ] ==
361
365
codeRefParts[codeRefParts.length - 2 ]) {
362
366
// Foobar.Foobar -- assume they really do mean the constructor for this class.
@@ -380,8 +384,8 @@ Map<String, Set<ModelElement>> _findRefElementCache;
380
384
// TODO(jcollins-g): Subcomponents of this function shouldn't be adding nulls to results, strip the
381
385
// removes out that are gratuitous and debug the individual pieces.
382
386
// 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 ) {
385
389
assert (element != null );
386
390
assert (element.package.allLibrariesAdded);
387
391
@@ -394,21 +398,24 @@ Element _findRefElementInLibrary(
394
398
// This might be an operator. Strip the operator prefix and try again.
395
399
if (results.isEmpty && codeRef.startsWith ('operator' )) {
396
400
String newCodeRef = codeRef.replaceFirst ('operator' , '' );
397
- return _findRefElementInLibrary (newCodeRef, element, commentRefs);
401
+ return _findRefElementInLibrary (
402
+ newCodeRef, element, commentRefs, preferredClass);
398
403
}
399
404
400
405
results.remove (null );
401
406
// Oh, and someone might have some type parameters or other garbage.
402
407
if (results.isEmpty && codeRef.contains (trailingIgnoreStuff)) {
403
408
String newCodeRef = codeRef.replaceFirst (trailingIgnoreStuff, '' );
404
- return _findRefElementInLibrary (newCodeRef, element, commentRefs);
409
+ return _findRefElementInLibrary (
410
+ newCodeRef, element, commentRefs, preferredClass);
405
411
}
406
412
407
413
results.remove (null );
408
414
// Oh, and someone might have thrown on a 'const' or 'final' in front.
409
415
if (results.isEmpty && codeRef.contains (leadingIgnoreStuff)) {
410
416
String newCodeRef = codeRef.replaceFirst (leadingIgnoreStuff, '' );
411
- return _findRefElementInLibrary (newCodeRef, element, commentRefs);
417
+ return _findRefElementInLibrary (
418
+ newCodeRef, element, commentRefs, preferredClass);
412
419
}
413
420
414
421
// Maybe this ModelElement has parameters, and this is one of them.
@@ -422,7 +429,7 @@ Element _findRefElementInLibrary(
422
429
if (results.isEmpty) {
423
430
// Maybe this is local to a class.
424
431
// TODO(jcollins-g): tryClasses is a strict subset of the superclass chain. Optimize.
425
- List <Class > tryClasses = [_getPreferredClass (element) ];
432
+ List <Class > tryClasses = [preferredClass ];
426
433
Class realClass = tryClasses.first;
427
434
if (element is Inheritable ) {
428
435
ModelElement overriddenElement = element.overriddenElement;
@@ -482,7 +489,8 @@ Element _findRefElementInLibrary(
482
489
_findRefElementCache.containsKey (codeRefChomped)) {
483
490
for (final modelElement in _findRefElementCache[codeRefChomped]) {
484
491
if (! _ConsiderIfConstructor (codeRef, modelElement)) continue ;
485
- results.add (package.findCanonicalModelElementFor (modelElement.element));
492
+ results.add (package.findCanonicalModelElementFor (modelElement.element,
493
+ preferredClass: preferredClass));
486
494
}
487
495
}
488
496
results.remove (null );
@@ -492,7 +500,8 @@ Element _findRefElementInLibrary(
492
500
for (final modelElement in library.allModelElements) {
493
501
if (! _ConsiderIfConstructor (codeRef, modelElement)) continue ;
494
502
if (codeRefChomped == modelElement.fullyQualifiedNameWithoutLibrary) {
495
- results.add (package.findCanonicalModelElementFor (modelElement.element));
503
+ results.add (package.findCanonicalModelElementFor (modelElement.element,
504
+ preferredClass: preferredClass));
496
505
}
497
506
}
498
507
}
0 commit comments