use of com.python.pydev.analysis.refactoring.changes.PyRenameResourceChange in project Pydev by fabioz.
the class PyRenameEntryPoint method checkFinalConditions.
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException, OperationCanceledException {
// Clear (will be filled now).
allChanges.clear();
fRequest.pushMonitor(pm);
RefactoringStatus status = new RefactoringStatus();
try {
if (this.fRequest.isModuleRenameRefactoringRequest() && this.fRequest.getSimpleResourceRename() && this.fRequest.getIFileResource() != null) {
// Ok, simple resource change
return status;
}
final Map<IPath, Tuple<TextChange, MultiTextEdit>> fileToChangeInfo = new HashMap<IPath, Tuple<TextChange, MultiTextEdit>>();
final Set<IResource> affectedResources = new HashSet<>();
for (RefactoringRequest request : this.fRequest.getRequests()) {
if (request.isModuleRenameRefactoringRequest()) {
boolean searchInit = true;
IModule module = request.getTargetNature().getAstManager().getModule(request.inputName, request.getTargetNature(), !searchInit, // i.e.: the parameter is dontSearchInit (so, pass in negative form to search)
new BaseModuleRequest(request.acceptTypeshed));
if (module != null) {
String partName = module.getName().endsWith(".__init__") ? "package" : "module";
status.addFatalError("Unable to perform module rename because a " + partName + " named: " + request.inputName + " already exists.");
return status;
}
}
List<IRefactorRenameProcess> processes = pyReferenceSearcher.getProcesses(request);
if (processes == null || processes.size() == 0) {
status.addFatalError("Refactoring Process not defined: the refactoring cycle did not complete correctly.");
return status;
}
request.getMonitor().beginTask("Finding references", processes.size());
try {
pyReferenceSearcher.search(request);
} catch (PyReferenceSearcher.SearchException e) {
status.addFatalError(e.getMessage());
return status;
}
TextEditCreation textEditCreation = new TextEditCreation(request.qualifier, request.inputName, request.getModule().getName(), request.getDoc(), processes, status, request.getIFile()) {
@Override
protected Tuple<TextChange, MultiTextEdit> getTextFileChange(IFile workspaceFile, IDocument doc) {
if (workspaceFile == null) {
// used for tests
TextChange docChange = PyDocumentChange.create("Current module: " + moduleName, doc);
MultiTextEdit rootEdit = new MultiTextEdit();
docChange.setEdit(rootEdit);
docChange.setKeepPreviewEdits(true);
allChanges.add(docChange);
return new Tuple<TextChange, MultiTextEdit>(docChange, rootEdit);
}
IPath fullPath = workspaceFile.getFullPath();
Tuple<TextChange, MultiTextEdit> tuple = fileToChangeInfo.get(fullPath);
if (tuple == null) {
TextFileChange docChange = new SynchronizedTextFileChange("RenameChange: " + inputName, workspaceFile);
docChange.setTextType("py");
MultiTextEdit rootEdit = new MultiTextEdit();
docChange.setEdit(rootEdit);
docChange.setKeepPreviewEdits(true);
allChanges.add(docChange);
affectedResources.add(workspaceFile);
tuple = new Tuple<TextChange, MultiTextEdit>(docChange, rootEdit);
fileToChangeInfo.put(fullPath, tuple);
}
return tuple;
}
@Override
protected PyRenameResourceChange createResourceChange(IResource resourceToRename, String newName, RefactoringRequest request) {
IContainer target = null;
if (request instanceof ModuleRenameRefactoringRequest) {
target = ((ModuleRenameRefactoringRequest) request).getTarget();
}
PyRenameResourceChange change = new PyRenameResourceChange(resourceToRename, initialName, newName, StringUtils.format("Changing %s to %s", initialName, inputName), target);
allChanges.add(change);
affectedResources.add(resourceToRename);
return change;
}
};
textEditCreation.fillRefactoringChangeObject(request, context);
if (status.hasFatalError() || request.getMonitor().isCanceled()) {
return status;
}
}
checkResourcesToBeChanged(affectedResources, context, status);
} catch (OperationCanceledException e) {
// OK
} finally {
fRequest.popMonitor().done();
}
return status;
}
use of com.python.pydev.analysis.refactoring.changes.PyRenameResourceChange in project Pydev by fabioz.
the class PyRenameEntryPoint method createChange.
/**
* Change is actually already created in this stage.
*/
@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
if (this.fRequest.isModuleRenameRefactoringRequest() && this.fRequest.getSimpleResourceRename() && this.fRequest.getIFileResource() != null) {
IFile targetFile = this.fRequest.getIFileResource();
return new RenameResourceChange(targetFile.getFullPath(), fRequest.getInputName());
}
PyCompositeChange finalChange;
List<RefactoringRequest> requests = fRequest.getRequests();
if (requests.size() == 1) {
RefactoringRequest request = requests.get(0);
boolean makeUndo = !(request.isModuleRenameRefactoringRequest());
finalChange = new PyCompositeChange("RenameChange: '" + request.qualifier + "' to '" + request.inputName + "'", makeUndo);
} else {
boolean makeUndo = false;
finalChange = new PyCompositeChange("Move: " + requests.size() + " resources to '" + fRequest.getInputName() + "'", makeUndo);
}
Collections.sort(allChanges, new Comparator<Change>() {
@Override
public int compare(Change o1, Change o2) {
if (o1.getClass() != o2.getClass()) {
if (o1 instanceof PyRenameResourceChange) {
// The rename changes must be the last ones (all the text-related changes must be done already).
return 1;
}
if (o2 instanceof PyRenameResourceChange) {
return -1;
}
}
return o1.getName().compareTo(o2.getName());
}
});
finalChange.addAll(allChanges.toArray(new Change[allChanges.size()]));
return finalChange;
}
Aggregations