private IASTNode getNextSiblingOrPreprocessorNode(IASTNode node) { int endOffset = endOffset(node); IASTTranslationUnit ast = node.getTranslationUnit(); IASTPreprocessorStatement[] preprocessorStatements = ast.getAllPreprocessorStatements(); int low = 0; int high = preprocessorStatements.length; while (low < high) { int mid = (low + high) / 2; IASTNode statement = preprocessorStatements[mid]; if (statement.isPartOfTranslationUnitFile() && offset(statement) > endOffset) { high = mid; } else { low = mid + 1; } } if (high < preprocessorStatements.length) { IASTNode statement = preprocessorStatements[high]; if (statement.isPartOfTranslationUnitFile()) { int offset = offset(statement); if (!doesRegionContainNode(ast, endOffset, offset - endOffset)) { return statement; } } } return getNextSiblingNode(node); }
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()); }
public void generateChange(IASTNode rootNode, ASTVisitor pathProvider) throws ProblemRuntimeException { change = new CompositeChange(ChangeGeneratorMessages.ChangeGenerator_compositeChange); classifyModifications(); rootNode.accept(pathProvider); for (IFile currentFile : changes.keySet()) { MultiTextEdit edit = changes.get(currentFile); edit = formatChangedCode( edit, rootNode.getTranslationUnit().getRawSignature(), currentFile.getProject()); TextFileChange subchange = ASTRewriteAnalyzer.createCTextFileChange(currentFile); subchange.setEdit(edit); change.add(subchange); } }
private void addDefinition( ModificationCollector collector, List<IASTFunctionDefinition> definitions, IProgressMonitor pm) throws CoreException { findDefinitionInsertLocation(pm); IASTNode parent = definitionInsertLocation.getParentOfNodeToInsertBefore(); IASTTranslationUnit ast = parent.getTranslationUnit(); ASTRewrite rewrite = collector.rewriterForTranslationUnit(ast); IASTNode nodeToInsertBefore = definitionInsertLocation.getNodeToInsertBefore(); ContainerNode cont = new ContainerNode(); for (IASTFunctionDefinition functionDefinition : definitions) { cont.addNode(functionDefinition); } rewrite = rewrite.insertBefore(parent, nodeToInsertBefore, cont, null); }
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()); }
public PDOMFile getFileForASTNode(int linkageID, IASTNode node) throws CoreException { if (fPathResolver != null && node != null) { IASTFileLocation loc = node.getFileLocation(); if (loc != null) { IASTPreprocessorIncludeStatement owner = loc.getContextInclusionStatement(); ISignificantMacros sigMacros = owner != null ? owner.getSignificantMacros() : ISignificantMacros.NONE; if (sigMacros != null) { IIndexFileLocation location = fPathResolver.resolveASTPath(loc.getFileName()); if (uncommittedKey != null && uncommittedKey.equals(new FileContentKey(linkageID, location, sigMacros))) return fileBeingUpdated != null ? fileBeingUpdated : uncommittedFile; return getBestFile( linkageID, location, node.getTranslationUnit().getOriginatingTranslationUnit()); } } } return null; }