Exemplo n.º 1
0
  /**
   * Adds the text and move changes to the root change This change is the a more global change, it
   * includes both the file(s) move, the update of it's includes and update all the references, all
   * the files that have includes to the moved file.
   *
   * @param pm - progress monitor
   * @param rootChange - the root change that the new changes are added to
   * @return the root change after the additions
   */
  private Change createReferenceUpdatingMoveChange(IProgressMonitor pm, CompositeChange rootChange)
      throws CoreException, OperationCanceledException {
    try {
      pm.beginTask(PhpRefactoringCoreMessages.getString("MoveDelegate.0"), 100); // $NON-NLS-1$

      IResource[] sourceResources = fProcessor.getSourceSelection();

      createTextChanges(new SubProgressMonitor(pm, 80), rootChange, phpFiles, sourceResources);
      pm.worked(80);

      // update configuration file.
      createRunConfigurationChange(sourceResources, rootChange);

      // There is a tricky thing here.
      // The resource move must be happened after text change, and run
      // configuration changes(this is because the share file under the
      // project)
      // but before the other changes, e.g. break point and etc.
      createMoveChange(sourceResources, rootChange);

      // update associated break point.
      createBreakPointChange(sourceResources, rootChange);

      createBuildPathChange(sourceResources, rootChange);

      createRenameLibraryFolderChange(sourceResources, rootChange);

      pm.worked(20);

    } finally {
      pm.done();
    }
    return rootChange;
  }
Exemplo n.º 2
0
  /**
   * Adds the move changes to the root change This change is the proper move change, nothing else
   *
   * @param pm - progress monitor
   * @param rootChange - the root change that the new changes are added to
   * @return the root change after the additions
   */
  private Change createSimpleMoveChange(final IProgressMonitor pm, final CompositeChange rootChange)
      throws CoreException, OperationCanceledException {
    try {
      pm.beginTask(PhpRefactoringCoreMessages.getString("MoveDelegate.0"), 100); // $NON-NLS-1$

      IResource[] sourceResources = fProcessor.getSourceSelection();
      createMoveChange(sourceResources, rootChange);
      pm.worked(100);

    } finally {
      pm.done();
    }
    return rootChange;
  }
Exemplo n.º 3
0
  /**
   * Initializes DLTKCore internal structures to allow subsequent operations (such as the ones that
   * need a resolved classpath) to run full speed. A client may choose to call this method in a
   * background thread early after the workspace has started so that the initialization is
   * transparent to the user.
   *
   * <p>However calling this method is optional. Services will lazily perform initialization when
   * invoked. This is only a way to reduce initialization overhead on user actions, if it can be
   * performed before at some appropriate moment.
   *
   * <p>This initialization runs accross all Java projects in the workspace. Thus the workspace root
   * scheduling rule is used during this operation.
   *
   * <p>This method may return before the initialization is complete. The initialization will then
   * continue in a background thread.
   *
   * <p>This method can be called concurrently.
   *
   * @param monitor a progress monitor, or <code>null</code> if progress reporting and cancellation
   *     are not desired
   * @exception CoreException if the initialization fails, the status of the exception indicates the
   *     reason of the failure
   * @since 3.1
   */
  public static void initializeAfterLoad(IProgressMonitor monitor) throws CoreException {
    try {
      if (monitor != null) {
        monitor.beginTask(CoreMessages.PHPCorePlugin_initializingPHPToolkit, 125);
      }

      // dummy query for waiting until the indexes are ready
      IDLTKSearchScope scope = SearchEngine.createWorkspaceScope(PHPLanguageToolkit.getDefault());
      try {
        LanguageModelInitializer.cleanup(monitor);
        if (monitor != null) {
          monitor.subTask(CoreMessages.PHPCorePlugin_initializingSearchEngine);
          monitor.worked(25);
        }

        PhpModelAccess.getDefault()
            .findMethods(ID, MatchRule.PREFIX, Modifiers.AccGlobal, 0, scope, monitor);
        if (monitor != null) {
          monitor.worked(25);
        }

        PhpModelAccess.getDefault()
            .findTypes(ID, MatchRule.PREFIX, Modifiers.AccGlobal, 0, scope, monitor);
        if (monitor != null) {
          monitor.worked(25);
        }

        PhpModelAccess.getDefault()
            .findFields(ID, MatchRule.PREFIX, Modifiers.AccGlobal, 0, scope, monitor);
        if (monitor != null) {
          monitor.worked(25);
        }

        PhpModelAccess.getDefault().findIncludes(ID, MatchRule.PREFIX, scope, monitor);
        if (monitor != null) {
          monitor.worked(25);
        }

      } catch (OperationCanceledException e) {
        if (monitor != null && monitor.isCanceled()) {
          throw e;
        }
        // else indexes were not ready: catch the exception so that jars
        // are still refreshed
      }
    } finally {
      if (monitor != null) {
        monitor.done();
      }
      toolkitInitialized = true;
    }
  }
Exemplo n.º 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;
  }