Skip to content

Commit 640ad14

Browse files
keertipCommit Queue
authored andcommitted
Add methods for element2 to be used for migration
Change-Id: I4794f03c2140ad84139114ea5a308116fb5d41ea Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390801 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]>
1 parent e526fd7 commit 640ad14

File tree

6 files changed

+492
-6
lines changed

6 files changed

+492
-6
lines changed

pkg/analysis_server/lib/plugin/protocol/protocol_dart.dart

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ library;
88

99
import 'package:analysis_server/src/protocol_server.dart';
1010
import 'package:analyzer/dart/element/element.dart' as engine;
11+
import 'package:analyzer/dart/element/element2.dart' as engine;
1112
import 'package:analyzer/dart/element/type.dart';
13+
import 'package:analyzer/src/utilities/extensions/element.dart';
1214
import '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+
133175
String 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+
141191
String? _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+
190289
String? _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+
203315
bool _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+
219344
bool _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+
229364
bool _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+
236378
bool _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.
247399
int _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+
}

pkg/analysis_server/lib/src/protocol_server.dart

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:analyzer/dart/analysis/results.dart' as engine;
1212
import 'package:analyzer/dart/ast/ast.dart' as engine;
1313
import 'package:analyzer/dart/ast/token.dart' as engine;
1414
import 'package:analyzer/dart/element/element.dart' as engine;
15+
import 'package:analyzer/dart/element/element2.dart' as engine;
1516
import 'package:analyzer/dart/element/type.dart';
1617
import 'package:analyzer/diagnostic/diagnostic.dart' as engine;
1718
import 'package:analyzer/error/error.dart' as engine;
@@ -55,6 +56,14 @@ String? getAliasedTypeString(engine.Element element) {
5556
return null;
5657
}
5758

59+
String? getAliasedTypeString2(engine.Element2 element) {
60+
if (element is engine.TypeAliasElement2) {
61+
var aliasedType = element.aliasedType;
62+
return aliasedType.getDisplayString();
63+
}
64+
return null;
65+
}
66+
5867
/// Returns a color hex code (in the form '#FFFFFF') if [element] represents
5968
/// a color.
6069
String? getColorHexString(engine.Element? element) {
@@ -74,6 +83,25 @@ String? getColorHexString(engine.Element? element) {
7483
return null;
7584
}
7685

86+
/// Returns a color hex code (in the form '#FFFFFF') if [element] represents
87+
/// a color.
88+
String? getColorHexString2(engine.Element2? element) {
89+
if (element is engine.VariableElement2) {
90+
var dartValue = element.computeConstantValue();
91+
if (dartValue != null) {
92+
var color = ColorComputer.getColorForObject(dartValue);
93+
if (color != null) {
94+
return '#'
95+
'${color.red.toRadixString(16).padLeft(2, '0')}'
96+
'${color.green.toRadixString(16).padLeft(2, '0')}'
97+
'${color.blue.toRadixString(16).padLeft(2, '0')}'
98+
.toUpperCase();
99+
}
100+
}
101+
}
102+
return null;
103+
}
104+
77105
String? getReturnTypeString(engine.Element element) {
78106
if (element is engine.ExecutableElement) {
79107
if (element.kind == engine.ElementKind.SETTER) {
@@ -94,6 +122,26 @@ String? getReturnTypeString(engine.Element element) {
94122
return null;
95123
}
96124

125+
String? getReturnTypeString2(engine.Element2 element) {
126+
if (element is engine.ExecutableElement2) {
127+
if (element.kind == engine.ElementKind.SETTER) {
128+
return null;
129+
} else {
130+
return element.returnType.getDisplayString();
131+
}
132+
} else if (element is engine.VariableElement2) {
133+
var type = element.type;
134+
return type.getDisplayString();
135+
} else if (element is engine.TypeAliasElement2) {
136+
var aliasedType = element.aliasedType;
137+
if (aliasedType is FunctionType) {
138+
var returnType = aliasedType.returnType;
139+
return returnType.getDisplayString();
140+
}
141+
}
142+
return null;
143+
}
144+
97145
/// Translates engine errors through the ErrorProcessor.
98146
List<T> mapEngineErrors<T>(
99147
engine.AnalysisResultWithErrors result,
@@ -213,6 +261,44 @@ Location? newLocation_fromElement(engine.Element? element) {
213261
return _locationForArgs(unitElement, range);
214262
}
215263

264+
/// Create a Location based on an [engine.Element].
265+
Location? newLocation_fromElement2(engine.Element2? element) {
266+
if (element == null) {
267+
return null;
268+
}
269+
if (element is engine.FragmentedElement) {
270+
var fragment = (element as engine.FragmentedElement).firstFragment;
271+
if (fragment == null) {
272+
return null;
273+
}
274+
var offset = fragment.nameOffset ?? 0;
275+
var length = fragment.name?.length ?? 0;
276+
var range = engine.SourceRange(offset, length);
277+
return _locationForArgs2(fragment, range);
278+
} else if (element is engine.LocalFunctionElement) {
279+
var offset = element.nameOffset;
280+
var length = element.name?.length ?? 0;
281+
var range = engine.SourceRange(offset, length);
282+
var fragment = element.enclosingFunction;
283+
return _locationForArgs2(fragment, range);
284+
} else if (element is engine.LocalVariableElement2) {
285+
var offset = element.nameOffset;
286+
var length = element.name.length;
287+
var range = engine.SourceRange(offset, length);
288+
var fragment = element.enclosingFunction;
289+
return _locationForArgs2(fragment, range);
290+
} else if (element is engine.LabelElement2) {
291+
var offset = element.nameOffset;
292+
var length = element.name.length;
293+
var range = engine.SourceRange(offset, length);
294+
var fragment = element.enclosingFunction;
295+
return _locationForArgs2(fragment, range);
296+
} else {
297+
assert(false, 'Could not convert ${element.runtimeType} to Location.');
298+
return null;
299+
}
300+
}
301+
216302
/// Create a Location based on an [engine.SearchMatch].
217303
Location newLocation_fromMatch(engine.SearchMatch match) {
218304
var unitElement = _getUnitElement(match.element);
@@ -339,3 +425,20 @@ Location _locationForArgs(
339425
startLine, startColumn,
340426
endLine: endLine, endColumn: endColumn);
341427
}
428+
429+
/// Creates a new [Location].
430+
Location _locationForArgs2(engine.Fragment fragment, engine.SourceRange range) {
431+
var lineInfo = fragment.libraryFragment.lineInfo;
432+
433+
var startLocation = lineInfo.getLocation(range.offset);
434+
var endLocation = lineInfo.getLocation(range.end);
435+
436+
var startLine = startLocation.lineNumber;
437+
var startColumn = startLocation.columnNumber;
438+
var endLine = endLocation.lineNumber;
439+
var endColumn = endLocation.columnNumber;
440+
441+
return Location(fragment.libraryFragment.source.fullName, range.offset,
442+
range.length, startLine, startColumn,
443+
endLine: endLine, endColumn: endColumn);
444+
}

0 commit comments

Comments
 (0)