88
99import 'package:analysis_server/src/protocol_server.dart' ;
1010import 'package:analyzer/dart/element/element.dart' as engine;
11+ import 'package:analyzer/dart/element/element2.dart' as engine;
1112import 'package:analyzer/dart/element/type.dart' ;
13+ import 'package:analyzer/src/utilities/extensions/element.dart' ;
1214import 'package:path/path.dart' as path;
1315
1416/// Return a protocol [Element] corresponding to the given [engine.Element] .
@@ -38,6 +40,33 @@ Element convertElement(engine.Element element) {
3840 );
3941}
4042
43+ Element convertElement2 (engine.Element2 element) {
44+ var kind = convertElementToElementKind2 (element);
45+ var name = getElementDisplayName2 (element);
46+ var elementTypeParameters = _getTypeParametersString2 (element);
47+ var aliasedType = getAliasedTypeString2 (element);
48+ var elementParameters = _getParametersString2 (element);
49+ var elementReturnType = getReturnTypeString2 (element);
50+ return Element (
51+ kind,
52+ name,
53+ Element .makeFlags (
54+ isPrivate: element.isPrivate,
55+ isDeprecated: (element is engine.Annotatable ) &&
56+ (element as engine.Annotatable ).metadata2.hasDeprecated,
57+ isAbstract: _isAbstract2 (element),
58+ isConst: _isConst2 (element),
59+ isFinal: _isFinal2 (element),
60+ isStatic: _isStatic2 (element),
61+ ),
62+ location: newLocation_fromElement2 (element),
63+ typeParameters: elementTypeParameters,
64+ aliasedType: aliasedType,
65+ parameters: elementParameters,
66+ returnType: elementReturnType,
67+ );
68+ }
69+
4170/// Return a protocol [ElementKind] corresponding to the given
4271/// [engine.ElementKind] .
4372///
@@ -130,6 +159,19 @@ ElementKind convertElementToElementKind(engine.Element element) {
130159 return convertElementKind (element.kind);
131160}
132161
162+ /// Return an [ElementKind] corresponding to the given [engine.Element2] .
163+ ElementKind convertElementToElementKind2 (engine.Element2 element) {
164+ if (element is engine.EnumElement2 ) {
165+ return ElementKind .ENUM ;
166+ } else if (element is engine.MixinElement2 ) {
167+ return ElementKind .MIXIN ;
168+ }
169+ if (element is engine.FieldElement2 && element.isEnumConstant) {
170+ return ElementKind .ENUM_CONSTANT ;
171+ }
172+ return convertElementKind (element.kind);
173+ }
174+
133175String getElementDisplayName (engine.Element element) {
134176 if (element is engine.CompilationUnitElement ) {
135177 return path.basename (element.source.fullName);
@@ -138,6 +180,14 @@ String getElementDisplayName(engine.Element element) {
138180 }
139181}
140182
183+ String getElementDisplayName2 (engine.Element2 element) {
184+ if (element is engine.LibraryFragment ) {
185+ return path.basename ((element as engine.LibraryFragment ).source.fullName);
186+ } else {
187+ return element.displayName;
188+ }
189+ }
190+
141191String ? _getParametersString (engine.Element element) {
142192 // TODO(scheglov): expose the corresponding feature from ExecutableElement
143193 List <engine.ParameterElement > parameters;
@@ -187,6 +237,55 @@ String? _getParametersString(engine.Element element) {
187237 return '($sb )' ;
188238}
189239
240+ String ? _getParametersString2 (engine.Element2 element) {
241+ // TODO(scheglov): expose the corresponding feature from ExecutableElement
242+ List <engine.FormalParameterElement > parameters;
243+ if (element is engine.ExecutableElement2 ) {
244+ // valid getters don't have parameters
245+ if (element.kind == engine.ElementKind .GETTER &&
246+ element.formalParameters.isEmpty) {
247+ return null ;
248+ }
249+ parameters = element.formalParameters.toList ();
250+ } else if (element is engine.TypeAliasElement2 ) {
251+ var aliasedType = element.aliasedType;
252+ if (aliasedType is FunctionType ) {
253+ parameters = aliasedType.formalParameters.toList ();
254+ } else {
255+ return null ;
256+ }
257+ } else {
258+ return null ;
259+ }
260+
261+ parameters.sort (_preferRequiredParams2);
262+
263+ var sb = StringBuffer ();
264+ var closeOptionalString = '' ;
265+ for (var parameter in parameters) {
266+ if (sb.isNotEmpty) {
267+ sb.write (', ' );
268+ }
269+ if (closeOptionalString.isEmpty) {
270+ if (parameter.isNamed) {
271+ sb.write ('{' );
272+ closeOptionalString = '}' ;
273+ } else if (parameter.isOptionalPositional) {
274+ sb.write ('[' );
275+ closeOptionalString = ']' ;
276+ }
277+ }
278+ if (parameter.isRequiredNamed) {
279+ sb.write ('required ' );
280+ } else if (parameter.metadata2.hasDeprecated) {
281+ sb.write ('@required ' );
282+ }
283+ parameter.appendToWithoutDelimiters (sb);
284+ }
285+ sb.write (closeOptionalString);
286+ return '($sb )' ;
287+ }
288+
190289String ? _getTypeParametersString (engine.Element element) {
191290 List <engine.TypeParameterElement >? typeParameters;
192291 if (element is engine.InterfaceElement ) {
@@ -200,6 +299,19 @@ String? _getTypeParametersString(engine.Element element) {
200299 return '<${typeParameters .join (', ' )}>' ;
201300}
202301
302+ String ? _getTypeParametersString2 (engine.Element2 element) {
303+ List <engine.TypeParameterElement2 >? typeParameters;
304+ if (element is engine.InterfaceElement2 ) {
305+ typeParameters = element.typeParameters2;
306+ } else if (element is engine.TypeAliasElement2 ) {
307+ typeParameters = element.typeParameters2;
308+ }
309+ if (typeParameters == null || typeParameters.isEmpty) {
310+ return null ;
311+ }
312+ return '<${typeParameters .join (', ' )}>' ;
313+ }
314+
203315bool _isAbstract (engine.Element element) {
204316 if (element is engine.ClassElement ) {
205317 return element.isAbstract;
@@ -216,6 +328,19 @@ bool _isAbstract(engine.Element element) {
216328 return false ;
217329}
218330
331+ bool _isAbstract2 (engine.Element2 element) {
332+ if (element is engine.ClassElement2 ) {
333+ return element.isAbstract;
334+ }
335+ if (element is engine.MethodElement2 ) {
336+ return element.isAbstract;
337+ }
338+ if (element is engine.MixinElement2 ) {
339+ return true ;
340+ }
341+ return false ;
342+ }
343+
219344bool _isConst (engine.Element element) {
220345 if (element is engine.ConstructorElement ) {
221346 return element.isConst;
@@ -226,13 +351,30 @@ bool _isConst(engine.Element element) {
226351 return false ;
227352}
228353
354+ bool _isConst2 (engine.Element2 element) {
355+ if (element is engine.ConstructorElement2 ) {
356+ return element.isConst;
357+ }
358+ if (element is engine.VariableElement2 ) {
359+ return element.isConst;
360+ }
361+ return false ;
362+ }
363+
229364bool _isFinal (engine.Element element) {
230365 if (element is engine.VariableElement ) {
231366 return element.isFinal;
232367 }
233368 return false ;
234369}
235370
371+ bool _isFinal2 (engine.Element2 element) {
372+ if (element is engine.VariableElement2 ) {
373+ return element.isFinal;
374+ }
375+ return false ;
376+ }
377+
236378bool _isStatic (engine.Element element) {
237379 if (element is engine.ExecutableElement ) {
238380 return element.isStatic;
@@ -243,6 +385,16 @@ bool _isStatic(engine.Element element) {
243385 return false ;
244386}
245387
388+ bool _isStatic2 (engine.Element2 element) {
389+ if (element is engine.ExecutableElement2 ) {
390+ return element.isStatic;
391+ }
392+ if (element is engine.PropertyInducingElement2 ) {
393+ return element.isStatic;
394+ }
395+ return false ;
396+ }
397+
246398/// Sort required named parameters before optional ones.
247399int _preferRequiredParams (
248400 engine.ParameterElement e1, engine.ParameterElement e2) {
@@ -258,3 +410,19 @@ int _preferRequiredParams(
258410 : 1 ;
259411 return rank1 - rank2;
260412}
413+
414+ /// Sort required named parameters before optional ones.
415+ int _preferRequiredParams2 (
416+ engine.FormalParameterElement e1, engine.FormalParameterElement e2) {
417+ var rank1 = (e1.isRequiredNamed || e1.metadata2.hasRequired)
418+ ? 0
419+ : ! e1.isNamed
420+ ? - 1
421+ : 1 ;
422+ var rank2 = (e2.isRequiredNamed || e2.metadata2.hasRequired)
423+ ? 0
424+ : ! e2.isNamed
425+ ? - 1
426+ : 1 ;
427+ return rank1 - rank2;
428+ }
0 commit comments