Search in sources :

Example 1 with TokenCompletionRequest

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

the class AbstractASTManager method getCompletionFromFuncDefReturn.

@Override
public TokensList getCompletionFromFuncDefReturn(ICompletionState state, IModule module, IDefinition definition, boolean considerYieldTheReturnType) throws CompletionRecursionException {
    TokensList ret = new TokensList();
    if (!(module instanceof SourceModule)) {
        return ret;
    }
    if (!(definition instanceof Definition)) {
        return ret;
    }
    Definition def = (Definition) definition;
    FunctionDef functionDef = (FunctionDef) def.ast;
    ITypeInfo type = NodeUtils.getReturnTypeFromFuncDefAST(functionDef);
    if (type != null) {
        ICompletionState copy = state.getCopy();
        copy.setActivationToken(type.getActTok());
        try (NoExceptionCloseable x = copy.pushLookingFor(LookingFor.LOOKING_FOR_INSTANCED_VARIABLE)) {
            stmtType[] body = functionDef.body;
            if (body.length > 0) {
                copy.setLine(body[0].beginLine - 1);
                copy.setCol(body[0].beginColumn - 1);
            }
            IModule definitionModule = def.module;
            state.checkDefinitionMemory(definitionModule, def);
            TokensList tks = this.getCompletionsForModule(definitionModule, copy);
            if (tks.notEmpty()) {
                // TODO: This is not ideal... ideally, we'd return this info along instead of setting
                // it in the token, but this may be hard as we have to touch LOTS of places for
                // this information to get to the needed place.
                tks.setGeneratorType(type);
                ret.addAll(tks);
                // Ok, resolved rtype!
                return ret;
            } else {
                // Try to deal with some token that's not imported
                List<IPyDevCompletionParticipant> participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_COMPLETION);
                for (IPyDevCompletionParticipant participant : participants) {
                    TokensList collection = participant.getCompletionsForType(copy);
                    if (collection != null && collection.notEmpty()) {
                        ret.addAll(collection);
                        // Ok, resolved rtype!
                        return ret;
                    }
                }
            }
        }
    }
    List<Return> returns = ReturnVisitor.findReturns(functionDef);
    Stream<exprType> map = returns.stream().map(r -> r.value);
    if (considerYieldTheReturnType) {
        List<Yield> yields = YieldVisitor.findYields(functionDef);
        map = Stream.concat(map, yields.stream().map(yield -> yield.value));
    }
    for (Iterator<exprType> it = map.iterator(); it.hasNext(); ) {
        exprType value = it.next();
        if (value == null) {
            continue;
        }
        String act = NodeUtils.getFullRepresentationString(value);
        if (act == null) {
            // may happen if the return we're seeing is a return without anything (keep on going to check other returns)
            continue;
        }
        ITokenCompletionRequest request = new TokenCompletionRequest(act, def.module, state.getNature(), "", def.line - 1, def.col - 1);
        TokensList tokensList = new TokensList();
        ICompletionState copy = state.getCopy();
        copy.setActivationToken(act);
        copy.setLine(value.beginLine - 1);
        copy.setCol(value.beginColumn - 1);
        LookingFor lookingFor = null;
        if (value instanceof Call) {
            lookingFor = ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE;
        }
        try (NoExceptionCloseable x = state.pushLookingFor(lookingFor)) {
            IModule definitionModule = def.module;
            state.checkDefinitionMemory(definitionModule, def);
            try {
                PyCodeCompletion.doTokenCompletion(request, this, tokensList, act, copy);
            } catch (MisconfigurationException | IOException | CoreException | PythonNatureWithoutProjectException e) {
                throw new RuntimeException(e);
            }
            if (lookingFor != null) {
                tokensList.setLookingFor(lookingFor);
            }
            ret.addAll(tokensList);
        }
    }
    return ret;
}
Also used : IPyDevCompletionParticipant(org.python.pydev.ast.codecompletion.IPyDevCompletionParticipant) org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) TokenCompletionRequest(org.python.pydev.ast.codecompletion.TokenCompletionRequest) ITokenCompletionRequest(org.python.pydev.core.ITokenCompletionRequest) IModule(org.python.pydev.core.IModule) MisconfigurationException(org.python.pydev.core.MisconfigurationException) LookingFor(org.python.pydev.core.ICompletionState.LookingFor) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) NoExceptionCloseable(org.python.pydev.core.NoExceptionCloseable) ICompletionState(org.python.pydev.core.ICompletionState) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) ITokenCompletionRequest(org.python.pydev.core.ITokenCompletionRequest) TokensList(org.python.pydev.core.TokensList) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) Call(org.python.pydev.parser.jython.ast.Call) Return(org.python.pydev.parser.jython.ast.Return) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) PyRefactoringFindDefinition(org.python.pydev.ast.refactoring.PyRefactoringFindDefinition) AssignDefinition(org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) ITypeInfo(org.python.pydev.core.ITypeInfo) Yield(org.python.pydev.parser.jython.ast.Yield)

Aggregations

IOException (java.io.IOException)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPyDevCompletionParticipant (org.python.pydev.ast.codecompletion.IPyDevCompletionParticipant)1 TokenCompletionRequest (org.python.pydev.ast.codecompletion.TokenCompletionRequest)1 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)1 AssignDefinition (org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition)1 Definition (org.python.pydev.ast.codecompletion.revisited.visitors.Definition)1 PyRefactoringFindDefinition (org.python.pydev.ast.refactoring.PyRefactoringFindDefinition)1 ICompletionState (org.python.pydev.core.ICompletionState)1 LookingFor (org.python.pydev.core.ICompletionState.LookingFor)1 IDefinition (org.python.pydev.core.IDefinition)1 IModule (org.python.pydev.core.IModule)1 ITokenCompletionRequest (org.python.pydev.core.ITokenCompletionRequest)1 ITypeInfo (org.python.pydev.core.ITypeInfo)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 NoExceptionCloseable (org.python.pydev.core.NoExceptionCloseable)1 PythonNatureWithoutProjectException (org.python.pydev.core.PythonNatureWithoutProjectException)1 TokensList (org.python.pydev.core.TokensList)1 Call (org.python.pydev.parser.jython.ast.Call)1 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)1