Search in sources :

Example 1 with PyRefactoringRequest

use of org.python.pydev.ast.refactoring.PyRefactoringRequest in project Pydev by fabioz.

the class RefactoringLocalTestBase method applyRenameRefactoring.

/**
 * Applies a rename refactoring
 */
protected void applyRenameRefactoring(RefactoringRequest request, boolean expectError) throws CoreException {
    PyRenameEntryPoint processor = new PyRenameEntryPoint(new PyRefactoringRequest(request));
    NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
    checkStatus(processor.checkInitialConditions(nullProgressMonitor), expectError);
    checkStatus(processor.checkFinalConditions(nullProgressMonitor, null), expectError);
    Change change = processor.createChange(nullProgressMonitor);
    if (!expectError) {
        // otherwise, if there is an error, the change may be null
        change.perform(nullProgressMonitor);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint) Change(org.eclipse.ltk.core.refactoring.Change) PyRefactoringRequest(org.python.pydev.ast.refactoring.PyRefactoringRequest)

Example 2 with PyRefactoringRequest

use of org.python.pydev.ast.refactoring.PyRefactoringRequest in project Pydev by fabioz.

the class PyRenameRefactoring method rename.

public static String rename(IPyRefactoringRequest request) {
    try {
        List<RefactoringRequest> actualRequests = request.getRequests();
        if (actualRequests.size() == 1) {
            RefactoringRequest req = actualRequests.get(0);
            // Note: if it's already a ModuleRenameRefactoringRequest, no need to change anything.
            if (!(req.isModuleRenameRefactoringRequest())) {
                // Note: if we're renaming an import, we must change to the appropriate req
                IPyRefactoring pyRefactoring = AbstractPyRefactoring.getPyRefactoring();
                ItemPointer[] pointers = pyRefactoring.findDefinition(req);
                for (ItemPointer pointer : pointers) {
                    Definition definition = pointer.definition;
                    if (RefactorProcessFactory.isModuleRename(definition)) {
                        try {
                            request = new PyRefactoringRequest(new ModuleRenameRefactoringRequest(definition.module.getFile(), req.nature, null));
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
        PyRenameEntryPoint entryPoint = new PyRenameEntryPoint(request);
        RenameRefactoring renameRefactoring = new RenameRefactoring(entryPoint);
        request.fillActivationTokenAndQualifier();
        String title = "Rename";
        if (request instanceof MultiModuleMoveRefactoringRequest) {
            MultiModuleMoveRefactoringRequest multiModuleMoveRefactoringRequest = (MultiModuleMoveRefactoringRequest) request;
            title = "Move To package (project: " + multiModuleMoveRefactoringRequest.getTarget().getProject().getName() + ")";
        }
        final PyRenameRefactoringWizard wizard = new PyRenameRefactoringWizard(renameRefactoring, title, "inputPageDescription", request);
        try {
            RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
            op.run(EditorUtils.getShell(), "Rename Refactor Action");
        } catch (InterruptedException e) {
        // do nothing. User action got cancelled
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return null;
}
Also used : MultiModuleMoveRefactoringRequest(org.python.pydev.ast.refactoring.MultiModuleMoveRefactoringRequest) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) MultiModuleMoveRefactoringRequest(org.python.pydev.ast.refactoring.MultiModuleMoveRefactoringRequest) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) PyRefactoringRequest(org.python.pydev.ast.refactoring.PyRefactoringRequest) IPyRefactoringRequest(org.python.pydev.ast.refactoring.IPyRefactoringRequest) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) IPyRefactoring(org.python.pydev.ast.refactoring.IPyRefactoring) IOException(java.io.IOException) IOException(java.io.IOException) RefactoringWizardOpenOperation(org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint) PyRefactoringRequest(org.python.pydev.ast.refactoring.PyRefactoringRequest) IPyRefactoringRequest(org.python.pydev.ast.refactoring.IPyRefactoringRequest) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 3 with PyRefactoringRequest

use of org.python.pydev.ast.refactoring.PyRefactoringRequest in project Pydev by fabioz.

the class PyRenameResourceAction method run.

/*
     * (non-Javadoc) Method declared on IAction.
     */
@Override
public void run() {
    if (!isEnabled()) {
        // will also update the list of resources (main change from the DeleteResourceAction)
        return;
    }
    IEditorPart[] dirtyEditors = Helpers.checkValidateState();
    List<IResource> resources = getSelectedResources();
    if (resources.size() != 1) {
        DialogHelpers.openWarning("Can only rename one element.", "One element must be selected for rename.");
        return;
    }
    IResource r = resources.get(0);
    if (r instanceof IFile) {
        for (IEditorPart iEditorPart : dirtyEditors) {
            IEditorInput editorInput = iEditorPart.getEditorInput();
            Object input = editorInput.getAdapter(IResource.class);
            if (r.equals(input)) {
                iEditorPart.doSave(null);
            }
        }
    } else if (r instanceof IFolder) {
        try {
            renamedFolder = (IFolder) r;
            preResources = new ArrayList<IResource>();
            IResource[] members = renamedFolder.getParent().members();
            for (IResource m : members) {
                preResources.add(m);
            }
        } catch (CoreException e) {
            Log.log(IStatus.ERROR, "Unexpected error reading parent properties", e);
            renamedFolder = null;
            preResources = null;
        }
    } else {
        renamedFolder = null;
        preResources = null;
    }
    IProject project = r.getProject();
    PythonNature n = PythonNature.getPythonNature(project);
    if (n != null) {
        if (r instanceof IFile && !PythonPathHelper.isValidSourceFile((IFile) r)) {
        // If it is a file which does not end with .py, don't try to do a regular refactoring.
        } else {
            try {
                String resolveModule = n.resolveModule(r);
                if (resolveModule != null && // -- the folder has to be selected to do a package rename
                !resolveModule.endsWith(".__init__")) {
                    IFile file = null;
                    boolean foundAsInit = false;
                    if (r instanceof IContainer) {
                        file = PythonPathHelper.getFolderInit((IContainer) r);
                        foundAsInit = true;
                    } else if (r instanceof IFile) {
                        file = (IFile) r;
                    }
                    if (file != null && file.exists()) {
                        // It's a directory without an __init__.py file, just keep going...
                        RefactoringRequest request = new ModuleRenameRefactoringRequest(file.getLocation().toFile(), n, null);
                        if (!foundAsInit) {
                            // If we have found it as an __init__ when renaming a module, we won't
                            // set the related IFile (because we don't want to provide a 'simple rename'
                            // in this case -- as if he did actually select the __init__, only the simple
                            // rename would be provided in the first place).
                            request.setFileResource(file);
                        }
                        PyRenameRefactoring.rename(new PyRefactoringRequest(request));
                        // route and do a refactoring request to rename it)!
                        return;
                    }
                }
            } catch (Exception e) {
                Log.log(e);
            }
        }
    }
    super.run();
    updatePyPath();
    renamedFolder = null;
    preResources = null;
}
Also used : IFile(org.eclipse.core.resources.IFile) PythonNature(org.python.pydev.plugin.nature.PythonNature) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) PyRefactoringRequest(org.python.pydev.ast.refactoring.PyRefactoringRequest) ArrayList(java.util.ArrayList) IEditorPart(org.eclipse.ui.IEditorPart) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) CoreException(org.eclipse.core.runtime.CoreException) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) IContainer(org.eclipse.core.resources.IContainer) PyRefactoringRequest(org.python.pydev.ast.refactoring.PyRefactoringRequest) IResource(org.eclipse.core.resources.IResource) IEditorInput(org.eclipse.ui.IEditorInput) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

PyRefactoringRequest (org.python.pydev.ast.refactoring.PyRefactoringRequest)3 PyRenameEntryPoint (com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint)2 ModuleRenameRefactoringRequest (org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest)2 RefactoringRequest (org.python.pydev.ast.refactoring.RefactoringRequest)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 IContainer (org.eclipse.core.resources.IContainer)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IProject (org.eclipse.core.resources.IProject)1 IResource (org.eclipse.core.resources.IResource)1 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Change (org.eclipse.ltk.core.refactoring.Change)1 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)1 RefactoringWizardOpenOperation (org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation)1 IEditorInput (org.eclipse.ui.IEditorInput)1 IEditorPart (org.eclipse.ui.IEditorPart)1 Definition (org.python.pydev.ast.codecompletion.revisited.visitors.Definition)1 ItemPointer (org.python.pydev.ast.item_pointer.ItemPointer)1