Skip to content

Commit

Permalink
DAS: Rename 'add___Insertion' to 'insert___' APIs
Browse files Browse the repository at this point in the history
Change-Id: I57693591999e0845e6a3560a19de770eef4968b2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/362001
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Apr 9, 2024
1 parent 7f588a1 commit c2f3418
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class AddDiagnosticPropertyReference extends ResolvedCorrectionProducer {
.singleOrNull;
if (debugFillProperties == null) {
await builder.addDartFileEdit(file, (builder) {
builder.addMethodInsertion(classDeclaration, (builder) {
builder.insertMethod(classDeclaration, (builder) {
final declPrefix = utils.oneIndent;
final bodyPrefix = utils.twoIndents;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class AddKeyToConstructors extends ResolvedCorrectionProducer {

var canBeConst = _canBeConst(node, constructors);
await builder.addDartFileEdit(file, (builder) {
builder.addConstructorInsertion(node, (builder) {
builder.insertConstructor(node, (builder) {
// TODO(srawlins): Replace this block with `writeConstructorDeclaration`
// and `parameterWriter`.
if (canBeConst) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class AddMissingEnumCaseClauses extends ResolvedCorrectionProducer {
await builder.addDartFileEdit(file, (builder) {
// TODO(brianwilkerson): Consider inserting the names in order into the
// switch statement.
builder.addCaseClauseAtEndInsertion(
builder.insertCaseClauseAtEnd(
switchKeyword: statement.switchKeyword,
rightParenthesis: statement.rightParenthesis,
leftBracket: statement.leftBracket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AddMissingEnumLikeCaseClauses extends ResolvedCorrectionProducer {
await builder.addDartFileEdit(file, (builder) {
// TODO(brianwilkerson): Consider inserting the names in order into the
// switch statement.
builder.addCaseClauseAtEndInsertion(
builder.insertCaseClauseAtEnd(
switchKeyword: node.switchKeyword,
rightParenthesis: node.rightParenthesis,
leftBracket: node.leftBracket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AddMissingSwitchCases extends ResolvedCorrectionProducer {
final singleIndent = utils.oneIndent;

await builder.addDartFileEdit(file, (builder) {
builder.addCaseClauseAtEndInsertion(
builder.insertCaseClauseAtEnd(
switchKeyword: node.switchKeyword,
rightParenthesis: node.rightParenthesis,
leftBracket: node.leftBracket,
Expand All @@ -86,7 +86,7 @@ class AddMissingSwitchCases extends ResolvedCorrectionProducer {
final singleIndent = utils.oneIndent;

await builder.addDartFileEdit(file, (builder) {
builder.addCaseClauseAtEndInsertion(
builder.insertCaseClauseAtEnd(
switchKeyword: node.switchKeyword,
rightParenthesis: node.rightParenthesis,
leftBracket: node.leftBracket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class CreateConstructor extends ResolvedCorrectionProducer {
var targetSource = targetElement.source;
var targetFile = targetSource.fullName;
await builder.addDartFileEdit(targetFile, (builder) {
builder.addConstructorInsertion(targetNode, (builder) {
builder.insertConstructor(targetNode, (builder) {
builder.writeConstructorDeclaration(targetElement.name,
argumentList: instanceCreation.argumentList);
});
Expand All @@ -185,7 +185,7 @@ class CreateConstructor extends ResolvedCorrectionProducer {
}) async {
var targetFile = resolvedUnit.file.path;
await builder.addDartFileEdit(targetFile, (builder) {
builder.addConstructorInsertion(unitMember, (builder) {
builder.insertConstructor(unitMember, (builder) {
builder.writeConstructorDeclaration(unitMember.name.lexeme,
isConst: isConst,
argumentList: argumentList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class CreateConstructorForFinalFields extends ResolvedCorrectionProducer {
}

await fixContext.builder.addDartFileEdit(file, (builder) {
builder.addConstructorInsertion(classDeclaration, (builder) {
builder.insertConstructor(classDeclaration, (builder) {
// TODO(srawlins): Replace this block with `writeConstructorDeclaration`
// and `parameterWriter`.
builder.write('const ');
Expand Down Expand Up @@ -177,7 +177,7 @@ class CreateConstructorForFinalFields extends ResolvedCorrectionProducer {
required NamedCompilationUnitMember classDeclaration,
}) async {
await fixContext.builder.addDartFileEdit(file, (builder) {
builder.addConstructorInsertion(classDeclaration, (builder) {
builder.insertConstructor(classDeclaration, (builder) {
// TODO(srawlins): Replace this block with `writeConstructorDeclaration`
// and `parameterWriter`.
builder.write('const ');
Expand All @@ -204,7 +204,7 @@ class CreateConstructorForFinalFields extends ResolvedCorrectionProducer {
final fieldsForInitializers = <_Field>[];

await fixContext.builder.addDartFileEdit(file, (builder) {
builder.addConstructorInsertion(containerDeclaration, (builder) {
builder.insertConstructor(containerDeclaration, (builder) {
// TODO(srawlins): Replace this block with `writeConstructorDeclaration`
// and `parameterWriter`.
if (isConst) {
Expand Down Expand Up @@ -266,7 +266,7 @@ class CreateConstructorForFinalFields extends ResolvedCorrectionProducer {
required List<_Field> fields,
}) async {
await fixContext.builder.addDartFileEdit(file, (builder) {
builder.addConstructorInsertion(containerDeclaration, (builder) {
builder.insertConstructor(containerDeclaration, (builder) {
// TODO(srawlins): Replace this block with `writeConstructorDeclaration`
// and `parameterWriter`.
if (isConst) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class _CreateConstructor extends ResolvedCorrectionProducer {
var requiredNamedParameters =
_constructor.parameters.where((parameter) => parameter.isRequiredNamed);
await builder.addDartFileEdit(file, (builder) {
builder.addConstructorInsertion(_targetClass, (builder) {
builder.insertConstructor(_targetClass, (builder) {
// TODO(srawlins): Replace this block with `writeConstructorDeclaration`
// and `parameterWriter`.
void writeParameters(bool isDefinition) {
Expand Down Expand Up @@ -173,7 +173,7 @@ class _CreateConstructor extends ResolvedCorrectionProducer {
var requiredNamedParameters =
_constructor.parameters.where((parameter) => parameter.isRequiredNamed);
await builder.addDartFileEdit(file, (builder) {
builder.addConstructorInsertion(_targetClass, (builder) {
builder.insertConstructor(_targetClass, (builder) {
// TODO(srawlins): Replace this block with `writeConstructorDeclaration`
// and `parameterWriter`.
void writeParameter(ParameterElement parameter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CreateField extends CreateFieldOrGetter {
var targetSource = targetElement.source;
var targetFile = targetSource.fullName;
await builder.addDartFileEdit(targetFile, (builder) {
builder.addFieldInsertion(targetNode, (builder) {
builder.insertField(targetNode, (builder) {
builder.writeFieldDeclaration(_fieldName,
isStatic: staticModifier,
nameGroupName: 'NAME',
Expand All @@ -104,7 +104,7 @@ class CreateField extends CreateFieldOrGetter {
// Add proposal.
await builder.addDartFileEdit(file, (builder) {
var fieldType = parameter.type?.type;
builder.addFieldInsertion(targetClassNode, (builder) {
builder.insertField(targetClassNode, (builder) {
builder.writeFieldDeclaration(_fieldName,
nameGroupName: 'NAME', type: fieldType, typeGroupName: 'TYPE');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class CreateGetter extends CreateFieldOrGetter {
// Build method source.
var targetFile = targetSource.fullName;
await builder.addDartFileEdit(targetFile, (builder) {
builder.addGetterInsertion(
builder.insertGetter(
targetNode,
(builder) {
builder.writeGetterDeclaration(_getterName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class CreateMethod extends ResolvedCorrectionProducer {
if (targetNode == null) {
return;
}
builder.addMethodInsertion(targetNode, (builder) {
builder.insertMethod(targetNode, (builder) {
// Maybe 'static'.
if (staticModifier) {
builder.write('static ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class CreateSetter extends ResolvedCorrectionProducer {
var targetFile = targetSource.fullName;
_setterName = nameNode.name;
await builder.addDartFileEdit(targetFile, (builder) {
builder.addGetterInsertion(
builder.insertGetter(
targetNode,
(builder) {
var parameterTypeNode = climbPropertyAccess(nameNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class RenameConstructorRefactoringImpl extends RenameRefactoringImpl {
) async {
var builder = ChangeBuilder(session: sessionHelper.session);
await builder.addDartFileEdit(resolvedUnit.path, (builder) {
builder.addConstructorInsertion(container, buildEdit);
builder.insertConstructor(container, buildEdit);
});
var fileEdit = builder.sourceChange.getFileEdit(resolvedUnit.path);
if (fileEdit == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1411,86 +1411,6 @@ class DartFileEditBuilderImpl extends FileEditBuilderImpl
@override
List<Uri> get requiredImports => librariesToImport.keys.toList();

@override
void addCaseClauseAtEndInsertion(
void Function(DartEditBuilder builder) buildEdit, {
required Token switchKeyword,
required Token rightParenthesis,
required Token leftBracket,
required Token rightBracket,
}) {
var blockStart = resolvedUnit.lineInfo.getLocation(leftBracket.offset);
var blockEnd = resolvedUnit.lineInfo.getLocation(rightBracket.offset);
var isBlockSingleLine = blockStart.lineNumber == blockEnd.lineNumber;
int offset;
if (isBlockSingleLine) {
offset = leftBracket.isSynthetic ? rightParenthesis.end : leftBracket.end;
} else {
offset = resolvedUnit.lineInfo.getOffsetOfLine(blockEnd.lineNumber - 1);
}

addInsertion(offset, (builder) {
if (leftBracket.isSynthetic) {
builder.write(' {');
}
if (isBlockSingleLine) {
builder.writeln();
}
buildEdit(builder);
if (isBlockSingleLine) {
builder.write(resolvedUnit.linePrefix(switchKeyword.offset));
}
if (rightBracket.isSynthetic) {
builder.write('}');
}
});
}

@override
void addConstructorInsertion(
CompilationUnitMember compilationUnitMember,
void Function(DartEditBuilder builder) buildEdit,
) {
final sortConstructorsFirst = resolvedUnit.session.analysisContext
.getAnalysisOptionsForFile(resolvedUnit.file)
.codeStyleOptions
.sortConstructorsFirst;
var lastMemberFilter = sortConstructorsFirst
? (member) => member is ConstructorDeclaration
: (member) =>
member is ConstructorDeclaration || member is FieldDeclaration;
_addCompilationUnitMemberInsertion(
compilationUnitMember,
buildEdit,
lastMemberFilter: lastMemberFilter,
);
}

@override
void addFieldInsertion(
CompilationUnitMember compilationUnitMember,
void Function(DartEditBuilder builder) buildEdit,
) =>
_addCompilationUnitMemberInsertion(
compilationUnitMember,
buildEdit,
lastMemberFilter: (member) => member is FieldDeclaration,
);

@override
void addGetterInsertion(
CompilationUnitMember compilationUnitMember,
void Function(DartEditBuilder builder) buildEdit,
) =>
_addCompilationUnitMemberInsertion(
compilationUnitMember,
buildEdit,
lastMemberFilter: (member) =>
member is FieldDeclaration ||
member is ConstructorDeclaration ||
member is MethodDeclaration && member.isGetter,
);

@override
void addInsertion(
int offset, void Function(DartEditBuilder builder) buildEdit,
Expand All @@ -1499,20 +1419,6 @@ class DartFileEditBuilderImpl extends FileEditBuilderImpl
offset, (builder) => buildEdit(builder as DartEditBuilder),
insertBeforeExisting: insertBeforeExisting);

@override
void addMethodInsertion(
CompilationUnitMember compilationUnitMember,
void Function(DartEditBuilder builder) buildEdit,
) =>
_addCompilationUnitMemberInsertion(
compilationUnitMember,
buildEdit,
lastMemberFilter: (member) =>
member is FieldDeclaration ||
member is ConstructorDeclaration ||
member is MethodDeclaration,
);

@override
void addReplacement(SourceRange range,
void Function(DartEditBuilder builder) buildEdit) =>
Expand Down Expand Up @@ -1711,6 +1617,100 @@ class DartFileEditBuilderImpl extends FileEditBuilderImpl
return false;
}

@override
void insertCaseClauseAtEnd(
void Function(DartEditBuilder builder) buildEdit, {
required Token switchKeyword,
required Token rightParenthesis,
required Token leftBracket,
required Token rightBracket,
}) {
var blockStart = resolvedUnit.lineInfo.getLocation(leftBracket.offset);
var blockEnd = resolvedUnit.lineInfo.getLocation(rightBracket.offset);
var isBlockSingleLine = blockStart.lineNumber == blockEnd.lineNumber;
int offset;
if (isBlockSingleLine) {
offset = leftBracket.isSynthetic ? rightParenthesis.end : leftBracket.end;
} else {
offset = resolvedUnit.lineInfo.getOffsetOfLine(blockEnd.lineNumber - 1);
}

addInsertion(offset, (builder) {
if (leftBracket.isSynthetic) {
builder.write(' {');
}
if (isBlockSingleLine) {
builder.writeln();
}
buildEdit(builder);
if (isBlockSingleLine) {
builder.write(resolvedUnit.linePrefix(switchKeyword.offset));
}
if (rightBracket.isSynthetic) {
builder.write('}');
}
});
}

@override
void insertConstructor(
CompilationUnitMember compilationUnitMember,
void Function(DartEditBuilder builder) buildEdit,
) {
final sortConstructorsFirst = resolvedUnit.session.analysisContext
.getAnalysisOptionsForFile(resolvedUnit.file)
.codeStyleOptions
.sortConstructorsFirst;
var lastMemberFilter = sortConstructorsFirst
? (member) => member is ConstructorDeclaration
: (member) =>
member is ConstructorDeclaration || member is FieldDeclaration;
_addCompilationUnitMemberInsertion(
compilationUnitMember,
buildEdit,
lastMemberFilter: lastMemberFilter,
);
}

@override
void insertField(
CompilationUnitMember compilationUnitMember,
void Function(DartEditBuilder builder) buildEdit,
) =>
_addCompilationUnitMemberInsertion(
compilationUnitMember,
buildEdit,
lastMemberFilter: (member) => member is FieldDeclaration,
);

@override
void insertGetter(
CompilationUnitMember compilationUnitMember,
void Function(DartEditBuilder builder) buildEdit,
) =>
_addCompilationUnitMemberInsertion(
compilationUnitMember,
buildEdit,
lastMemberFilter: (member) =>
member is FieldDeclaration ||
member is ConstructorDeclaration ||
member is MethodDeclaration && member.isGetter,
);

@override
void insertMethod(
CompilationUnitMember compilationUnitMember,
void Function(DartEditBuilder builder) buildEdit,
) =>
_addCompilationUnitMemberInsertion(
compilationUnitMember,
buildEdit,
lastMemberFilter: (member) =>
member is FieldDeclaration ||
member is ConstructorDeclaration ||
member is MethodDeclaration,
);

@override
void replaceTypeWithFuture(
TypeAnnotation? typeAnnotation, TypeProvider typeProvider) {
Expand Down
Loading

0 comments on commit c2f3418

Please sign in to comment.