@@ -236,24 +236,14 @@ MatchingLinkResult _getMatchingLinkElement(
236
236
237
237
// Try expensive not-scoped lookup.
238
238
if (refElement == null ) {
239
- refElement = _findRefElementInLibrary (codeRef, element);
239
+ refElement = _findRefElementInLibrary (codeRef, element, commentRefs );
240
240
}
241
241
242
242
// This is faster but does not take canonicalization into account; try
243
243
// only as a last resort. TODO(jcollins-g): make analyzer comment references
244
244
// dartdoc-canonicalization-aware?
245
245
if (refElement == null ) {
246
- for (CommentReference ref in commentRefs) {
247
- if (ref.identifier.name == codeRef) {
248
- bool isConstrElement =
249
- ref.identifier.staticElement is ConstructorElement ;
250
- // Constructors are now handled by library search.
251
- if (! isConstrElement) {
252
- refElement = ref.identifier.staticElement;
253
- break ;
254
- }
255
- }
256
- }
246
+ refElement = _getRefElementFromCommentRefs (commentRefs, codeRef);
257
247
}
258
248
259
249
// Did not find it anywhere.
@@ -302,6 +292,21 @@ MatchingLinkResult _getMatchingLinkElement(
302
292
return new MatchingLinkResult (refModelElement, null );
303
293
}
304
294
295
+ /// Given a set of commentRefs, return the one whose name matches the codeRef.
296
+ Element _getRefElementFromCommentRefs (List <CommentReference > commentRefs, String codeRef) {
297
+ for (CommentReference ref in commentRefs) {
298
+ if (ref.identifier.name == codeRef) {
299
+ bool isConstrElement =
300
+ ref.identifier.staticElement is ConstructorElement ;
301
+ // Constructors are now handled by library search.
302
+ if (! isConstrElement) {
303
+ return ref.identifier.staticElement;
304
+ }
305
+ }
306
+ }
307
+ return null ;
308
+ }
309
+
305
310
/// Returns true if this is a constructor we should consider in
306
311
/// _findRefElementInLibrary, or if this isn't a constructor.
307
312
bool _ConsiderIfConstructor (String codeRef, ModelElement modelElement) {
@@ -333,7 +338,7 @@ Map<String, Set<ModelElement>> _findRefElementCache;
333
338
// TODO(jcollins-g): Subcomponents of this function shouldn't be adding nulls to results, strip the
334
339
// removes out that are gratuitous and debug the individual pieces.
335
340
// TODO(jcollins-g): A complex package winds up spending a lot of cycles in here. Optimize.
336
- Element _findRefElementInLibrary (String codeRef, ModelElement element) {
341
+ Element _findRefElementInLibrary (String codeRef, ModelElement element, List < CommentReference > commentRefs ) {
337
342
assert (element.package.allLibrariesAdded);
338
343
339
344
String codeRefChomped = codeRef.replaceFirst (isConstructor, '' );
@@ -345,21 +350,21 @@ Element _findRefElementInLibrary(String codeRef, ModelElement element) {
345
350
// This might be an operator. Strip the operator prefix and try again.
346
351
if (results.isEmpty && codeRef.startsWith ('operator' )) {
347
352
String newCodeRef = codeRef.replaceFirst ('operator' , '' );
348
- return _findRefElementInLibrary (newCodeRef, element);
353
+ return _findRefElementInLibrary (newCodeRef, element, commentRefs );
349
354
}
350
355
351
356
results.remove (null );
352
357
// Oh, and someone might have some type parameters or other garbage.
353
358
if (results.isEmpty && codeRef.contains (trailingIgnoreStuff)) {
354
359
String newCodeRef = codeRef.replaceFirst (trailingIgnoreStuff, '' );
355
- return _findRefElementInLibrary (newCodeRef, element);
360
+ return _findRefElementInLibrary (newCodeRef, element, commentRefs );
356
361
}
357
362
358
363
results.remove (null );
359
364
// Oh, and someone might have thrown on a 'const' or 'final' in front.
360
365
if (results.isEmpty && codeRef.contains (leadingIgnoreStuff)) {
361
366
String newCodeRef = codeRef.replaceFirst (leadingIgnoreStuff, '' );
362
- return _findRefElementInLibrary (newCodeRef, element);
367
+ return _findRefElementInLibrary (newCodeRef, element, commentRefs );
363
368
}
364
369
365
370
// Maybe this ModelElement has parameters, and this is one of them.
@@ -515,8 +520,20 @@ Element _findRefElementInLibrary(String codeRef, ModelElement element) {
515
520
results.removeWhere ((r) => ! r.fullyQualifiedName.startsWith (startName));
516
521
}
517
522
}
518
- // TODO(jcollins-g): further disambiguations based on package information?
519
523
524
+ // TODO(jcollins-g): As a last resort, try disambiguation with commentRefs.
525
+ // Maybe one of these is the same as what's resolvable with
526
+ // the analyzer, and if so, drop the others. We can't
527
+ // do this in reverse order because commentRefs don't know
528
+ // about dartdoc canonicalization.
529
+ if (results.length > 1 ) {
530
+ Element refElement = _getRefElementFromCommentRefs (commentRefs, codeRef);
531
+ if (results.any ((me) => me.element == refElement)) {
532
+ results.removeWhere ((me) => me.element != refElement);
533
+ }
534
+ }
535
+
536
+ // TODO(jcollins-g): further disambiguations based on package information?
520
537
if (results.isEmpty) {
521
538
result = null ;
522
539
} else if (results.length == 1 ) {
0 commit comments