Skip to content

Commit

Permalink
Progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
khatchad committed Jan 17, 2025
1 parent 1f3ec91 commit 5fbac95
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CheckingPreconditions=Checking preconditions (may take a while)...
CompilingSource=Compiling source...
CheckingPreconditions=Checking preconditions (may take a while)...
CreatingChange=Creating change...
CUContainsCompileErrors=Member ''{0}'' has a compilation unit ''{1}'' that contains compilation errors.
RefactoringNotPossible=Refactoring not possible.
ClearingCaches=Clearing caches...
NoElementsToRefactor=No elements have passed preconditions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package edu.cuny.hunter.refactoring.common.java.core;

import static edu.cuny.citytech.refactoring.common.core.Messages.CheckingPreconditions;
import static edu.cuny.citytech.refactoring.common.core.Messages.NoElementsToRefactor;
import static java.util.stream.Collectors.toSet;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -23,6 +26,8 @@
import org.eclipse.jdt.internal.corext.refactoring.util.TextEditBasedChangeManager;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.NullChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;

import edu.cuny.citytech.refactoring.common.core.Messages;

Expand All @@ -31,7 +36,6 @@
*
* @author <a href="mailto:[email protected]">Raffi Khatchadourian</a>
*/
@SuppressWarnings("restriction")
public abstract class RefactoringProcessor extends edu.cuny.citytech.refactoring.common.core.RefactoringProcessor {

protected Set<RefactorableEntity> entities = new HashSet<>();
Expand Down Expand Up @@ -92,12 +96,42 @@ protected static void manageCompilationUnit(final TextEditBasedChangeManager man
manager.manage(rewrite.getCu(), change);
}

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor monitor, CheckConditionsContext context)
throws CoreException, OperationCanceledException {
RefactoringStatus status = new RefactoringStatus();
SubMonitor progress = SubMonitor.convert(monitor, CheckingPreconditions, 1);

// get the status of each stream.
RefactoringStatus collectedStatus = this.getEntities().parallelStream().map(RefactorableEntity::getStatus)
.collect(() -> new RefactoringStatus(), (a, b) -> a.merge(b), (a, b) -> a.merge(b));
status.merge(collectedStatus);
progress.worked(1);

// if there are no fatal errors.
if (!status.hasFatalError()) {
// these are the entities passing preconditions.
Set<RefactorableEntity> passingEntities = this.getPassingEntities();

// add a fatal error if there are no passing entities.
if (passingEntities.isEmpty())
status.addFatalError(Messages.NoElementsToRefactor);
else {
// TODO:
// Checks.addModifiedFilesToChecker(ResourceUtil.getFiles(fChangeManager.getAllCompilationUnits()),
// context);
}
}

return status;
}

@Override
public Change createChange(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
try {
SubMonitor progress = SubMonitor.convert(monitor, Messages.CreatingChange, 100);

Set<RefactorableEntity> entities = this.getEntities();
Set<RefactorableEntity> entities = this.getPassingEntities();

if (entities.isEmpty())
return new NullChange(NoElementsToRefactor);
Expand All @@ -107,9 +141,7 @@ public Change createChange(IProgressMonitor monitor) throws CoreException, Opera
for (RefactorableEntity entity : entities) {
CompilationUnitRewrite rewrite = this.getCompilationUnitRewrite(entity.getCompilationUnit(),
entity.getEnclosingCompilationUnit());

entity.transform(rewrite);

transformProgress.worked(1);
}

Expand All @@ -120,7 +152,6 @@ public Change createChange(IProgressMonitor monitor) throws CoreException, Opera
.filter(cu -> !manager.containsChangesIn(cu)).toArray(ICompilationUnit[]::new);

SubMonitor saveProgress = progress.split(30).setWorkRemaining(units.length * 2);

for (ICompilationUnit cu : units) {
CompilationUnit compilationUnit = this.getCompilationUnit(cu, saveProgress.split(1));
manageCompilationUnit(manager, this.getCompilationUnitRewrite(cu, compilationUnit), saveProgress.split(1));
Expand All @@ -142,4 +173,19 @@ public Object[] getElements() {
protected Set<RefactorableEntity> getEntities() {
return entities;
}

/**
* @return True iff the {@link RefactorableEntity} {@link Set} changed as a result of this call.
* @see java.util.Set#addAll
*/
protected boolean addEntities(Set<? extends RefactorableEntity> entities) {
return this.getEntities().addAll(entities);
}

public Set<RefactorableEntity> getPassingEntities() {
Set<RefactorableEntity> entities = this.getEntities();

return entities == null ? Collections.emptySet()
: entities.parallelStream().filter(s -> !s.getStatus().hasError()).collect(toSet());
}
}

0 comments on commit 5fbac95

Please sign in to comment.