Search in sources :

Example 1 with TooManyMatchesException

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

the class RefactorerFindDefinition method findDefinition.

/**
 * This function is used to find the definition for some token.
 * It may return a list of ItemPointer because the actual definition may not be
 * easy to find (so, multiple places that could be the definitions for
 * the given token may be returned... and it may be up to the user to actually
 * choose the best match).
 * @throws BadLocationException
 *
 * @see org.python.pydev.ast.refactoring.IPyRefactoring#findDefinition(org.python.pydev.ast.refactoring.RefactoringRequest)
 */
public ItemPointer[] findDefinition(RefactoringRequest request) throws BadLocationException {
    try {
        request.getMonitor().beginTask("Find definition", 100);
        List<ItemPointer> pointers = new ArrayList<ItemPointer>();
        CompletionState completionState = new CompletionState();
        completionState.setAcceptTypeshed(request.acceptTypeshed);
        ArrayList<IDefinition> selected = new ArrayList<IDefinition>();
        String[] tokenAndQual;
        try {
            tokenAndQual = PyRefactoringFindDefinition.findActualDefinition(request, completionState, selected);
        } catch (CompletionRecursionException e1) {
            Log.log(e1);
            return new ItemPointer[0];
        }
        if (tokenAndQual == null) {
            return new ItemPointer[0];
        }
        PyRefactoringFindDefinition.getAsPointers(pointers, selected.toArray(new Definition[0]));
        if (pointers.size() == 0 && ((Boolean) request.getAdditionalInfo(RefactoringRequest.FIND_DEFINITION_IN_ADDITIONAL_INFO, true))) {
            String lookForInterface = tokenAndQual[1];
            List<IInfo> tokensEqualTo;
            try {
                tokensEqualTo = AdditionalProjectInterpreterInfo.getTokensEqualTo(lookForInterface, request.nature, AbstractAdditionalTokensInfo.TOP_LEVEL | AbstractAdditionalTokensInfo.INNER);
                ICodeCompletionASTManager manager = request.nature.getAstManager();
                if (manager == null) {
                    return new ItemPointer[0];
                }
                if (tokensEqualTo.size() > 50) {
                    // too many matches for that...
                    throw new TooManyMatchesException("Too Many matches (" + tokensEqualTo.size() + ") were found for the requested token:" + lookForInterface, tokensEqualTo.size());
                }
                request.communicateWork(StringUtils.format("Found: %s possible matches.", tokensEqualTo.size()));
                IPythonNature nature = request.nature;
                for (IInfo info : tokensEqualTo) {
                    AnalysisPlugin.getDefinitionFromIInfo(pointers, manager, nature, info, completionState, true, true);
                    request.checkCancelled();
                }
            } catch (MisconfigurationException e) {
                Log.log(e);
                return new ItemPointer[0];
            }
        }
        request.communicateWork(StringUtils.format("Found: %s matches.", pointers.size()));
        return pointers.toArray(new ItemPointer[0]);
    } catch (BadLocationException e) {
        throw e;
    } catch (OperationCanceledException e) {
        // that's ok... it was cancelled
        throw e;
    } finally {
        request.getMonitor().done();
    }
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) PyRefactoringFindDefinition(org.python.pydev.ast.refactoring.PyRefactoringFindDefinition) IPythonNature(org.python.pydev.core.IPythonNature) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) IDefinition(org.python.pydev.core.IDefinition) IInfo(org.python.pydev.core.IInfo) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) TooManyMatchesException(org.python.pydev.ast.refactoring.TooManyMatchesException) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) BadLocationException(org.eclipse.jface.text.BadLocationException) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Aggregations

ArrayList (java.util.ArrayList)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 CompletionState (org.python.pydev.ast.codecompletion.revisited.CompletionState)1 Definition (org.python.pydev.ast.codecompletion.revisited.visitors.Definition)1 ItemPointer (org.python.pydev.ast.item_pointer.ItemPointer)1 PyRefactoringFindDefinition (org.python.pydev.ast.refactoring.PyRefactoringFindDefinition)1 TooManyMatchesException (org.python.pydev.ast.refactoring.TooManyMatchesException)1 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)1 IDefinition (org.python.pydev.core.IDefinition)1 IInfo (org.python.pydev.core.IInfo)1 IPythonNature (org.python.pydev.core.IPythonNature)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)1