Search in sources :

Example 1 with CompletionState

use of org.python.pydev.ast.codecompletion.revisited.CompletionState 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)

Example 2 with CompletionState

use of org.python.pydev.ast.codecompletion.revisited.CompletionState in project Pydev by fabioz.

the class PyCodeCompletionsForTypedDict method getStringCompletions.

public TokensOrProposalsList getStringCompletions() throws CoreException, BadLocationException, IOException, MisconfigurationException, PythonNatureWithoutProjectException, CompletionRecursionException {
    if (artificialRequest.isPresent()) {
        CompletionRequest request = artificialRequest.get();
        int line = request.doc.getLineOfOffset(request.documentOffset);
        IRegion region = request.doc.getLineInformation(line);
        ICompletionState state = new CompletionState(line, request.documentOffset - region.getOffset(), null, request.nature, request.qualifier);
        state.setCancelMonitor(request.getCancelMonitor());
        state.setIsInCalltip(false);
        TokensList tokenCompletions = new TokensList();
        IPythonNature nature = request.nature;
        if (nature == null) {
            throw new RuntimeException("Unable to get python nature.");
        }
        ICodeCompletionASTManager astManager = nature.getAstManager();
        if (astManager == null) {
            // we're probably still loading it.
            return new TokensOrProposalsList();
        }
        String trimmed = request.getActivationToken().replace('.', ' ').trim();
        // We know it has to be a token completion, so, call it directly.
        PyCodeCompletion.doTokenCompletion(request, astManager, tokenCompletions, trimmed, state);
        TokensOrProposalsList tokensList = new TokensOrProposalsList();
        tokensList.addAll(tokenCompletions);
        List<ICompletionProposalHandle> completionProposals = new ArrayList<>();
        PyCodeCompletion.changeItokenToCompletionPropostal(request, completionProposals, tokensList, false, state);
        TokensOrProposalsList ret = new TokensOrProposalsList(completionProposals);
        return ret;
    }
    return null;
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) ArrayList(java.util.ArrayList) ICompletionState(org.python.pydev.core.ICompletionState) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) TokensOrProposalsList(org.python.pydev.core.TokensOrProposalsList) IRegion(org.eclipse.jface.text.IRegion) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) ICompletionState(org.python.pydev.core.ICompletionState) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) TokensList(org.python.pydev.core.TokensList)

Example 3 with CompletionState

use of org.python.pydev.ast.codecompletion.revisited.CompletionState in project Pydev by fabioz.

the class PyCodeCompletion method getCodeCompletionProposals.

@Override
public TokensOrProposalsList getCodeCompletionProposals(CompletionRequest request) throws CoreException, BadLocationException, IOException, MisconfigurationException, PythonNatureWithoutProjectException {
    if (request.getPySelection().getCursorLineContents().trim().startsWith("#")) {
        // this may happen if the context is still not correctly computed in python
        return new PyStringCodeCompletion().getCodeCompletionProposals(request);
    }
    if (PySelection.isCompletionForLiteralNumber(request.getActivationToken())) {
        // suppress completions that would be invalid
        return new TokensOrProposalsList();
    }
    if (DebugSettings.DEBUG_CODE_COMPLETION) {
        org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "Starting getCodeCompletionProposals");
        org.python.pydev.shared_core.log.ToLogFile.addLogLevel();
        org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "Request:" + request);
    }
    ArrayList<ICompletionProposalHandle> ret = new ArrayList<ICompletionProposalHandle>();
    // let's see if we should do a code-completion in the current scope...
    // this engine does not work 'correctly' in the default scope on:
    // - class definitions - after 'class' and before '('
    // - method definitions - after 'def' and before '('
    PySelection ps = request.getPySelection();
    int lineCtx = ps.isRightAfterDeclarationInLine();
    if (lineCtx != PySelection.DECLARATION_NONE) {
        if (lineCtx == PySelection.DECLARATION_METHOD) {
            createOverrideCodeCompletions(request, ret, ps);
        }
        request.showTemplates = false;
        return new TokensOrProposalsList(ret);
    }
    try {
        IPythonNature nature = request.nature;
        checkPythonNature(nature);
        ICodeCompletionASTManager astManager = nature.getAstManager();
        if (astManager == null) {
            // we're probably still loading it.
            return new TokensOrProposalsList(ret);
        }
        // list of Object[], IToken or ICompletionProposalHandle
        TokensOrProposalsList tokensList = new TokensOrProposalsList();
        String trimmed = request.activationToken.replace('.', ' ').trim();
        ImportInfo importsTipper = getImportsTipperStr(request);
        int line = request.doc.getLineOfOffset(request.documentOffset);
        IRegion region = request.doc.getLineInformation(line);
        ICompletionState state = new CompletionState(line, request.documentOffset - region.getOffset(), null, request.nature, request.qualifier);
        state.setCancelMonitor(request.getCancelMonitor());
        state.setIsInCalltip(request.isInCalltip);
        Map<String, IterTokenEntry> alreadyChecked = new HashMap<>();
        boolean importsTip = false;
        if (request.activationToken.startsWith("super()")) {
            createSuperCodeCompletions(request, tokensList, ps);
        } else if (importsTipper.importsTipperStr.length() != 0) {
            // code completion in imports
            // if found after (, but in an import, it is not a calltip!
            request.isInCalltip = false;
            // if found after (, but in an import, it is not a calltip!
            request.isInMethodKeywordParam = false;
            importsTip = doImportCompletion(request, astManager, tokensList, importsTipper);
        } else if (trimmed.length() > 0 && request.activationToken.indexOf('.') != -1) {
            // code completion for a token
            if (false) {
                // disabled for now.
                fillTokensWithJediCompletions(request, request.getPySelection(), request.nature, astManager, tokensList);
            } else {
                TokensList tokenCompletions = new TokensList();
                doTokenCompletion(request, astManager, tokenCompletions, trimmed, state);
                tokensList.addAll(tokenCompletions);
            }
            handleKeywordParam(request, line, alreadyChecked);
        } else {
            // go to globals
            if (request.isInCalltip) {
                // # SomeCall(|<- here)
                state.setLookingFor(ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE);
            }
            doGlobalsCompletion(request, astManager, tokensList, state);
            // At this point, after doing the globals completion, we may also need to check if we need to show
            // keyword parameters to the user.
            handleKeywordParam(request, line, alreadyChecked);
        }
        List<Object> analyzedTokens = null;
        if (request.qualifier.length() >= PyCodeCompletionPreferences.getArgumentsDeepAnalysisNChars()) {
            // this can take some time on the analysis, so, let's let the user choose on how many chars does he
            // want to do the analysis...
            state.pushFindResolveImportMemoryCtx();
            try {
                IFilter nameFilter = PyCodeCompletionUtils.getNameFilter(request.useSubstringMatchInCodeCompletion, request.qualifier);
                int i = 0;
                analyzedTokens = new ArrayList<Object>();
                // Note: later on we'll clear the tokensList and re-add the tokens on alreadyChecked.
                for (Iterator<IterEntry> it = tokensList.iterator(); it.hasNext(); ) {
                    i++;
                    if (i > 10000) {
                        // tokensList.size(), request));
                        break;
                    }
                    IterEntry entry = it.next();
                    Object o = entry.object;
                    if (!(o instanceof IToken)) {
                        // Don't check things that are not tokens
                        analyzedTokens.add(o);
                    } else {
                        IToken initialToken = (IToken) o;
                        IToken token = initialToken;
                        String strRep = token.getRepresentation();
                        IterTokenEntry iterTokenEntry = alreadyChecked.get(strRep);
                        IToken prev;
                        if (iterTokenEntry == null) {
                            prev = null;
                        } else {
                            prev = iterTokenEntry.getToken();
                        }
                        if (prev != null) {
                            if (prev.getArgs().length() != 0) {
                                // we already have a version with args... just keep going
                                continue;
                            }
                        }
                        if (!nameFilter.acceptName(strRep)) {
                            // just re-add it if we're going to actually use it (depending on the qualifier)
                            continue;
                        }
                        IModule current = request.getModule();
                        while (token.isImportFrom()) {
                            if (token.getArgs().length() > 0) {
                                // if we already have the args, there's also no reason to do it (that's what we'll do here)
                                break;
                            }
                            ICompletionState s = state.getCopyForResolveImportWithActTok(token.getRepresentation());
                            s.checkFindResolveImportMemory(token);
                            ImmutableTuple<IModule, IToken> modTok = astManager.resolveImport(s, token, current);
                            IToken token2 = modTok.o2;
                            current = modTok.o1;
                            if (token2 != null && initialToken != token2) {
                                String args = token2.getArgs();
                                if (args.length() > 0) {
                                    // put it into the map (may override previous if it didn't have args)
                                    initialToken.setArgs(args);
                                    initialToken.setDocStr(token2.getDocStr());
                                    if (initialToken instanceof SourceToken && token2 instanceof SourceToken) {
                                        SourceToken initialSourceToken = (SourceToken) initialToken;
                                        SourceToken token2SourceToken = (SourceToken) token2;
                                        initialSourceToken.setAst(token2SourceToken.getAst());
                                    }
                                    break;
                                }
                                if (token2 == null || (token2.equals(token) && token2.getArgs().equals(token.getArgs()) && token2.getParentPackage().equals(token.getParentPackage()))) {
                                    break;
                                }
                                token = token2;
                            } else {
                                break;
                            }
                        }
                        // Completions of B should have B with the 'a' argument.
                        if (token instanceof SourceToken) {
                            SourceToken sourceToken = (SourceToken) token;
                            if (sourceToken.getAst() instanceof ClassDef && token.getArgs().length() == 0) {
                                ClassDef classDef = (ClassDef) sourceToken.getAst();
                                if (classDef.bases != null && classDef.bases.length > 0) {
                                    String parentPackage = sourceToken.getParentPackage();
                                    if (parentPackage != null) {
                                        IModule module;
                                        if (parentPackage.equals(current.getName())) {
                                            module = current;
                                        } else {
                                            module = astManager.getModule(parentPackage, nature, true, state);
                                        }
                                        if (module != null) {
                                            if (module instanceof SourceModule) {
                                                SourceModule sourceModule = (SourceModule) module;
                                                OUT: for (int j = 0; j < classDef.bases.length; j++) {
                                                    try {
                                                        LookingFor lookingFor = state.getLookingFor();
                                                        TokensList completions;
                                                        try {
                                                            ClassDefTokensExtractor classTokensExtractor = new ClassDefTokensExtractor(classDef, sourceModule, state);
                                                            completions = classTokensExtractor.getCompletionsForBase(astManager, classDef.bases[j]);
                                                        } finally {
                                                            // Completions at this point shouldn't change the state of what we were looking for.
                                                            state.setLookingFor(lookingFor, true);
                                                        }
                                                        if (completions != null && completions.size() > 0) {
                                                            for (IterTokenEntry entry1 : completions) {
                                                                IToken comp = entry1.getToken();
                                                                if ("__init__".equals(comp.getRepresentation())) {
                                                                    if (comp.getArgs().length() > 0) {
                                                                        initialToken.setArgs(comp.getArgs());
                                                                    }
                                                                    break OUT;
                                                                }
                                                            }
                                                        }
                                                    } catch (CompletionRecursionException e) {
                                                    // Ignore (just don't get the args).
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        alreadyChecked.put(strRep, new IterTokenEntry(initialToken, entry.lookingFor));
                    }
                }
            } finally {
                state.popFindResolveImportMemoryCtx();
            }
        }
        if (analyzedTokens == null) {
            tokensList.addAll(new TokensListMixedLookingFor(alreadyChecked.values()));
        } else {
            tokensList = new TokensOrProposalsList(analyzedTokens.toArray(new Object[0]));
            tokensList.addAll(new TokensListMixedLookingFor(alreadyChecked.values()));
        }
        changeItokenToCompletionPropostal(request, ret, tokensList, importsTip, state);
    } catch (CompletionRecursionException e) {
        if (onCompletionRecursionException != null) {
            onCompletionRecursionException.call(e);
        }
        if (DebugSettings.DEBUG_CODE_COMPLETION) {
            org.python.pydev.shared_core.log.ToLogFile.toLogFile(e);
        }
    // PydevPlugin.log(e);
    // ret.add(new CompletionProposal("",request.documentOffset,0,0,null,e.getMessage(), null,null));
    }
    if (DebugSettings.DEBUG_CODE_COMPLETION) {
        org.python.pydev.shared_core.log.ToLogFile.remLogLevel();
        org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "Finished completion. Returned:" + ret.size() + " completions.\r\n");
    }
    return new TokensOrProposalsList(ret);
}
Also used : IModule(org.python.pydev.core.IModule) HashMap(java.util.HashMap) TokensListMixedLookingFor(org.python.pydev.core.TokensListMixedLookingFor) LookingFor(org.python.pydev.core.ICompletionState.LookingFor) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) TokensOrProposalsList(org.python.pydev.core.TokensOrProposalsList) ICompletionState(org.python.pydev.core.ICompletionState) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) IRegion(org.eclipse.jface.text.IRegion) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) IToken(org.python.pydev.core.IToken) ICompletionState(org.python.pydev.core.ICompletionState) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) TokensList(org.python.pydev.core.TokensList) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) IterTokenEntry(org.python.pydev.core.IterTokenEntry) ImportInfo(org.python.pydev.core.ICodeCompletionASTManager.ImportInfo) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IterEntry(org.python.pydev.core.IterEntry) IFilter(org.python.pydev.ast.codecompletion.PyCodeCompletionUtils.IFilter) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) PySelection(org.python.pydev.core.docutils.PySelection) TokensListMixedLookingFor(org.python.pydev.core.TokensListMixedLookingFor) ClassDefTokensExtractor(org.python.pydev.ast.codecompletion.revisited.modules.ClassDefTokensExtractor) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)

Example 4 with CompletionState

use of org.python.pydev.ast.codecompletion.revisited.CompletionState in project Pydev by fabioz.

the class PyCodeCompletion method createOverrideCodeCompletions.

private void createOverrideCodeCompletions(CompletionRequest request, ArrayList<ICompletionProposalHandle> ret, PySelection ps) throws BadLocationException {
    IImageCache imageCache = SharedCorePlugin.getImageCache();
    IImageHandle imageOverride = imageCache != null ? imageCache.get(UIConstants.METHOD_ICON) : null;
    String lineContentsToCursor = ps.getLineContentsToCursor();
    LineStartingScope scopeStart = ps.getPreviousLineThatStartsScope(PySelection.CLASS_TOKEN, false, PySelection.getFirstCharPosition(lineContentsToCursor));
    String className = null;
    if (scopeStart != null) {
        className = PySelection.getClassNameInLine(scopeStart.lineStartingScope);
        if (className != null && className.length() > 0) {
            Tuple<List<String>, Integer> insideParensBaseClasses = ps.getInsideParentesisToks(true, scopeStart.iLineStartingScope);
            if (insideParensBaseClasses != null) {
                // representation -> token and base class
                OrderedMap<String, ImmutableTuple<IToken, String>> map = new OrderedMap<String, ImmutableTuple<IToken, String>>();
                for (String baseClass : insideParensBaseClasses.o1) {
                    try {
                        ICompletionState state = new CompletionState(-1, -1, null, request.nature, baseClass);
                        state.setActivationToken(baseClass);
                        state.setIsInCalltip(false);
                        IPythonNature pythonNature = request.nature;
                        checkPythonNature(pythonNature);
                        ICodeCompletionASTManager astManager = pythonNature.getAstManager();
                        if (astManager == null) {
                            // we're probably still loading it.
                            return;
                        }
                        // Ok, looking for a token in globals.
                        IModule module = request.getModule();
                        if (module == null) {
                            continue;
                        }
                        TokensList comps = astManager.getCompletionsForModule(module, state, true, true);
                        for (IterTokenEntry entry : comps) {
                            IToken iToken = entry.getToken();
                            String representation = iToken.getRepresentation();
                            ImmutableTuple<IToken, String> curr = map.get(representation);
                            if (curr != null && curr.o1 instanceof SourceToken) {
                                // source tokens are never reset!
                                continue;
                            }
                            int type = iToken.getType();
                            if (iToken instanceof SourceToken && ((SourceToken) iToken).getAst() instanceof FunctionDef) {
                                map.put(representation, new ImmutableTuple<IToken, String>(iToken, baseClass));
                            } else if (type == IToken.TYPE_FUNCTION || type == IToken.TYPE_UNKNOWN || type == IToken.TYPE_BUILTIN) {
                                map.put(representation, new ImmutableTuple<IToken, String>(iToken, baseClass));
                            }
                        }
                    } catch (Exception e) {
                        Log.log(e);
                    }
                }
                for (ImmutableTuple<IToken, String> tokenAndBaseClass : map.values()) {
                    FunctionDef functionDef = null;
                    // No checkings needed for type (we already did that above).
                    if (tokenAndBaseClass.o1 instanceof SourceToken) {
                        SourceToken sourceToken = (SourceToken) tokenAndBaseClass.o1;
                        SimpleNode ast = sourceToken.getAst();
                        if (ast instanceof FunctionDef) {
                            functionDef = (FunctionDef) ast;
                        } else {
                            functionDef = sourceToken.getAliased().createCopy();
                            NameTok t = (NameTok) functionDef.name;
                            t.id = sourceToken.getRepresentation();
                        }
                    } else {
                        // unfortunately, for builtins we usually cannot trust the parameters.
                        String representation = tokenAndBaseClass.o1.getRepresentation();
                        PyAstFactory factory = new PyAstFactory(new AdapterPrefs(ps.getEndLineDelim(), request.nature));
                        functionDef = factory.createFunctionDef(representation);
                        functionDef.args = factory.createArguments(true);
                        functionDef.args.vararg = new NameTok("args", NameTok.VarArg);
                        functionDef.args.kwarg = new NameTok("kwargs", NameTok.KwArg);
                        if (!representation.equals("__init__")) {
                            // signal that the return should be added
                            functionDef.body = new stmtType[] { new Return(null) };
                        }
                    }
                    if (functionDef != null) {
                        ret.add(CompletionProposalFactory.get().createOverrideMethodCompletionProposal(request, ps, ps.getAbsoluteCursorOffset(), 0, 0, imageOverride, functionDef, tokenAndBaseClass.o2, className));
                    }
                }
            }
        }
    }
}
Also used : IModule(org.python.pydev.core.IModule) IPythonNature(org.python.pydev.core.IPythonNature) ICompletionState(org.python.pydev.core.ICompletionState) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) IToken(org.python.pydev.core.IToken) ICompletionState(org.python.pydev.core.ICompletionState) TokensList(org.python.pydev.core.TokensList) List(java.util.List) TokensOrProposalsList(org.python.pydev.core.TokensOrProposalsList) ArrayList(java.util.ArrayList) PyAstFactory(org.python.pydev.parser.jython.ast.factory.PyAstFactory) TokensList(org.python.pydev.core.TokensList) Return(org.python.pydev.parser.jython.ast.Return) IImageCache(org.python.pydev.shared_core.image.IImageCache) IterTokenEntry(org.python.pydev.core.IterTokenEntry) ImmutableTuple(org.python.pydev.shared_core.structure.ImmutableTuple) LineStartingScope(org.python.pydev.core.docutils.PySelection.LineStartingScope) CoreException(org.eclipse.core.runtime.CoreException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IImageHandle(org.python.pydev.shared_core.image.IImageHandle) AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) OrderedMap(org.python.pydev.shared_core.structure.OrderedMap) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 5 with CompletionState

use of org.python.pydev.ast.codecompletion.revisited.CompletionState in project Pydev by fabioz.

the class PyRenameGlobalProcess method findReferencesOnOtherModule.

@Override
protected List<ASTEntry> findReferencesOnOtherModule(RefactoringStatus status, RefactoringRequest request, String initialName, SourceModule module) {
    ICompletionState completionCache = new CompletionState();
    completionCache.setAcceptTypeshed(request.acceptTypeshed);
    Set<ASTEntry> ret = new OrderedSet<ASTEntry>();
    List<ASTEntry> localOccurrences = ScopeAnalysis.getLocalOccurrences(initialName, module.getAst(), false);
    localOccurrences.addAll(ScopeAnalysis.getFullAttributeReferencesFromPartialName(initialName, module.getAst()));
    if (localOccurrences.size() > 0) {
        try {
            List<IDefinition> actualDefinitions = request.findActualDefinitions(completionCache);
            for (ASTEntry occurrence : localOccurrences) {
                String fullRepresentationString = NodeUtils.getFullRepresentationString(occurrence.node);
                List<IDefinition> foundDefs = PyRefactoringFindDefinition.findActualDefinition(request.getMonitor(), request.acceptTypeshed, module, fullRepresentationString, null, occurrence.node.beginLine, occurrence.node.beginColumn, module.getNature(), completionCache, false);
                for (IDefinition def : foundDefs) {
                    IModule defModule = def.getModule();
                    if (defModule == null || (def instanceof Definition && ((Definition) def).ast == null)) {
                        // If we're unsure, just add it.
                        ret.add(occurrence);
                        continue;
                    }
                    for (IDefinition actualDef : actualDefinitions) {
                        IModule actualDefModule = actualDef.getModule();
                        if (defModule.getName().equals(actualDefModule.getName())) {
                            ret.add(occurrence);
                        }
                    }
                }
            }
        } catch (Exception e) {
            Log.log(e);
        }
    }
    return new ArrayList<ASTEntry>(ret);
}
Also used : OrderedSet(org.python.pydev.shared_core.structure.OrderedSet) IModule(org.python.pydev.core.IModule) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) PyRefactoringFindDefinition(org.python.pydev.ast.refactoring.PyRefactoringFindDefinition) ArrayList(java.util.ArrayList) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) ICompletionState(org.python.pydev.core.ICompletionState) IDefinition(org.python.pydev.core.IDefinition) ICompletionState(org.python.pydev.core.ICompletionState) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry)

Aggregations

CompletionState (org.python.pydev.ast.codecompletion.revisited.CompletionState)15 ArrayList (java.util.ArrayList)13 ICompletionState (org.python.pydev.core.ICompletionState)13 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)9 TokensList (org.python.pydev.core.TokensList)9 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)9 IModule (org.python.pydev.core.IModule)7 IPythonNature (org.python.pydev.core.IPythonNature)7 IDefinition (org.python.pydev.core.IDefinition)6 IterTokenEntry (org.python.pydev.core.IterTokenEntry)6 SimpleNode (org.python.pydev.parser.jython.SimpleNode)6 Definition (org.python.pydev.ast.codecompletion.revisited.visitors.Definition)5 IToken (org.python.pydev.core.IToken)5 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)5 List (java.util.List)4 BadLocationException (org.eclipse.jface.text.BadLocationException)4 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)4 PyRefactoringFindDefinition (org.python.pydev.ast.refactoring.PyRefactoringFindDefinition)4 MisconfigurationException (org.python.pydev.core.MisconfigurationException)4 PySelection (org.python.pydev.core.docutils.PySelection)4