Search in sources :

Example 1 with ImmutableTuple

use of org.python.pydev.shared_core.structure.ImmutableTuple in project Pydev by fabioz.

the class AbstractASTManager method resolveImport.

/**
 * Resolves a token defined with 'from module import something' statement
 * to a proper type, as defined in module.
 * @param imported the token to resolve.
 * @return the resolved token or the original token in case no additional information could be obtained.
 * @throws CompletionRecursionException
 */
@Override
public ImmutableTuple<IModule, IToken> resolveImport(ICompletionState state, final IToken imported, IModule current) throws CompletionRecursionException {
    String currModName = imported.getParentPackage();
    Tuple3<IModule, String, IToken> modTok = findOnImportedMods(new TokensList(new IToken[] { imported }), state.getCopyWithActTok(imported.getRepresentation()), currModName, current);
    if (modTok != null && modTok.o1 != null) {
        if (modTok.o2.length() == 0) {
            // it's a module actually, so, no problems...
            return new ImmutableTuple<IModule, IToken>(current, imported);
        } else {
            try {
                state.checkResolveImportMemory(modTok.o1, modTok.o2);
            } catch (CompletionRecursionException e) {
                return new ImmutableTuple<IModule, IToken>(current, imported);
            }
            IToken repInModule = getRepInModule(modTok.o1, modTok.o2, state.getNature(), state);
            if (repInModule != null) {
                return new ImmutableTuple<IModule, IToken>(modTok.o1, repInModule);
            }
        }
    }
    return new ImmutableTuple<IModule, IToken>(current, imported);
}
Also used : IModule(org.python.pydev.core.IModule) IToken(org.python.pydev.core.IToken) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) ImmutableTuple(org.python.pydev.shared_core.structure.ImmutableTuple) TokensList(org.python.pydev.core.TokensList)

Example 2 with ImmutableTuple

use of org.python.pydev.shared_core.structure.ImmutableTuple 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)

Aggregations

IModule (org.python.pydev.core.IModule)2 IToken (org.python.pydev.core.IToken)2 TokensList (org.python.pydev.core.TokensList)2 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)2 ImmutableTuple (org.python.pydev.shared_core.structure.ImmutableTuple)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CoreException (org.eclipse.core.runtime.CoreException)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 CompletionState (org.python.pydev.ast.codecompletion.revisited.CompletionState)1 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)1 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)1 ICompletionState (org.python.pydev.core.ICompletionState)1 IPythonNature (org.python.pydev.core.IPythonNature)1 IterTokenEntry (org.python.pydev.core.IterTokenEntry)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 PythonNatureWithoutProjectException (org.python.pydev.core.PythonNatureWithoutProjectException)1 TokensOrProposalsList (org.python.pydev.core.TokensOrProposalsList)1 LineStartingScope (org.python.pydev.core.docutils.PySelection.LineStartingScope)1