Пример #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));
 }
 /** Shifts and adds the replace edits to the target list. */
 private void shiftAndAddEdits(
     final int shift, ReplaceEdit[] stepEdits, List<ReplaceEdit> target) {
   for (int j = 0; j < stepEdits.length; j++) {
     final ReplaceEdit r = stepEdits[j];
     final String rtext = r.getText();
     target.add(new ReplaceEdit(shift + r.getOffset(), r.getLength(), rtext));
   }
 }
Пример #3
0
 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());
 }
Пример #4
0
  /**
   * Creates the text changes for all the affected files. Updates all the include statements in the
   * current file and all the includes in the "including " files. In case of folders, creates the
   * changes recursively
   *
   * @param pm - progress monitor
   * @param rootChange - the root change that the new changes are added to
   * @param sourceResources
   * @return the root change after the additions
   * @throws CoreException
   */
  private Change createTextChanges(
      IProgressMonitor pm,
      CompositeChange rootChange,
      Set<IFile> phpFiles,
      IResource[] sourceResources)
      throws CoreException {
    List<ProgramFileChange> changes = new ArrayList<ProgramFileChange>();
    try {
      pm.beginTask(PhpRefactoringCoreMessages.getString("MoveDelegate.1"), 100); // $NON-NLS-1$

      // creat text changes:
      // for each file that will be moved, update its includes
      // and update all the files that include it,

      IResource[] uniqueSourceResources = removeDuplicateResources(sourceResources);

      for (Iterator<IFile> it = phpFiles.iterator(); it.hasNext(); ) {
        IFile currentMovedResource = it.next();

        Map<IFile, Program> participantFiles = collectReferencingFiles(currentMovedResource, pm);

        for (Entry<IFile, Program> entry : participantFiles.entrySet()) {
          final IFile file = entry.getKey();
          if (phpFiles.contains(file)) {
            continue;
          }
          final Program program = entry.getValue();

          final ChangeIncludePath rename =
              new ChangeIncludePath(
                  currentMovedResource, file, fMainDestinationPath, false, uniqueSourceResources);
          // aggregate the changes identifiers
          program.accept(rename);

          if (pm.isCanceled()) throw new OperationCanceledException();

          pm.worked(1);

          if (rename.hasChanges()) {
            ProgramFileChange change = new ProgramFileChange(file.getName(), file, program);
            change.setEdit(new MultiTextEdit());
            change.setTextType("php"); // $NON-NLS-1$

            changes.add(change);
            rename.updateChange(change);
          }
        }

        ISourceModule sourceModule = DLTKCore.createSourceModuleFrom(currentMovedResource);

        if (sourceModule instanceof ISourceModule) {

          Program program = null;
          try {
            program = ASTUtils.createProgramFromSource(sourceModule);
          } catch (Exception e) {
          }

          if (program != null) {
            final ChangeIncludePath rename =
                new ChangeIncludePath(
                    currentMovedResource,
                    currentMovedResource,
                    fMainDestinationPath,
                    true,
                    uniqueSourceResources);

            // aggregate the changes identifiers
            program.accept(rename);

            if (pm.isCanceled()) throw new OperationCanceledException();

            pm.worked(1);

            if (rename.hasChanges()) {
              ProgramFileChange change =
                  new ProgramFileChange(
                      currentMovedResource.getName(), currentMovedResource, program);
              change.setEdit(new MultiTextEdit());
              change.setTextType("php"); // $NON-NLS-1$

              changes.add(change);
              rename.updateChange(change);
            }
          }
        }
      }
      pm.worked(70);

    } finally {
      pm.done();
    } // getChildren()

    Map<IFile, List<TextEdit>> changeMap = new HashMap<IFile, List<TextEdit>>();
    Map<IFile, ProgramFileChange> fileMap = new HashMap<IFile, ProgramFileChange>();
    for (ProgramFileChange programFileChange : changes) {
      List<TextEdit> list = changeMap.get(programFileChange.getFile());
      if (list == null) {
        list = new ArrayList<TextEdit>();
        changeMap.put(programFileChange.getFile(), list);
        fileMap.put(programFileChange.getFile(), programFileChange);
      } else {

      }
      list.addAll(Arrays.asList(programFileChange.getEdit().getChildren()));
    }
    for (IFile file : changeMap.keySet()) {
      ProgramFileChange change =
          new ProgramFileChange(file.getName(), file, fileMap.get(file).getProgram());
      change.setEdit(new MultiTextEdit());
      change.setTextType("php"); // $NON-NLS-1$

      List<TextEdit> list = changeMap.get(file);
      Collections.sort(
          list,
          new Comparator<TextEdit>() {
            public int compare(TextEdit o1, TextEdit o2) {
              return o2.getOffset() - o1.getOffset();
            }
          });

      for (TextEdit textEdit : list) {
        if (textEdit instanceof ReplaceEdit) {
          ReplaceEdit replaceEdit = (ReplaceEdit) textEdit;
          change.addEdit(
              new ReplaceEdit(
                  replaceEdit.getOffset(), replaceEdit.getLength(), replaceEdit.getText()));
        }
      }
      rootChange.add(change);
    }
    return rootChange;
  }