private void handleAppends(IASTNode node) { List<ASTModification> modifications = getModifications(node, ModificationKind.APPEND_CHILD); if (modifications.isEmpty()) return; ChangeGeneratorWriterVisitor writer = new ChangeGeneratorWriterVisitor(modificationStore, commentMap); ReplaceEdit anchor = getAppendAnchor(node); Assert.isNotNull(anchor); IASTNode precedingNode = getLastNodeBeforeAppendPoint(node); for (ASTModification modification : modifications) { IASTNode newNode = modification.getNewNode(); if (precedingNode != null) { if (ASTWriter.requireBlankLineInBetween(precedingNode, newNode)) { writer.newLine(); } } else if (node instanceof ICPPASTNamespaceDefinition) { writer.newLine(); } precedingNode = null; newNode.accept(writer); } if (node instanceof ICPPASTNamespaceDefinition) { writer.newLine(); } String code = writer.toString(); IFile file = FileHelper.getFileFromNode(node); MultiTextEdit parentEdit = getEdit(node, file); ReplaceEdit edit = new ReplaceEdit(anchor.getOffset(), anchor.getLength(), code + anchor.getText()); parentEdit.addChild(edit); IASTFileLocation fileLocation = node.getFileLocation(); sourceOffsets.put(fileLocation.getFileName(), endOffset(fileLocation)); }
private void handleAppends(IASTTranslationUnit tu) { List<ASTModification> modifications = getModifications(tu, ModificationKind.APPEND_CHILD); if (modifications.isEmpty()) return; IASTNode prevNode = null; IASTDeclaration[] declarations = tu.getDeclarations(); if (declarations.length != 0) { prevNode = declarations[declarations.length - 1]; } else { IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements(); if (preprocessorStatements.length != 0) { prevNode = preprocessorStatements[preprocessorStatements.length - 1]; } } int offset = prevNode != null ? getEndOffsetIncludingComments(prevNode) : 0; String source = tu.getRawSignature(); int endOffset = skipTrailingBlankLines(source, offset); ChangeGeneratorWriterVisitor writer = new ChangeGeneratorWriterVisitor(modificationStore, commentMap); IASTNode newNode = null; for (ASTModification modification : modifications) { boolean first = newNode == null; newNode = modification.getNewNode(); if (first) { if (prevNode != null) { writer.newLine(); if (ASTWriter.requireBlankLineInBetween(prevNode, newNode)) { writer.newLine(); } } } newNode.accept(writer); } if (prevNode != null) { IASTNode nextNode = getNextSiblingOrPreprocessorNode(prevNode); if (nextNode != null && ASTWriter.requireBlankLineInBetween(newNode, nextNode)) { writer.newLine(); } } String code = writer.toString(); IFile file = FileHelper.getFileFromNode(tu); MultiTextEdit parentEdit = getEdit(tu, file); parentEdit.addChild(new ReplaceEdit(offset, endOffset - offset, code)); }
private void handleReplace(IASTNode node) { List<ASTModification> modifications = getModifications(node, ModificationKind.REPLACE); String source = node.getTranslationUnit().getRawSignature(); TextEdit edit; ChangeGeneratorWriterVisitor writer = new ChangeGeneratorWriterVisitor(modificationStore, commentMap); IASTFileLocation fileLocation = node.getFileLocation(); Integer val = sourceOffsets.get(fileLocation.getFileName()); int processedOffset = val != null ? val.intValue() : 0; if (modifications.size() == 1 && modifications.get(0).getNewNode() == null) { int offset = getOffsetIncludingComments(node); int endOffset = getEndOffsetIncludingComments(node); offset = Math.max(skipPrecedingBlankLines(source, offset), processedOffset); endOffset = skipTrailingBlankLines(source, endOffset); 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); } } IASTNode prevNode = getPreviousSiblingOrPreprocessorNode(node); IASTNode nextNode = getNextSiblingOrPreprocessorNode(node); if (prevNode != null && nextNode != null) { if (ASTWriter.requireBlankLineInBetween(prevNode, nextNode)) { writer.newLine(); } } else if (node.getParent() instanceof ICPPASTNamespaceDefinition) { writer.newLine(); } String code = writer.toString(); edit = new ReplaceEdit(offset, endOffset - offset, code); } else { node.accept(writer); String code = writer.toString(); int offset = fileLocation.getNodeOffset(); int endOffset = offset + fileLocation.getNodeLength(); if (node instanceof IASTStatement || node instanceof IASTDeclaration) { // Include trailing comments in the area to be replaced. endOffset = Math.max(endOffset, getEndOffsetIncludingTrailingComments(node)); } String lineSeparator = writer.getScribe().getLineSeparator(); if (code.endsWith(lineSeparator)) { code = code.substring(0, code.length() - lineSeparator.length()); } edit = new ReplaceEdit(offset, endOffset - offset, code); } IFile file = FileHelper.getFileFromNode(node); MultiTextEdit parentEdit = getEdit(node, file); parentEdit.addChild(edit); sourceOffsets.put(fileLocation.getFileName(), edit.getExclusiveEnd()); }
private void handleInserts(IASTNode anchorNode) { List<ASTModification> modifications = getModifications(anchorNode, ModificationKind.INSERT_BEFORE); if (modifications.isEmpty()) return; ChangeGeneratorWriterVisitor writer = new ChangeGeneratorWriterVisitor(modificationStore, commentMap); IASTNode newNode = null; for (ASTModification modification : modifications) { boolean first = newNode == null; newNode = modification.getNewNode(); if (first) { IASTNode prevNode = getPreviousSiblingOrPreprocessorNode(anchorNode); if (prevNode != null) { if (ASTWriter.requireBlankLineInBetween(prevNode, newNode)) { writer.newLine(); } } else if (anchorNode.getParent() instanceof ICPPASTNamespaceDefinition) { writer.newLine(); } } newNode.accept(writer); if (getContainingNodeList(anchorNode) != null) { writer.getScribe().print(", "); // $NON-NLS-1$ } } if (ASTWriter.requireBlankLineInBetween(newNode, anchorNode)) { writer.newLine(); } int insertPos = getOffsetIncludingComments(anchorNode); int length = 0; if (writer.getScribe().isAtBeginningOfLine()) { String tuCode = anchorNode.getTranslationUnit().getRawSignature(); insertPos = skipPrecedingWhitespace(tuCode, insertPos); length = insertPos; insertPos = skipPrecedingBlankLines(tuCode, insertPos); length -= insertPos; } String code = writer.toString(); ReplaceEdit edit = new ReplaceEdit(insertPos, length, code); IFile file = FileHelper.getFileFromNode(anchorNode); MultiTextEdit parentEdit = getEdit(anchorNode, file); parentEdit.addChild(edit); sourceOffsets.put(file.getName(), edit.getOffset()); }