private void handleReplace(final IASTNode node) {
final List<ASTModification> modifications = getModifications(node, ModificationKind.REPLACE);
final String source = node.getTranslationUnit().getRawSignature();
final ChangeGeneratorWriterVisitor writer = new ChangeGeneratorWriterVisitor(modificationStore, commentMap);
final IASTFileLocation fileLocation = node.getFileLocation();
addToRootEdit(node);
if ((modifications.size() == 1) && (modifications.get(0).getNewNode() == null)) {
// There is no replacement. We are deleting a piece of existing code.
int offset = getOffsetIncludingComments(node);
int endOffset = getEndOffsetIncludingComments(node);
offset = Math.max(skipPrecedingBlankLines(source, offset), processedOffset);
endOffset = skipTrailingBlankLines(source, endOffset);
final IASTNode[] siblingsList = getContainingNodeList(node);
if (siblingsList != null) {
if (siblingsList.length > 1) {
if (node == siblingsList[0]) {
endOffset = skipToTrailingDelimiter(source, ',', endOffset);
} else {
offset = skipToPrecedingDelimiter(source, ',', offset);
}
} else if (node.getPropertyInParent() == ICPPASTFunctionDefinition.MEMBER_INITIALIZER) {
offset = skipToPrecedingDelimiter(source, ':', offset);
}
}
final IASTNode prevNode = getPreviousSiblingOrPreprocessorNode(node);
final IASTNode nextNode = getNextSiblingOrPreprocessorNode(node);
if ((prevNode != null) && (nextNode != null)) {
if (ASTWriter.requireBlankLineInBetween(prevNode, nextNode)) {
writer.newLine();
}
} else if (node.getParent() instanceof ICPPASTNamespaceDefinition) {
writer.newLine();
}
final String code = writer.toString();
if (endOffset > offset) {
addChildEdit(new DeleteEdit(offset, endOffset - offset));
}
if (!code.isEmpty()) {
addChildEdit(new InsertEdit(endOffset, code));
}
} else {
node.accept(writer);
String code = writer.toString();
final int offset = fileLocation.getNodeOffset();
final int endOffset = offset + fileLocation.getNodeLength();
final String lineSeparator = writer.getScribe().getLineSeparator();
if (code.endsWith(lineSeparator)) {
code = code.substring(0, code.length() - lineSeparator.length());
}
addChildEdit(new ReplaceEdit(offset, endOffset - offset, code));