예제 #1
0
 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));
 }
예제 #2
0
  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());
  }
예제 #3
0
 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;
 }
예제 #4
0
 /**
  * Returns a replace edit whose offset is the position where child appended nodes should be
  * inserted at. The text contains the content of the code region that will be disturbed by the
  * insertion.
  *
  * @param node The node to append children to.
  * @return a ReplaceEdit object, or <code>null</code> if the node does not support appending
  *     children to it.
  */
 private ReplaceEdit getAppendAnchor(IASTNode node) {
   if (!(node instanceof IASTCompositeTypeSpecifier
       || node instanceof IASTCompoundStatement
       || node instanceof ICPPASTNamespaceDefinition)) {
     return null;
   }
   String code = node.getRawSignature();
   IASTFileLocation location = node.getFileLocation();
   int pos = location.getNodeOffset() + location.getNodeLength();
   int len = code.endsWith("}") ? 1 : 0; // $NON-NLS-1$
   int insertPos = code.length() - len;
   int startOfLine = skipPrecedingBlankLines(code, insertPos);
   if (startOfLine == insertPos) {
     // Include the closing brace in the region that will be reformatted.
     return new ReplaceEdit(pos - len, len, code.substring(insertPos));
   }
   return new ReplaceEdit(
       location.getNodeOffset() + startOfLine, insertPos - startOfLine, ""); // $NON-NLS-1$
 }
예제 #5
0
 private int getEndOffsetIncludingTrailingComments(IASTNode node) {
   int endOffset = 0;
   while (true) {
     IASTFileLocation fileLocation = node.getFileLocation();
     if (fileLocation != null) endOffset = Math.max(endOffset, endOffset(fileLocation));
     List<IASTComment> comments = commentMap.getTrailingCommentsForNode(node);
     if (!comments.isEmpty()) {
       for (IASTComment comment : comments) {
         int commentEndOffset = endOffset(comment);
         if (commentEndOffset >= endOffset) {
           endOffset = commentEndOffset;
         }
       }
     }
     IASTNode[] children = node.getChildren();
     if (children.length == 0) break;
     node = children[children.length - 1];
   }
   return endOffset;
 }
예제 #6
0
 public int getEndOffsetIncludingComments(IASTNode node) {
   int endOffset = 0;
   while (true) {
     IASTFileLocation fileLocation = node.getFileLocation();
     if (fileLocation != null)
       endOffset =
           Math.max(endOffset, fileLocation.getNodeOffset() + fileLocation.getNodeLength());
     List<IASTComment> comments = trailingMap.get(node);
     if (comments != null && !comments.isEmpty()) {
       for (IASTComment comment : comments) {
         int commentEndOffset = ASTNodes.endOffset(comment);
         if (commentEndOffset >= endOffset) {
           endOffset = commentEndOffset;
         }
       }
     }
     IASTNode[] children = node.getChildren();
     if (children.length == 0) break;
     node = children[children.length - 1];
   }
   return endOffset;
 }
예제 #7
0
 private int offset(IASTNode node) {
   return node.getFileLocation().getNodeOffset();
 }
예제 #8
0
 private int endOffset(IASTNode node) {
   return endOffset(node.getFileLocation());
 }