@@ -198,8 +198,9 @@ MatchingLinkResult _getMatchingLinkElement(
198
198
// Try expensive not-scoped lookup.
199
199
if (refModelElement == null && element is ModelElement ) {
200
200
Class preferredClass = _getPreferredClass (element);
201
- refModelElement =
202
- new _MarkdownCommentReference (codeRef, element, commentRefs, preferredClass).computeReferredElement ();
201
+ refModelElement = new _MarkdownCommentReference (
202
+ codeRef, element, commentRefs, preferredClass)
203
+ .computeReferredElement ();
203
204
}
204
205
205
206
// Did not find it anywhere.
@@ -269,32 +270,40 @@ Element _getRefElementFromCommentRefs(
269
270
return null ;
270
271
}
271
272
272
-
273
273
/// Represents a single comment reference.
274
274
class _MarkdownCommentReference {
275
275
/// The code reference text.
276
276
final String codeRef;
277
+
277
278
/// The element containing the code reference.
278
279
final Warnable element;
280
+
279
281
/// A list of [CommentReference] s from the analyzer.
280
282
final List <CommentReference > commentRefs;
283
+
281
284
/// Disambiguate inheritance with this class.
282
285
final Class preferredClass;
286
+
283
287
/// Current results. Input/output of all _find and _reduce methods.
284
288
Set <ModelElement > results;
289
+
285
290
/// codeRef with any leading constructor string, stripped.
286
291
String codeRefChomped;
292
+
287
293
/// Library associated with this element.
288
294
Library library;
295
+
289
296
/// PackageGraph associated with this element.
290
297
PackageGraph packageGraph;
291
298
292
- _MarkdownCommentReference (this .codeRef, this .element, this .commentRefs, this .preferredClass) {
299
+ _MarkdownCommentReference (
300
+ this .codeRef, this .element, this .commentRefs, this .preferredClass) {
293
301
assert (element != null );
294
302
assert (element.packageGraph.allLibrariesAdded);
295
303
296
304
codeRefChomped = codeRef.replaceFirst (isConstructor, '' );
297
- library = element is ModelElement ? (element as ModelElement ).library : null ;
305
+ library =
306
+ element is ModelElement ? (element as ModelElement ).library : null ;
298
307
packageGraph = library.packageGraph;
299
308
}
300
309
@@ -334,7 +343,8 @@ class _MarkdownCommentReference {
334
343
// This could conceivably be a reference to an enum member. They don't show up in allModelElements.
335
344
_findEnumReferences,
336
345
// Use the analyzer to resolve a comment reference.
337
- _findAnalyzerReferences]) {
346
+ _findAnalyzerReferences
347
+ ]) {
338
348
findMethod ();
339
349
// Remove any "null" objects after each step of trying to add to results.
340
350
// TODO(jcollins-g): Eliminate all situations where nulls can be added
@@ -382,13 +392,20 @@ class _MarkdownCommentReference {
382
392
if (! results.every ((r) => r is Parameter )) {
383
393
element.warn (PackageWarning .ambiguousDocReference,
384
394
message:
385
- "[$codeRef ] => ${results .map ((r ) => "'${r .fullyQualifiedName }'" ).join (", " )}" );
395
+ "[$codeRef ] => ${results .map ((r ) => "'${r .fullyQualifiedName }'" ).join (", " )}" );
386
396
}
387
397
result = results.first;
388
398
}
389
399
return result;
390
400
}
391
401
402
+ List <String > _codeRefParts;
403
+ List <String > get codeRefParts => _codeRefParts ?? = codeRef.split ('.' );
404
+
405
+ List <String > _codeRefChompedParts;
406
+ List <String > get codeRefChompedParts =>
407
+ _codeRefChompedParts ?? = codeRefChomped.split ('.' );
408
+
392
409
/// Returns true if this is a constructor we should consider due to its
393
410
/// name and the code reference, or if this isn't a constructor. False
394
411
/// otherwise.
@@ -397,7 +414,6 @@ class _MarkdownCommentReference {
397
414
if (modelElement is ! Constructor ) return true ;
398
415
if (codeRef.contains (isConstructor)) return true ;
399
416
Constructor aConstructor = modelElement;
400
- List <String > codeRefParts = codeRef.split ('.' );
401
417
if (codeRefParts.length > 1 ) {
402
418
// Pick the last two parts, in case a specific library was part of the
403
419
// codeRef.
@@ -434,18 +450,20 @@ class _MarkdownCommentReference {
434
450
435
451
void _reducePreferLibrariesInLocalImportExportGraph () {
436
452
if (results.any (
437
- (r) => library.packageImportedExportedLibraries.contains (r.library))) {
453
+ (r) => library.packageImportedExportedLibraries.contains (r.library))) {
438
454
results.removeWhere (
439
- (r) => ! library.packageImportedExportedLibraries.contains (r.library));
455
+ (r) => ! library.packageImportedExportedLibraries.contains (r.library));
440
456
}
441
457
}
442
458
443
459
void _reducePreferResultsAccessibleInSameLibrary () {
444
460
// TODO(jcollins-g): we could have saved ourselves some work by using the analyzer
445
461
// to search the namespace, somehow. Do that instead.
446
- if (element is ModelElement && results.any ((r) => r.element.isAccessibleIn ((element as ModelElement ).library.element))) {
447
- results.removeWhere (
448
- (r) => ! r.element.isAccessibleIn ((element as ModelElement ).library.element));
462
+ if (element is ModelElement &&
463
+ results.any ((r) => r.element
464
+ .isAccessibleIn ((element as ModelElement ).library.element))) {
465
+ results.removeWhere ((r) =>
466
+ ! r.element.isAccessibleIn ((element as ModelElement ).library.element));
449
467
}
450
468
}
451
469
@@ -464,53 +482,55 @@ class _MarkdownCommentReference {
464
482
void _findTypeParameters () {
465
483
if (element is TypeParameters ) {
466
484
results.addAll ((element as TypeParameters ).typeParameters.where ((p) =>
467
- p.name == codeRefChomped || codeRefChomped.startsWith ("${p .name }." )));
485
+ p.name == codeRefChomped || codeRefChomped.startsWith ("${p .name }." )));
468
486
}
469
487
}
470
488
471
489
void _findParameters () {
472
490
if (element is ModelElement ) {
473
491
results.addAll ((element as ModelElement ).allParameters.where ((p) =>
474
- p.name == codeRefChomped || codeRefChomped.startsWith ("${p .name }." )));
492
+ p.name == codeRefChomped || codeRefChomped.startsWith ("${p .name }." )));
475
493
}
476
494
}
477
495
478
496
void _findWithoutLeadingIgnoreStuff () {
479
497
if (codeRef.contains (leadingIgnoreStuff)) {
480
498
String newCodeRef = codeRef.replaceFirst (leadingIgnoreStuff, '' );
481
499
results.add (new _MarkdownCommentReference (
482
- newCodeRef, element, commentRefs, preferredClass).computeReferredElement ());
500
+ newCodeRef, element, commentRefs, preferredClass)
501
+ .computeReferredElement ());
483
502
}
484
503
}
485
504
486
505
void _findWithoutTrailingIgnoreStuff () {
487
506
if (codeRef.contains (trailingIgnoreStuff)) {
488
507
String newCodeRef = codeRef.replaceFirst (trailingIgnoreStuff, '' );
489
508
results.add (new _MarkdownCommentReference (
490
- newCodeRef, element, commentRefs, preferredClass).computeReferredElement ());
509
+ newCodeRef, element, commentRefs, preferredClass)
510
+ .computeReferredElement ());
491
511
}
492
512
}
493
513
494
514
void _findWithoutOperatorPrefix () {
495
515
if (codeRef.startsWith (operatorPrefix)) {
496
516
String newCodeRef = codeRef.replaceFirst (operatorPrefix, '' );
497
517
results.add (new _MarkdownCommentReference (
498
- newCodeRef, element, commentRefs, preferredClass).computeReferredElement ());
518
+ newCodeRef, element, commentRefs, preferredClass)
519
+ .computeReferredElement ());
499
520
}
500
521
}
501
522
502
523
void _findEnumReferences () {
503
524
// TODO(jcollins-g): Put enum members in allModelElements with useful hrefs without blowing up other assumptions about what that means.
504
525
// TODO(jcollins-g): This doesn't provide good warnings if an enum and class have the same name in different libraries in the same package. Fix that.
505
- List <String > codeRefChompedParts = codeRefChomped.split ('.' );
506
526
if (codeRefChompedParts.length >= 2 ) {
507
527
String maybeEnumName = codeRefChompedParts
508
528
.sublist (0 , codeRefChompedParts.length - 1 )
509
529
.join ('.' );
510
530
String maybeEnumMember = codeRefChompedParts.last;
511
531
if (packageGraph.findRefElementCache.containsKey (maybeEnumName)) {
512
532
for (final modelElement
513
- in packageGraph.findRefElementCache[maybeEnumName]) {
533
+ in packageGraph.findRefElementCache[maybeEnumName]) {
514
534
if (modelElement is Enum ) {
515
535
if (modelElement.constants.any ((e) => e.name == maybeEnumMember)) {
516
536
results.add (modelElement);
@@ -525,7 +545,7 @@ class _MarkdownCommentReference {
525
545
void _findGlobalWithinRefElementCache () {
526
546
if (packageGraph.findRefElementCache.containsKey (codeRefChomped)) {
527
547
for (final modelElement
528
- in packageGraph.findRefElementCache[codeRefChomped]) {
548
+ in packageGraph.findRefElementCache[codeRefChomped]) {
529
549
if (codeRefChomped == modelElement.fullyQualifiedNameWithoutLibrary ||
530
550
(modelElement is Library &&
531
551
codeRefChomped == modelElement.fullyQualifiedName)) {
@@ -552,10 +572,10 @@ class _MarkdownCommentReference {
552
572
// We now need the ref element cache to keep from repeatedly searching [Package.allModelElements].
553
573
// But if not, look for a fully qualified match. (That only makes sense
554
574
// if the codeRef might be qualified, and contains periods.)
555
- if (
556
- codeRefChomped.contains ('.' ) &&
575
+ if (codeRefChomped.contains ('.' ) &&
557
576
packageGraph.findRefElementCache.containsKey (codeRefChomped)) {
558
- for (final ModelElement modelElement in packageGraph.findRefElementCache[codeRefChomped]) {
577
+ for (final ModelElement modelElement
578
+ in packageGraph.findRefElementCache[codeRefChomped]) {
559
579
if (! _ConsiderIfConstructor (modelElement)) continue ;
560
580
// For fully qualified matches, the original preferredClass passed
561
581
// might make no sense. Instead, use the enclosing class from the
@@ -576,10 +596,12 @@ class _MarkdownCommentReference {
576
596
List <Class > tryClasses = [preferredClass];
577
597
Class realClass = tryClasses.first;
578
598
if (element is Inheritable ) {
579
- Inheritable overriddenElement = (element as Inheritable ).overriddenElement;
599
+ Inheritable overriddenElement =
600
+ (element as Inheritable ).overriddenElement;
580
601
while (overriddenElement != null ) {
581
602
tryClasses.add (
582
- ((element as Inheritable ).overriddenElement as EnclosedElement ).enclosingElement);
603
+ ((element as Inheritable ).overriddenElement as EnclosedElement )
604
+ .enclosingElement);
583
605
overriddenElement = overriddenElement.overriddenElement;
584
606
}
585
607
}
@@ -594,7 +616,7 @@ class _MarkdownCommentReference {
594
616
595
617
if (results.isEmpty && realClass != null ) {
596
618
for (Class superClass
597
- in realClass.publicSuperChain.map ((et) => et.element as Class )) {
619
+ in realClass.publicSuperChain.map ((et) => et.element as Class )) {
598
620
if (! tryClasses.contains (superClass)) {
599
621
_getResultsForClass (superClass);
600
622
}
@@ -613,7 +635,8 @@ class _MarkdownCommentReference {
613
635
if (refModelElement is Accessor ) {
614
636
refModelElement = (refModelElement as Accessor ).enclosingCombo;
615
637
}
616
- refModelElement = refModelElement.canonicalModelElement ?? refModelElement;
638
+ refModelElement =
639
+ refModelElement.canonicalModelElement ?? refModelElement;
617
640
results.add (refModelElement);
618
641
}
619
642
}
@@ -626,13 +649,14 @@ class _MarkdownCommentReference {
626
649
if ((tryClass.modelType.typeArguments.map ((e) => e.name))
627
650
.contains (codeRefChomped)) {
628
651
results.add ((tryClass.modelType.typeArguments.firstWhere (
629
- (e) => e.name == codeRefChomped && e is DefinedElementType )
630
- as DefinedElementType )
652
+ (e) => e.name == codeRefChomped && e is DefinedElementType )
653
+ as DefinedElementType )
631
654
.element);
632
655
} else {
633
656
// People like to use 'this' in docrefs too.
634
657
if (codeRef == 'this' ) {
635
- results.add (packageGraph.findCanonicalModelElementFor (tryClass.element));
658
+ results
659
+ .add (packageGraph.findCanonicalModelElementFor (tryClass.element));
636
660
} else {
637
661
// TODO(jcollins-g): get rid of reimplementation of identifier resolution
638
662
// or integrate into ModelElement in a simpler way.
@@ -643,7 +667,6 @@ class _MarkdownCommentReference {
643
667
// TODO(jcollins-g): This makes our caller ~O(n^2) vs length of superChain.
644
668
// Fortunately superChains are short, but optimize this if it matters.
645
669
superChain.addAll (tryClass.superChain.map ((t) => t.element as Class ));
646
- List <String > codeRefParts = codeRefChomped.split ('.' );
647
670
for (final c in superChain) {
648
671
// TODO(jcollins-g): add a hash-map-enabled lookup function to Class?
649
672
for (final modelElement in c.allModelElements) {
@@ -667,17 +690,19 @@ class _MarkdownCommentReference {
667
690
// TODO(jcollins-g): Fix partial qualifications in _findRefElementInLibrary so it can tell
668
691
// when it is referenced from a non-documented element?
669
692
// TODO(jcollins-g): We could probably check this early.
670
- if (codeRefParts.first == c.name && codeRefParts.last == namePart) {
693
+ if (codeRefChompedParts.first == c.name &&
694
+ codeRefChompedParts.last == namePart) {
671
695
results.add (packageGraph.findCanonicalModelElementFor (
672
696
modelElement.element,
673
697
preferredClass: tryClass));
674
698
continue ;
675
699
}
676
700
if (modelElement is Constructor ) {
677
701
// Constructor names don't include the class, so we might miss them in the above search.
678
- if (codeRefParts.length > 1 ) {
679
- String codeRefClass = codeRefParts[codeRefParts.length - 2 ];
680
- String codeRefConstructor = codeRefParts.last;
702
+ if (codeRefChompedParts.length > 1 ) {
703
+ String codeRefClass =
704
+ codeRefChompedParts[codeRefChompedParts.length - 2 ];
705
+ String codeRefConstructor = codeRefChompedParts.last;
681
706
if (codeRefClass == c.name &&
682
707
codeRefConstructor ==
683
708
modelElement.fullyQualifiedName.split ('.' ).last) {
0 commit comments