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;
}
Aggregations