use of com.python.pydev.analysis.visitors.ImportChecker.ImportInfo in project Pydev by fabioz.
the class Scope method addImportTokens.
/**
* Adds many tokens at once. (created by the same token)
* Adding more than one ONLY happens for:
* - wild imports (kind of obvious)
* - imports such as import os.path (one token is created for os and one for os.path)
*/
public void addImportTokens(TokensList list, IToken generator, ICompletionCache completionCache) {
ScopeItems.TryExceptInfo withinExceptNode = scope.peek().getTryExceptImportError();
// only report undefined imports if we're not inside a try..except ImportError.
boolean reportUndefinedImports = withinExceptNode == null;
boolean requireTokensToBeImports = false;
ImportInfo importInfo = null;
if (generator != null) {
// import)
if (!generator.isImport()) {
throw new RuntimeException("Only imports should generate multiple tokens " + "(it may be null for imports in the form import foo.bar, but then all its tokens must be imports).");
}
importInfo = importChecker.visitImportToken(generator, reportUndefinedImports, completionCache);
} else {
requireTokensToBeImports = true;
}
ScopeItems m = scope.peek();
for (IterTokenEntry entry : list) {
IToken o = entry.getToken();
// System.out.println("adding: "+o.getRepresentation());
Found found = addToken(generator, m, o, o.getRepresentation());
if (withinExceptNode != null) {
// may mark previous as used...
withinExceptNode.addFoundImportToTryExcept(found);
}
// or a Name, ClassDef, MethodDef, etc. (in the case of wild imports)
if (requireTokensToBeImports) {
if (!o.isImport()) {
throw new RuntimeException("Expecting import token");
}
importInfo = importChecker.visitImportToken(o, reportUndefinedImports, completionCache);
}
// can be either the one resolved in the wild import or in this token (if it is not a wild import)
found.importInfo = importInfo;
visitor.onImportInfoSetOnFound(found);
}
}
use of com.python.pydev.analysis.visitors.ImportChecker.ImportInfo in project Pydev by fabioz.
the class ScopeAnalyzerVisitor method visitImportFrom.
@Override
public Object visitImportFrom(ImportFrom node) throws Exception {
Object ret = super.visitImportFrom(node);
// the import from will generate the tokens that go into the module namespace, but still, it needs to
// create tokens that will not be used in code-analysis, but will be used in matching tokens
// regarding its module.
NameTok tokModName = (NameTok) node.module;
for (String m : new FullRepIterable(tokModName.id)) {
if (m.indexOf(".") == -1) {
aliasType[] names = new aliasType[1];
NameTok importNameTok = new NameTok(m, NameTok.ImportModule);
importNameTok.beginLine = tokModName.beginLine;
importNameTok.beginColumn = tokModName.beginColumn;
names[0] = new aliasType(importNameTok, null);
names[0].beginLine = tokModName.beginLine;
names[0].beginColumn = tokModName.beginColumn;
Import importTok = new Import(names);
importTok.beginLine = tokModName.beginLine;
importTok.beginColumn = tokModName.beginColumn;
List<IToken> createdTokens = AbstractVisitor.makeImportToken(importTok, null, this.current.getName(), true, this.current.getNature());
for (IToken token : createdTokens) {
ImportInfo info = this.scope.importChecker.visitImportToken(token, false, this.completionCache);
Found found = new Found(token, token, scope.getCurrScopeId(), scope.getCurrScopeItems());
found.importInfo = info;
// check if this is actually a match
checkFound(found, peekParent());
addFoundToImportsMap(found, importsFoundFromModuleName);
}
}
}
return ret;
}
use of com.python.pydev.analysis.visitors.ImportChecker.ImportInfo in project Pydev by fabioz.
the class ScopeAnalyzerVisitor method addFoundToImportsMap.
/**
* Used to add some Found that is related to an import to a 'global import register'.
* This is needed because, unlike other regular tokens, we want to find imports that are
* in diferent contexts as being in the same context.
*
* @param found this is the Found that we want to add to the imports
* @param map this is the map that contains the imports Found occurrences (it has to be passed,
* as there is a map for the imports that are actually in the namespace and another for those
* that are 'artificially' generated).
*/
private void addFoundToImportsMap(Found found, Map<String, List<Tuple3<Found, Integer, ASTEntry>>> map) {
ImportInfo info = found.importInfo;
String modName = UNRESOLVED_MOD_NAME;
if (info != null && info.mod != null) {
modName = info.mod.getName();
}
List<Tuple3<Found, Integer, ASTEntry>> prev = map.get(modName);
if (prev == null) {
prev = new ArrayList<Tuple3<Found, Integer, ASTEntry>>();
map.put(modName, prev);
}
// col delta is undefined
prev.add(new Tuple3<Found, Integer, ASTEntry>(found, -1, peekParent()));
}
Aggregations