Skip to content

Commit

Permalink
Rework the new element model so that all elements have fragments
Browse files Browse the repository at this point in the history
Change-Id: Ic4fc0368cabc5fa8dfc21c958e1619777f2701b5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392541
Commit-Queue: Brian Wilkerson <[email protected]>
Reviewed-by: Konstantin Shcheglov <[email protected]>
  • Loading branch information
bwilkerson authored and Commit Queue committed Oct 31, 2024
1 parent ae0510d commit 5ef250a
Show file tree
Hide file tree
Showing 19 changed files with 625 additions and 234 deletions.
23 changes: 1 addition & 22 deletions pkg/analysis_server/lib/src/protocol_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -261,38 +261,17 @@ Location? newLocation_fromElement(engine.Element? element) {
return _locationForArgs(unitElement, range);
}

/// Create a Location based on an [engine.Element].
/// Create a Location based on an [engine.Element2].
Location? newLocation_fromElement2(engine.Element2? element) {
if (element == null) {
return null;
}
if (element is engine.FragmentedElement) {
var fragment = (element as engine.FragmentedElement).firstFragment;
if (fragment == null) {
return null;
}
var offset = fragment.name2?.nameOffset ?? 0;
var length = fragment.name2?.name.length ?? 0;
var range = engine.SourceRange(offset, length);
return _locationForArgs2(fragment, range);
} else if (element is engine.LocalFunctionElement) {
var offset = element.nameOffset;
var length = element.name3?.length ?? 0;
var range = engine.SourceRange(offset, length);
var fragment = element.enclosingFunction;
return _locationForArgs2(fragment, range);
} else if (element is engine.LocalVariableElement2) {
var offset = element.nameOffset;
var length = element.name3?.length ?? 0;
var range = engine.SourceRange(offset, length);
var fragment = element.enclosingFunction;
return _locationForArgs2(fragment, range);
} else if (element is engine.LabelElement2) {
var offset = element.nameOffset;
var length = element.name3?.length ?? 0;
var range = engine.SourceRange(offset, length);
var fragment = element.enclosingFunction;
return _locationForArgs2(fragment, range);
} else {
assert(false, 'Could not convert ${element.runtimeType} to Location.');
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AddAsync extends ResolvedCorrectionProducer {
switch (node) {
case FunctionDeclaration():
body = node.functionExpression.body;
if (node.declaredElement2 case var declaredElement?) {
if (node.declaredFragment?.element case var declaredElement?) {
returnType = declaredElement.returnType;
} else if (node.declaredFragment case var declaredFragment?) {
returnType = declaredFragment.element.returnType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class AddFieldFormalParameters extends ResolvedCorrectionProducer {
var fields = ErrorVerifier.computeNotInitializedFields2(constructor);
fields.retainWhere((FieldElement2 field) => field.isFinal);
fields.sort((a, b) =>
a.firstFragment!.name2!.nameOffset -
b.firstFragment!.name2!.nameOffset);
a.firstFragment.name2!.nameOffset - b.firstFragment.name2!.nameOffset);

// Specialize for Flutter widgets.
if (superType.isExactlyStatelessWidgetType ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AddLate extends ResolvedCorrectionProducer {
!variableElement.isSynthetic &&
!variableElement.isLate &&
variableElement.setter2 == null) {
var variableFragment = variableElement.firstFragment!;
var variableFragment = variableElement.firstFragment;
var declarationResult =
await sessionHelper.getElementDeclaration2(variableFragment);
if (declarationResult == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class _AddMissingOptionalPositionalParameter extends _AddMissingParameter {
if (_executableParameters.required.isNotEmpty) {
var lastElement = _executableParameters.required.last;
var prevNode = await _executableParameters
.getParameterNode2(lastElement.firstFragment!);
.getParameterNode2(lastElement.firstFragment);
await _addParameter(builder, prevNode?.end, prefix, ']');
} else {
var parameterList = await _executableParameters.getParameterList();
Expand Down Expand Up @@ -122,7 +122,7 @@ class _AddMissingRequiredPositionalParameter extends _AddMissingParameter {
if (_executableParameters.required.isNotEmpty) {
var lastElement = _executableParameters.required.last;
var prevNode = await _executableParameters
.getParameterNode2(lastElement.firstFragment!);
.getParameterNode2(lastElement.firstFragment);
await _addParameter(builder, prevNode?.end, ', ', '');
} else {
var parameterList = await _executableParameters.getParameterList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class AddMissingParameterNamed extends ResolvedCorrectionProducer {
}

if (context.named.isNotEmpty) {
var lastFirst = context.named.last.firstFragment!;
var lastFirst = context.named.last.firstFragment;
var prevNode = await context.getParameterNode2(lastFirst);
await addParameter(prevNode?.end, ', ', '');
} else if (context.required.isNotEmpty) {
var lastFirst = context.required.last.firstFragment!;
var lastFirst = context.required.last.firstFragment;
var prevNode = await context.getParameterNode2(lastFirst);
await addParameter(prevNode?.end, ', {', '}');
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MakeFieldNotFinal extends ResolvedCorrectionProducer {
}

var declaration =
await sessionHelper.getElementDeclaration2(variable.firstFragment!);
await sessionHelper.getElementDeclaration2(variable.firstFragment);
var variableNode = declaration?.node;
if (variableNode is! VariableDeclaration) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RemoveLeadingUnderscore extends ResolvedCorrectionProducer {
element = node.declaredElement2;
} else if (node is FunctionDeclaration) {
nameToken = node.name;
element = node.declaredElement2 ?? node.declaredFragment?.element;
element = node.declaredFragment?.element;
} else {
return;
}
Expand Down
Loading

0 comments on commit 5ef250a

Please sign in to comment.