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