Search in sources :

Example 1 with PyReferenceSearcher

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

the class Refactorer method findAllOccurrences.

@Override
public Map<Tuple<String, File>, HashSet<ASTEntry>> findAllOccurrences(RefactoringRequest req) throws OperationCanceledException, CoreException {
    PyReferenceSearcher pyReferenceSearcher = new PyReferenceSearcher(req);
    // to see if a new request was not created in the meantime (in which case this one will be cancelled)
    req.checkCancelled();
    IProgressMonitor monitor = req.getMonitor();
    Map<Tuple<String, File>, HashSet<ASTEntry>> occurrencesInOtherFiles;
    try {
        monitor.beginTask("Find all occurrences", 100);
        monitor.setTaskName("Find all occurrences");
        try {
            req.pushMonitor(new SubProgressMonitor(monitor, 10));
            pyReferenceSearcher.prepareSearch(req);
        } catch (PyReferenceSearcher.SearchException | BadLocationException e) {
            return null;
        } finally {
            req.popMonitor().done();
        }
        req.checkCancelled();
        try {
            req.pushMonitor(new SubProgressMonitor(monitor, 85));
            pyReferenceSearcher.search(req);
        } catch (PyReferenceSearcher.SearchException e) {
            return null;
        } finally {
            req.popMonitor().done();
        }
        req.checkCancelled();
        occurrencesInOtherFiles = pyReferenceSearcher.getWorkspaceReferences(req);
        HashSet<ASTEntry> occurrences = pyReferenceSearcher.getLocalReferences(req);
        occurrencesInOtherFiles.put(new Tuple<String, File>(req.moduleName, req.pyEdit != null ? req.pyEdit.getEditorFile() : req.file), occurrences);
        req.getMonitor().worked(5);
    } finally {
        monitor.done();
    }
    return occurrencesInOtherFiles;
}
Also used : SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) PyReferenceSearcher(com.python.pydev.analysis.refactoring.wizards.rename.PyReferenceSearcher) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple) BadLocationException(org.eclipse.jface.text.BadLocationException) HashSet(java.util.HashSet)

Example 2 with PyReferenceSearcher

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

the class MarkOccurrencesJob method createRequest.

/**
 * @return a tuple with the refactoring request, the processor and a boolean indicating if all pre-conditions succedded.
 * @throws MisconfigurationException
 */
@Override
protected MarkOccurrencesRequest createRequest(BaseEditor baseEditor, IDocumentProvider documentProvider, IProgressMonitor monitor) throws BadLocationException, OperationCanceledException, CoreException, MisconfigurationException {
    if (!MarkOccurrencesPreferencesPage.useMarkOccurrences()) {
        return new PyMarkOccurrencesRequest(false, null, null);
    }
    PyEdit pyEdit = (PyEdit) baseEditor;
    // ok, the editor is still there wit ha document... move on
    PyRefactorAction pyRefactorAction = getRefactorAction(pyEdit);
    String currToken = this.ps.getCurrToken().o1;
    if (LOCAL_TEXT_SEARCHES_ON.contains(currToken) && IDocument.DEFAULT_CONTENT_TYPE.equals(ParsingUtils.getContentType(this.ps.getDoc(), this.ps.getAbsoluteCursorOffset()))) {
        return new TextBasedLocalMarkOccurrencesRequest(currToken);
    }
    final RefactoringRequest req = getRefactoringRequest(pyEdit, pyRefactorAction, PySelection.fromTextSelection(this.ps));
    if (req == null || !req.nature.getRelatedInterpreterManager().isConfigured()) {
        // we check if it's configured because it may still be a stub...
        return new PyMarkOccurrencesRequest(false, null, null);
    }
    PyReferenceSearcher searcher = new PyReferenceSearcher(req);
    // to see if a new request was not created in the meantime (in which case this one will be cancelled)
    if (monitor.isCanceled()) {
        return new PyMarkOccurrencesRequest(false, null, null);
    }
    try {
        searcher.prepareSearch(req);
        if (monitor.isCanceled()) {
            return new PyMarkOccurrencesRequest(false, null, null);
        }
        searcher.search(req);
        if (monitor.isCanceled()) {
            return new PyMarkOccurrencesRequest(false, null, null);
        }
        // Ok, search succeeded.
        return new PyMarkOccurrencesRequest(true, req, searcher);
    } catch (PyReferenceSearcher.SearchException | BadLocationException e) {
        // Suppress search failures.
        return new PyMarkOccurrencesRequest(false, null, null);
    } catch (Throwable e) {
        throw new RuntimeException("Error in occurrences while analyzing modName:" + req.moduleName + " initialName:" + req.qualifier + " line (start at 0):" + req.ps.getCursorLine(), e);
    }
}
Also used : RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) PyReferenceSearcher(com.python.pydev.analysis.refactoring.wizards.rename.PyReferenceSearcher) PyRefactorAction(org.python.pydev.editor.actions.refactoring.PyRefactorAction) PyEdit(org.python.pydev.editor.PyEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with PyReferenceSearcher

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

the class FindActualDefinitionTest method testOccurrencesForDefinition.

public void testOccurrencesForDefinition() throws Exception {
    int usedGrammar = GRAMMAR_TO_USE_FOR_PARSING;
    GRAMMAR_TO_USE_FOR_PARSING = PythonNature.LATEST_GRAMMAR_PY3_VERSION;
    try {
        File file = new File(TestDependent.TEST_PYSRC_TESTING_LOC + "occurrencesForDefinition/foo.py");
        PySelection selection = new PySelection(new Document(FileUtils.getFileContents(file)), 0, 19, 4);
        Refactorer.setPyRefactoring(new Refactorer());
        RefactoringRequest req = new RefactoringRequest(file, selection, nature);
        req.fillActivationTokenAndQualifier();
        req.setAdditionalInfo(RefactoringRequest.FIND_DEFINITION_IN_ADDITIONAL_INFO, false);
        req.setAdditionalInfo(RefactoringRequest.FIND_REFERENCES_ONLY_IN_LOCAL_SCOPE, true);
        PyReferenceSearcher pyReferenceSearcher = new PyReferenceSearcher(req);
        pyReferenceSearcher.prepareSearch(req);
        pyReferenceSearcher.search(req);
        HashSet<ASTEntry> ref = pyReferenceSearcher.getLocalReferences(req);
        assertEquals(4, ref.size());
    } finally {
        GRAMMAR_TO_USE_FOR_PARSING = usedGrammar;
    }
}
Also used : RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) PyReferenceSearcher(com.python.pydev.analysis.refactoring.wizards.rename.PyReferenceSearcher) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) PySelection(org.python.pydev.core.docutils.PySelection) Document(org.eclipse.jface.text.Document) File(java.io.File) Refactorer(com.python.pydev.analysis.refactoring.refactorer.Refactorer)

Aggregations

PyReferenceSearcher (com.python.pydev.analysis.refactoring.wizards.rename.PyReferenceSearcher)3 File (java.io.File)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 RefactoringRequest (org.python.pydev.ast.refactoring.RefactoringRequest)2 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)2 Refactorer (com.python.pydev.analysis.refactoring.refactorer.Refactorer)1 HashSet (java.util.HashSet)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1 Document (org.eclipse.jface.text.Document)1 PySelection (org.python.pydev.core.docutils.PySelection)1 PyEdit (org.python.pydev.editor.PyEdit)1 PyRefactorAction (org.python.pydev.editor.actions.refactoring.PyRefactorAction)1 Tuple (org.python.pydev.shared_core.structure.Tuple)1