use of org.python.pydev.core.IToken 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.core.IToken in project Pydev by fabioz.
the class AbstractASTManager method getTokToSearchInOtherModule.
/**
* When we have an import, we have one token which we used to find it and another which is the
* one we refer to at the current module. This method will get the way it's referred at the
* actual module and not at the current module (at the current module it's modTok.o2).
*/
public static String getTokToSearchInOtherModule(Tuple3<IModule, String, IToken> modTok) {
String tok = modTok.o2;
String tokForSearchInOtherModule = tok;
if (tok.length() > 0) {
IToken sourceToken = modTok.o3;
if (sourceToken instanceof SourceToken) {
SourceToken sourceToken2 = (SourceToken) sourceToken;
if (sourceToken2.getAst() instanceof ImportFrom) {
ImportFrom importFrom = (ImportFrom) sourceToken2.getAst();
if (importFrom.names.length > 0 && importFrom.names[0].asname != null) {
String originalRep = sourceToken.getOriginalRep();
tokForSearchInOtherModule = FullRepIterable.getLastPart(originalRep);
}
}
}
}
return tokForSearchInOtherModule;
}
use of org.python.pydev.core.IToken in project Pydev by fabioz.
the class AbstractASTManager method getCompletionsUnpackingAST.
private TokensList getCompletionsUnpackingAST(SimpleNode ast, final IModule module, ICompletionState state, UnpackInfo unpackPos) throws CompletionRecursionException {
if (ast instanceof FunctionDef) {
// let's try to find as an annotation first
ITypeInfo type = NodeUtils.getReturnTypeFromFuncDefAST(ast);
if (type != null) {
TokensList completionsUnpackingType = getCompletionsUnpackingType(module, state, unpackPos, type);
if (completionsUnpackingType != null && completionsUnpackingType.size() > 0) {
return completionsUnpackingType;
}
}
TokensList tokens = getCompletionsUnpackingDocstring(module, state, unpackPos, NodeUtils.getNodeDocString(ast));
if (tokens != null && tokens.size() > 0) {
return tokens;
}
List<Yield> findYields = YieldVisitor.findYields((FunctionDef) ast);
for (Yield yield : findYields) {
// what we should complete on.
if (yield.value != null) {
String rep = NodeUtils.getFullRepresentationString(yield.value);
if (rep != null) {
ICompletionState copyWithActTok = state.getCopyWithActTok(rep);
copyWithActTok.setLookingFor(ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE);
TokensList completionsForModule = getCompletionsForModule(module, copyWithActTok);
if (completionsForModule.size() > 0) {
return completionsForModule;
}
}
}
}
List<Return> findReturns = ReturnVisitor.findReturns((FunctionDef) ast);
for (Return return1 : findReturns) {
// Return types have to be unpacked...
if (return1.value != null) {
exprType[] elts = NodeUtils.getEltsFromCompoundObject(return1.value);
if (elts != null) {
TokensList ret = getCompletionsFromUnpackedCompoundObject(module, state, elts, unpackPos);
if (ret != null && ret.size() > 0) {
return ret;
}
} else {
String rep = NodeUtils.getFullRepresentationString(return1.value);
if (rep != null) {
TokensList completionsUnpackingObject = getCompletionsUnpackingObject(module, state.getCopyWithActTok(rep), null, unpackPos);
if (completionsUnpackingObject != null && completionsUnpackingObject.size() > 0) {
return completionsUnpackingObject;
}
}
}
}
}
} else if (ast instanceof ClassDef) {
String rep = NodeUtils.getFullRepresentationString(ast);
if (rep != null) {
TokensList completionsForModule = this.getCompletionsForModule(module, state.getCopyWithActTok(rep));
IToken getItemToken = null;
for (IterTokenEntry entry : completionsForModule) {
IToken iToken = entry.getToken();
switch(iToken.getRepresentation()) {
case "__getitem__":
getItemToken = iToken;
break;
case "__iter__":
case "__next__":
case "__enter__":
// If we find it we'll try to unpack completions from it.
if (iToken instanceof SourceToken) {
SourceToken sourceToken = (SourceToken) iToken;
IModule useModule = null;
if (module.getName().equals(sourceToken.getParentPackage())) {
useModule = module;
}
if (useModule == null) {
String parentPackage = sourceToken.getParentPackage();
useModule = getModule(parentPackage, state.getNature(), true, state);
}
TokensList ret = getCompletionsUnpackingAST(sourceToken.getAst(), useModule, state, unpackPos);
if (ret != null && ret.size() > 0) {
return ret;
}
}
break;
}
}
if (getItemToken instanceof SourceToken) {
// The __getitem__ is already unpacked (i.e.: __iter__ returns a generator
// and __getitem__ already returns the value we're iterating through).
SourceToken sourceToken = (SourceToken) getItemToken;
IModule useModule = null;
if (module.getName().equals(sourceToken.getParentPackage())) {
useModule = module;
} else {
String parentPackage = getItemToken.getParentPackage();
useModule = getModule(parentPackage, state.getNature(), true, state);
}
TokensList ret = getCompletionsNotUnpackingToken(sourceToken, useModule, state);
if (ret != null && ret.size() > 0) {
return ret;
}
}
}
}
return null;
}
use of org.python.pydev.core.IToken in project Pydev by fabioz.
the class AbstractASTManager method getAbsoluteImportTokens.
/**
* @param moduleToGetTokensFrom the string that represents the token from where we are getting the imports
* @param set the set where the tokens should be added
* @param importInfo if null, only the 1st element of the module will be added, otherwise, it'll check the info
* to see if it should add only the 1st element of the module or the complete module (e.g.: add only xml or
* xml.dom and other submodules too)
*/
public void getAbsoluteImportTokens(String moduleToGetTokensFrom, Set<IToken> inputOutput, int type, boolean onlyFilesOnSameLevel, ImportInfo importInfo, boolean onlyGetDirectModules) {
// boolean getSubModules = false;
// if(importInfo != null){
// //we only want to get submodules if we're in:
// //from xxx
// //import xxx
// //
// //We do NOT want to get it on:
// //from xxx import yyy
// if(importInfo.hasFromSubstring != importInfo.hasImportSubstring){
// getSubModules = true;
// }
// }
HashMap<String, IToken> temp = new HashMap<String, IToken>();
SortedMap<ModulesKey, ModulesKey> modulesStartingWith;
if (onlyGetDirectModules) {
modulesStartingWith = modulesManager.getAllDirectModulesStartingWith(moduleToGetTokensFrom);
} else {
modulesStartingWith = modulesManager.getAllModulesStartingWith(moduleToGetTokensFrom);
}
Iterator<ModulesKey> itModules = modulesStartingWith.keySet().iterator();
while (itModules.hasNext()) {
ModulesKey key = itModules.next();
String element = key.name;
// if (element.startsWith(moduleToGetTokensFrom)) { we don't check that anymore because we get all the modules starting with it already
if (onlyFilesOnSameLevel && key.file != null && key.file.isDirectory()) {
// we only want those that are in the same directory, and not in other directories...
continue;
}
element = element.substring(moduleToGetTokensFrom.length());
// we only check this if we only want file modules (in
if (onlyFilesOnSameLevel && StringUtils.countChars('.', element) > 1) {
continue;
}
boolean goForIt = false;
// if we have xml token (not using the qualifier here)
if (moduleToGetTokensFrom.length() != 0) {
if (element.length() > 0 && element.charAt(0) == ('.')) {
element = element.substring(1);
goForIt = true;
}
} else {
goForIt = true;
}
if (element.length() > 0 && goForIt) {
List<String> splitted = StringUtils.dotSplit(element);
if (splitted.size() > 0) {
String strToAdd;
strToAdd = splitted.get(0);
// if(!getSubModules){
// }else{
// if(element.endsWith(".__init__")){
// strToAdd = element.substring(0, element.length()-9);
// }else{
// strToAdd = element;
// }
// }
// this is the completion
temp.put(strToAdd, new ConcreteToken(strToAdd, "", "", moduleToGetTokensFrom, type, modulesManager.getNature()));
}
}
// }
}
inputOutput.addAll(temp.values());
}
use of org.python.pydev.core.IToken in project Pydev by fabioz.
the class AbstractASTManager method findModule.
@Override
public Tuple<IModule, String> findModule(String moduleToFind, String currentModule, ICompletionState state, IModule current) throws CompletionRecursionException, MisconfigurationException {
NameTok name = new NameTok(moduleToFind, NameTok.ImportModule);
Import impTok = new Import(new aliasType[] { new aliasType(name, null) });
List<IToken> tokens = new ArrayList<IToken>();
List<IToken> imp = AbstractVisitor.makeImportToken(impTok, tokens, currentModule, true, current != null ? current.getNature() : state.getNature());
// get the last one (it's the one with the 'longest' representation).
IToken importedModule = imp.get(imp.size() - 1);
return this.findOnImportedMods(importedModule, "", state, "", currentModule, current);
}
Aggregations