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 boolean isAppendable(ASTModification modification) { if (modification.getKind() != ModificationKind.APPEND_CHILD) return false; IASTNode node = modification.getNewNode(); if (node instanceof ContainerNode) { for (IASTNode containedNode : ((ContainerNode) node).getNodes()) { if (!(containedNode instanceof IASTDeclaration || containedNode instanceof IASTStatement)) return false; } return true; } return node instanceof IASTDeclaration || node instanceof IASTStatement; }
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 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()); }