Search in sources :

Example 1 with PyRenameEntryPoint

use of com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint 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 PyRenameEntryPoint

use of com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint in project Pydev by fabioz.

the class PyRenameInFileAction method fillWithOccurrences.

/**
 * Puts the found positions referente to the occurrences in the group
 *
 * @param document the document that will contain this positions
 * @param group the group that will contain this positions
 * @param ps the selection used
 * @return
 *
 * @throws BadLocationException
 * @throws OperationCanceledException
 * @throws CoreException
 * @throws MisconfigurationException
 */
private boolean fillWithOccurrences(IDocument document, LinkedPositionGroup group, IProgressMonitor monitor, PySelection ps) throws BadLocationException, OperationCanceledException, CoreException, MisconfigurationException {
    RefactoringRequest req = MarkOccurrencesJob.getRefactoringRequest(pyEdit, MarkOccurrencesJob.getRefactorAction(pyEdit), ps);
    if (monitor.isCanceled()) {
        return false;
    }
    PyRenameEntryPoint processor = new PyRenameEntryPoint(req);
    // process it to get what we need
    processor.checkInitialConditions(monitor);
    processor.checkFinalConditions(monitor, null);
    HashSet<ASTEntry> occurrences = processor.getOccurrences();
    if (monitor.isCanceled()) {
        return false;
    }
    // used so that we don't add duplicates
    Set<Tuple<Integer, Integer>> found = new HashSet<Tuple<Integer, Integer>>();
    List<ProposalPosition> groupPositions = new ArrayList<ProposalPosition>();
    if (occurrences != null) {
        // first, just sort by position (line, col)
        ArrayList<ASTEntry> sortedOccurrences = new ArrayList<ASTEntry>(occurrences);
        Collections.sort(sortedOccurrences, new Comparator<ASTEntry>() {

            @Override
            public int compare(ASTEntry o1, ASTEntry o2) {
                int thisVal = o1.node.beginLine;
                int anotherVal = o2.node.beginLine;
                int ret;
                if (thisVal == anotherVal) {
                    // if it's in the same line, let's sort by column
                    thisVal = o1.node.beginColumn;
                    anotherVal = o2.node.beginColumn;
                    ret = (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
                } else {
                    ret = (thisVal < anotherVal ? -1 : 1);
                }
                return ret;
            }
        });
        // now, gather positions to add to the group
        int i = 0;
        int firstPosition = -1;
        int absoluteCursorOffset = ps.getAbsoluteCursorOffset();
        for (ASTEntry entry : sortedOccurrences) {
            try {
                IRegion lineInformation = document.getLineInformation(entry.node.beginLine - 1);
                int colDef = NodeUtils.getClassOrFuncColDefinition(entry.node) - 1;
                int offset = lineInformation.getOffset() + colDef;
                int len = req.qualifier.length();
                Tuple<Integer, Integer> foundAt = new Tuple<Integer, Integer>(offset, len);
                if (!found.contains(foundAt)) {
                    i++;
                    ProposalPosition proposalPosition = new ProposalPosition(document, offset, len, i, new ICompletionProposal[0]);
                    found.add(foundAt);
                    groupPositions.add(proposalPosition);
                    if (offset <= absoluteCursorOffset && absoluteCursorOffset < offset + len) {
                        firstPosition = i;
                    }
                }
            } catch (Exception e) {
                Log.log(e);
                return false;
            }
        }
        if (firstPosition != -1) {
            ArrayList<ProposalPosition> newGroupPositions = new ArrayList<ProposalPosition>();
            // add from current to end
            for (i = firstPosition - 1; i < groupPositions.size(); i++) {
                newGroupPositions.add(groupPositions.get(i));
            }
            // and now from the start up to the current
            for (i = 0; i < firstPosition - 1; i++) {
                newGroupPositions.add(groupPositions.get(i));
            }
            groupPositions = newGroupPositions;
        }
        for (ProposalPosition proposalPosition : groupPositions) {
            group.addPosition(proposalPosition);
        }
    }
    return groupPositions.size() > 0;
}
Also used : RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ArrayList(java.util.ArrayList) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint) IRegion(org.eclipse.jface.text.IRegion) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ProposalPosition(org.eclipse.jface.text.link.ProposalPosition) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Example 3 with PyRenameEntryPoint

use of com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint 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 4 with PyRenameEntryPoint

use of com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint in project Pydev by fabioz.

the class RefactoringRenameTestBase method getReferencesForRename.

/**
 * Goes through all the workspace (in this case the refactoring project) and gathers the references
 * for the current selection.
 *
 * @param moduleName the name of the module we're currently in
 * @param line the line we're in
 * @param col the col we're in
 * @param expectError whether we are expecting some error or not
 * @return a map with the name of the module and the file representing it pointing to the
 * references found in that module.
 */
protected Map<Tuple<String, File>, HashSet<ASTEntry>> getReferencesForRename(String moduleName, int line, int col, boolean expectError) {
    Map<Tuple<String, File>, HashSet<ASTEntry>> occurrencesToReturn = null;
    try {
        IProjectModulesManager modulesManager = (IProjectModulesManager) natureRefactoring.getAstManager().getModulesManager();
        IModule module = modulesManager.getModuleInDirectManager(moduleName, natureRefactoring, true, new BaseModuleRequest(true));
        if (module == null) {
            throw new RuntimeException("Unable to get source module for module:" + moduleName);
        }
        String strDoc = FileUtils.getFileContents(module.getFile());
        Document doc = new Document(strDoc);
        PySelection ps = new PySelection(doc, line, col);
        RefactoringRequest request = new RefactoringRequest(null, ps, natureRefactoring);
        request.setAdditionalInfo(RefactoringRequest.FIND_REFERENCES_ONLY_IN_LOCAL_SCOPE, false);
        request.moduleName = moduleName;
        request.inputName = "new_name";
        request.fillActivationTokenAndQualifier();
        PyRenameEntryPoint processor = new PyRenameEntryPoint(request);
        NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
        checkStatus(processor.checkInitialConditions(nullProgressMonitor), expectError);
        lastProcessorUsed = processor;
        checkProcessors();
        checkStatus(processor.checkFinalConditions(nullProgressMonitor, null), expectError);
        occurrencesToReturn = processor.getOccurrencesInOtherFiles();
        occurrencesToReturn.put(new Tuple<String, File>(moduleName, module.getFile()), processor.getOccurrences());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return occurrencesToReturn;
}
Also used : IProjectModulesManager(org.python.pydev.core.IProjectModulesManager) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IModule(org.python.pydev.core.IModule) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) Document(org.eclipse.jface.text.Document) MisconfigurationException(org.python.pydev.core.MisconfigurationException) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint) PySelection(org.python.pydev.core.docutils.PySelection) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Example 5 with PyRenameEntryPoint

use of com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint in project Pydev by fabioz.

the class RenameModuleRefactoringTest method getReferencesForModuleRename.

protected Map<Tuple<String, File>, HashSet<ASTEntry>> getReferencesForModuleRename(String moduleName, String newName, boolean expectError) {
    Map<Tuple<String, File>, HashSet<ASTEntry>> occurrencesToReturn = null;
    try {
        IProjectModulesManager modulesManager = (IProjectModulesManager) natureRefactoring.getAstManager().getModulesManager();
        BaseModuleRequest moduleRequest = new BaseModuleRequest(true);
        IModule module = modulesManager.getModuleInDirectManager(moduleName, natureRefactoring, true, moduleRequest);
        if (module == null) {
            if (!moduleName.endsWith("__init__")) {
                module = modulesManager.getModuleInDirectManager(moduleName + ".__init__", natureRefactoring, true, moduleRequest);
            }
            if (module == null) {
                throw new RuntimeException("Unable to get source module for module:" + moduleName);
            }
        }
        ModuleRenameRefactoringRequest request = new ModuleRenameRefactoringRequest(module.getFile(), natureRefactoring, null);
        request.setAdditionalInfo(RefactoringRequest.FIND_REFERENCES_ONLY_IN_LOCAL_SCOPE, false);
        request.moduleName = moduleName;
        request.fillActivationTokenAndQualifier();
        request.inputName = newName;
        PyRenameEntryPoint processor = new PyRenameEntryPoint(request);
        NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
        checkStatus(processor.checkInitialConditions(nullProgressMonitor), expectError);
        lastProcessorUsed = processor;
        checkProcessors();
        checkStatus(processor.checkFinalConditions(nullProgressMonitor, null), expectError);
        occurrencesToReturn = processor.getOccurrencesInOtherFiles();
        occurrencesToReturn.put(new Tuple<String, File>(CURRENT_MODULE_IN_REFERENCES, null), processor.getOccurrences());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return occurrencesToReturn;
}
Also used : IProjectModulesManager(org.python.pydev.core.IProjectModulesManager) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IModule(org.python.pydev.core.IModule) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Aggregations

PyRenameEntryPoint (com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint)5 HashSet (java.util.HashSet)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 RefactoringRequest (org.python.pydev.ast.refactoring.RefactoringRequest)3 Tuple (org.python.pydev.shared_core.structure.Tuple)3 File (java.io.File)2 ModuleRenameRefactoringRequest (org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest)2 PyRefactoringRequest (org.python.pydev.ast.refactoring.PyRefactoringRequest)2 BaseModuleRequest (org.python.pydev.core.BaseModuleRequest)2 IModule (org.python.pydev.core.IModule)2 IProjectModulesManager (org.python.pydev.core.IProjectModulesManager)2 MisconfigurationException (org.python.pydev.core.MisconfigurationException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Document (org.eclipse.jface.text.Document)1 IRegion (org.eclipse.jface.text.IRegion)1