//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 ret;
}
//Ok, looking for a token in globals.
IModule module = request.getModule();
if (module == null) {
continue;
}
IToken[] comps = astManager.getCompletionsForModule(module, state, true, true);
for (int i = 0; i < comps.length; i++) {
IToken iToken = comps[i];
String representation = iToken.getRepresentation();
ImmutableTuple<IToken, String> curr = map.get(representation);
if (curr != null && curr.o1 instanceof SourceToken) {
continue; //source tokens are never reset!
}
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__")) {
functionDef.body = new stmtType[] { new Return(null) }; //signal that the return should be added
}
}
if (functionDef != null) {
ret.add(new OverrideMethodCompletionProposal(ps.getAbsoluteCursorOffset(), 0, 0,
imageOverride, functionDef, tokenAndBaseClass.o2, //baseClass
className));
}
}
}
}
}
}
request.showTemplates = false;
return ret;
}
try {
IPythonNature pythonNature = request.nature;
checkPythonNature(pythonNature);
ICodeCompletionASTManager astManager = pythonNature.getAstManager();
if (astManager == null) {
//we're probably still loading it.
return ret;
}
//list of Object[], IToken or ICompletionProposal
List<Object> tokensList = new ArrayList<Object>();
lazyStartShell(request);
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.setIsInCalltip(request.isInCalltip);
Map<String, IToken> alreadyChecked = new HashMap<String, IToken>();
boolean importsTip = false;
if (importsTipper.importsTipperStr.length() != 0) {
//code completion in imports
request.isInCalltip = false; //if found after (, but in an import, it is not a calltip!
request.isInMethodKeywordParam = false; //if found after (, but in an import, it is not a calltip!
importsTip = doImportCompletion(request, astManager, tokensList, importsTipper);
} else if (trimmed.length() > 0 && request.activationToken.indexOf('.') != -1) {
//code completion for a token
doTokenCompletion(request, astManager, tokensList, trimmed, state);
handleKeywordParam(request, line, alreadyChecked);
} else {
//go to globals
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);
}
String lowerCaseQual = request.qualifier.toLowerCase();
if (lowerCaseQual.length() >= PyCodeCompletionPreferencesPage.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 {
for (Iterator<Object> it = tokensList.listIterator(); it.hasNext();) {
Object o = it.next();
if (o instanceof IToken) {
it.remove(); // always remove the tokens from the list (they'll be re-added later once they are filtered)
IToken initialToken = (IToken) o;
IToken token = initialToken;
String strRep = token.getRepresentation();
IToken prev = alreadyChecked.get(strRep);
if (prev != null) {
if (prev.getArgs().length() != 0) {
continue; // we already have a version with args... just keep going
}
}
if (!strRep.toLowerCase().startsWith(lowerCaseQual)) {
//just re-add it if we're going to actually use it (depending on the qualifier)
continue;
}
IModule current = request.getModule();
while (token.isImportFrom()) {
//we'll only add it here if it is an import from (so, set the flag to false for the outer add)
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;
}
}
alreadyChecked.put(strRep, initialToken);
}
}
} finally {
state.popFindResolveImportMemoryCtx();
}
}
tokensList.addAll(alreadyChecked.values());
changeItokenToCompletionPropostal(viewer, request, ret, tokensList, importsTip, state);