@@ -15,6 +15,7 @@ import 'package:dartdoc/src/tuple.dart';
1515import 'package:dartdoc/src/warnings.dart' ;
1616import 'package:html/parser.dart' show parse;
1717import 'package:markdown/markdown.dart' as md;
18+ import 'package:quiver/iterables.dart' as quiverIterables;
1819
1920const validHtmlTags = const [
2021 "a" ,
@@ -547,8 +548,7 @@ class _MarkdownCommentReference {
547548 if (codeRefChomped == modelElement.fullyQualifiedNameWithoutLibrary ||
548549 (modelElement is Library &&
549550 codeRefChomped == modelElement.fullyQualifiedName)) {
550- results.add (
551- packageGraph.findCanonicalModelElementFor (modelElement.element));
551+ _addCanonicalResult (modelElement, null );
552552 }
553553 }
554554 }
@@ -559,9 +559,7 @@ class _MarkdownCommentReference {
559559 if (library.modelElementsNameMap.containsKey (codeRefChomped)) {
560560 for (final modelElement in library.modelElementsNameMap[codeRefChomped]) {
561561 if (! _ConsiderIfConstructor (modelElement)) continue ;
562- results.add (packageGraph.findCanonicalModelElementFor (
563- modelElement.element,
564- preferredClass: preferredClass));
562+ _addCanonicalResult (modelElement, preferredClass);
565563 }
566564 }
567565 }
@@ -579,11 +577,11 @@ class _MarkdownCommentReference {
579577 // might make no sense. Instead, use the enclosing class from the
580578 // element in [packageGraph.findRefElementCache], because that element's
581579 // enclosing class will be preferred from [codeRefChomped]'s perspective.
582- results. add (packageGraph. findCanonicalModelElementFor (
583- modelElement.element ,
584- preferredClass : modelElement.enclosingElement is Class
580+ _addCanonicalResult (
581+ modelElement,
582+ modelElement.enclosingElement is Class
585583 ? modelElement.enclosingElement
586- : null )) ;
584+ : null );
587585 }
588586 }
589587 }
@@ -639,6 +637,12 @@ class _MarkdownCommentReference {
639637 }
640638 }
641639
640+ // Add a result, but make it canonical.
641+ void _addCanonicalResult (ModelElement modelElement, Class tryClass) {
642+ results.add (packageGraph.findCanonicalModelElementFor (modelElement.element,
643+ preferredClass: tryClass));
644+ }
645+
642646 /// _getResultsForClass assumes codeRefChomped might be a member of tryClass (inherited or not)
643647 /// and will add to [results]
644648 void _getResultsForClass (Class tryClass) {
@@ -653,8 +657,7 @@ class _MarkdownCommentReference {
653657 } else {
654658 // People like to use 'this' in docrefs too.
655659 if (codeRef == 'this' ) {
656- results
657- .add (packageGraph.findCanonicalModelElementFor (tryClass.element));
660+ _addCanonicalResult (tryClass, null );
658661 } else {
659662 // TODO(jcollins-g): get rid of reimplementation of identifier resolution
660663 // or integrate into ModelElement in a simpler way.
@@ -666,62 +669,42 @@ class _MarkdownCommentReference {
666669 // Fortunately superChains are short, but optimize this if it matters.
667670 superChain.addAll (tryClass.superChain.map ((t) => t.element as Class ));
668671 for (final c in superChain) {
669- // TODO(jcollins-g): add a hash-map-enabled lookup function to Class?
670- for (final modelElement in c.allModelElements) {
671- if (! _ConsiderIfConstructor (modelElement)) continue ;
672- String namePart = modelElement.fullyQualifiedName.split ('.' ).last;
673- if (modelElement is Accessor ) {
674- // TODO(jcollins-g): Individual classes should be responsible for
675- // this name comparison munging.
676- namePart = namePart.split ('=' ).first;
677- }
678- // TODO(jcollins-g): fix operators so we can use 'name' here or similar.
679- if (codeRefChomped == namePart) {
680- results.add (packageGraph.findCanonicalModelElementFor (
681- modelElement.element,
682- preferredClass: tryClass));
683- continue ;
684- }
685- // Handle non-documented class documentation being imported into a
686- // documented class when it refers to itself (with help from caller's
687- // iteration on tryClasses).
688- // TODO(jcollins-g): Fix partial qualifications in _findRefElementInLibrary so it can tell
689- // when it is referenced from a non-documented element?
690- // TODO(jcollins-g): We could probably check this early.
691- if (codeRefChompedParts.first == c.name &&
692- codeRefChompedParts.last == namePart) {
693- results.add (packageGraph.findCanonicalModelElementFor (
694- modelElement.element,
695- preferredClass: tryClass));
696- continue ;
697- }
698- if (modelElement is Constructor ) {
699- // Constructor names don't include the class, so we might miss them in the above search.
700- if (codeRefChompedParts.length > 1 ) {
701- String codeRefClass =
702- codeRefChompedParts[codeRefChompedParts.length - 2 ];
703- String codeRefConstructor = codeRefChompedParts.last;
704- if (codeRefClass == c.name &&
705- codeRefConstructor ==
706- modelElement.fullyQualifiedName.split ('.' ).last) {
707- results.add (packageGraph.findCanonicalModelElementFor (
708- modelElement.element,
709- preferredClass: tryClass));
710- continue ;
711- }
712- }
713- }
714- }
715- results.remove (null );
672+ _getResultsForSuperChainElement (c, tryClass);
716673 if (results.isNotEmpty) break ;
717- if (c.fullyQualifiedNameWithoutLibrary == codeRefChomped) {
718- results.add (c);
719- break ;
720- }
721674 }
722675 }
723676 }
724677 }
678+
679+ /// Get any possible results for this class in the superChain. Returns
680+ /// true if we found something.
681+ void _getResultsForSuperChainElement (Class c, Class tryClass) {
682+ Iterable <ModelElement > membersToCheck;
683+ membersToCheck = (c.allModelElementsByNamePart[codeRefChomped] ?? [])
684+ .where ((m) => _ConsiderIfConstructor (m));
685+ for (final ModelElement modelElement in membersToCheck) {
686+ // [thing], a member of this class
687+ _addCanonicalResult (modelElement, tryClass);
688+ }
689+ membersToCheck = (c.allModelElementsByNamePart[codeRefChompedParts.last] ??
690+ < ModelElement > [])
691+ .where ((m) => _ConsiderIfConstructor (m));
692+ if (codeRefChompedParts.first == c.name) {
693+ // [Foo...thing], a member of this class (possibly a parameter).
694+ membersToCheck.map ((m) => _addCanonicalResult (m, tryClass));
695+ } else if (codeRefChompedParts.length > 1 &&
696+ codeRefChompedParts[codeRefChompedParts.length - 2 ] == c.name) {
697+ // [....Foo.thing], a member of this class partially specified.
698+ membersToCheck
699+ .whereType <Constructor >()
700+ .map ((m) => _addCanonicalResult (m, tryClass));
701+ }
702+ results.remove (null );
703+ if (results.isNotEmpty) return ;
704+ if (c.fullyQualifiedNameWithoutLibrary == codeRefChomped) {
705+ results.add (c);
706+ }
707+ }
725708}
726709
727710String _linkDocReference (String codeRef, Warnable warnable,
0 commit comments