22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5- // ignore_for_file: analyzer_use_new_elements
6-
7- import 'package:analyzer/dart/element/element.dart' ;
5+ import 'package:analyzer/dart/element/element2.dart' ;
6+ // ignore: implementation_imports
7+ import 'package:analyzer/src/ dart/element/element.dart' ;
88import 'package:dartdoc/src/model/model.dart' ;
99import 'package:dartdoc/src/warnings.dart' ;
1010
11+ const int _separatorChar = 0x3B ;
12+
1113/// Searches [PackageGraph.libraryExports] for a public, documented library
1214/// which exports this [ModelElement] , ideally in its library's package.
1315Library ? canonicalLibraryCandidate (ModelElement modelElement) {
1416 var thisAndExported =
15- modelElement.packageGraph.libraryExports[modelElement.library.element ];
17+ modelElement.packageGraph.libraryExports[modelElement.library.element2 ];
1618 if (thisAndExported == null ) {
1719 return null ;
1820 }
1921
20- // Since we're looking for a library, find the [Element] immediately
21- // contained by a [CompilationUnitElement] in the tree.
22- var topLevelElement = modelElement.element;
23- while (topLevelElement.enclosingElement3 is ! LibraryElement &&
24- topLevelElement.enclosingElement3 is ! CompilationUnitElement &&
25- topLevelElement.enclosingElement3 != null ) {
26- topLevelElement = topLevelElement.enclosingElement3! ;
22+ // Since we're looking for a library, go up in the tree until we find it.
23+ var topLevelElement = modelElement.element2;
24+ while (topLevelElement.enclosingElement2 is ! LibraryElement2 &&
25+ topLevelElement.enclosingElement2 != null ) {
26+ topLevelElement = topLevelElement.enclosingElement2! ;
2727 }
28- var topLevelElementName = topLevelElement.name ;
28+ var topLevelElementName = topLevelElement.name3 ;
2929 if (topLevelElementName == null ) {
3030 // Any member of an unnamed extension is not public, and has no
3131 // canonical library.
@@ -36,9 +36,9 @@ Library? canonicalLibraryCandidate(ModelElement modelElement) {
3636 if (! l.isPublic) return false ;
3737 if (l.package.documentedWhere == DocumentLocation .missing) return false ;
3838 if (modelElement is Library ) return true ;
39- var lookup = l.element .exportNamespace.definedNames [topLevelElementName];
39+ var lookup = l.element2 .exportNamespace.definedNames2 [topLevelElementName];
4040 return topLevelElement ==
41- (lookup is PropertyAccessorElement ? lookup.variable2 : lookup);
41+ (lookup is PropertyAccessorElement2 ? lookup.variable3 : lookup);
4242 }).toList (growable: true );
4343
4444 if (candidateLibraries.isEmpty) {
@@ -58,7 +58,7 @@ Library? canonicalLibraryCandidate(ModelElement modelElement) {
5858 }
5959
6060 var topLevelModelElement =
61- ModelElement .forElement (topLevelElement, modelElement.packageGraph);
61+ ModelElement .forElement2 (topLevelElement, modelElement.packageGraph);
6262
6363 return _Canonicalization (topLevelModelElement)
6464 .canonicalLibraryCandidate (candidateLibraries);
@@ -74,10 +74,57 @@ final class _Canonicalization {
7474
7575 _Canonicalization (this ._element);
7676
77+ /// Append an encoded form of the given [component] to the given [buffer] .
78+ void _encode (StringBuffer buffer, String component) {
79+ var length = component.length;
80+ for (var i = 0 ; i < length; i++ ) {
81+ var currentChar = component.codeUnitAt (i);
82+ if (currentChar == _separatorChar) {
83+ buffer.writeCharCode (_separatorChar);
84+ }
85+ buffer.writeCharCode (currentChar);
86+ }
87+ }
88+
89+ // Copied from package analyzer ElementLocationImpl.fromElement.
90+ String _getElementLocation (Element2 element) {
91+ var components = < String > [];
92+ Element2 ? ancestor = element;
93+ while (ancestor != null ) {
94+ if (ancestor is ! ElementImpl2 ) {
95+ if (ancestor is LibraryElementImpl ) {
96+ components.insert (0 , ancestor.identifier);
97+ } else if (ancestor is AugmentableElement ) {
98+ components.insert (0 , ancestor.identifier);
99+ } else {
100+ throw Exception ('${ancestor .runtimeType } is not an ElementImpl2' );
101+ }
102+ ancestor = ancestor.enclosingElement2;
103+ } else {
104+ components.insert (0 , ancestor.identifier);
105+ if (ancestor is LocalFunctionElementImpl ) {
106+ ancestor = (ancestor.wrappedElement.enclosingElement2
107+ as ExecutableElementImpl )
108+ .element;
109+ } else {
110+ ancestor = ancestor.enclosingElement2;
111+ }
112+ }
113+ }
114+ var buffer = StringBuffer ();
115+ var length = components.length;
116+ for (var i = 0 ; i < length; i++ ) {
117+ if (i > 0 ) {
118+ buffer.writeCharCode (_separatorChar);
119+ }
120+ _encode (buffer, components[i]);
121+ }
122+ return buffer.toString ();
123+ }
124+
77125 /// Calculates a candidate for the canonical library of [_element] , among [libraries] .
78126 Library canonicalLibraryCandidate (Iterable <Library > libraries) {
79- var locationPieces = _element.element.location
80- .toString ()
127+ var locationPieces = _getElementLocation (_element.element2)
81128 .split (_locationSplitter)
82129 .where ((s) => s.isNotEmpty)
83130 .toSet ();
0 commit comments