Search in sources :

Example 1 with PyRenameResourceChange

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;
}
Also used : IModule(org.python.pydev.core.IModule) IFile(org.eclipse.core.resources.IFile) PyRenameResourceChange(com.python.pydev.analysis.refactoring.changes.PyRenameResourceChange) HashMap(java.util.HashMap) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) PyRefactoringRequest(org.python.pydev.ast.refactoring.PyRefactoringRequest) IPyRefactoringRequest(org.python.pydev.ast.refactoring.IPyRefactoringRequest) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) IRefactorRenameProcess(com.python.pydev.analysis.refactoring.wizards.IRefactorRenameProcess) IContainer(org.eclipse.core.resources.IContainer) HashSet(java.util.HashSet) IPath(org.eclipse.core.runtime.IPath) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) SynchronizedTextFileChange(org.python.pydev.refactoring.core.base.SynchronizedTextFileChange) SynchronizedTextFileChange(org.python.pydev.refactoring.core.base.SynchronizedTextFileChange) Tuple(org.python.pydev.shared_core.structure.Tuple) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with PyRenameResourceChange

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;
}
Also used : IFile(org.eclipse.core.resources.IFile) PyRenameResourceChange(com.python.pydev.analysis.refactoring.changes.PyRenameResourceChange) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) PyRefactoringRequest(org.python.pydev.ast.refactoring.PyRefactoringRequest) IPyRefactoringRequest(org.python.pydev.ast.refactoring.IPyRefactoringRequest) PyRenameResourceChange(com.python.pydev.analysis.refactoring.changes.PyRenameResourceChange) RenameResourceChange(org.eclipse.ltk.core.refactoring.resource.RenameResourceChange) PyDocumentChange(org.python.pydev.refactoring.core.base.PyDocumentChange) PyRenameResourceChange(com.python.pydev.analysis.refactoring.changes.PyRenameResourceChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) Change(org.eclipse.ltk.core.refactoring.Change) RenameResourceChange(org.eclipse.ltk.core.refactoring.resource.RenameResourceChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) SynchronizedTextFileChange(org.python.pydev.refactoring.core.base.SynchronizedTextFileChange) PyCompositeChange(com.python.pydev.analysis.refactoring.changes.PyCompositeChange) PyCompositeChange(com.python.pydev.analysis.refactoring.changes.PyCompositeChange)

Aggregations

PyRenameResourceChange (com.python.pydev.analysis.refactoring.changes.PyRenameResourceChange)2 IFile (org.eclipse.core.resources.IFile)2 TextChange (org.eclipse.ltk.core.refactoring.TextChange)2 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)2 IPyRefactoringRequest (org.python.pydev.ast.refactoring.IPyRefactoringRequest)2 ModuleRenameRefactoringRequest (org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest)2 PyRefactoringRequest (org.python.pydev.ast.refactoring.PyRefactoringRequest)2 RefactoringRequest (org.python.pydev.ast.refactoring.RefactoringRequest)2 SynchronizedTextFileChange (org.python.pydev.refactoring.core.base.SynchronizedTextFileChange)2 PyCompositeChange (com.python.pydev.analysis.refactoring.changes.PyCompositeChange)1 IRefactorRenameProcess (com.python.pydev.analysis.refactoring.wizards.IRefactorRenameProcess)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IContainer (org.eclipse.core.resources.IContainer)1 IResource (org.eclipse.core.resources.IResource)1 IPath (org.eclipse.core.runtime.IPath)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IDocument (org.eclipse.jface.text.IDocument)1 Change (org.eclipse.ltk.core.refactoring.Change)1 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)1