Search in sources :

Example 1 with ImportInfo

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);
    }
}
Also used : IToken(org.python.pydev.core.IToken) IterTokenEntry(org.python.pydev.core.IterTokenEntry) ImportInfo(com.python.pydev.analysis.visitors.ImportChecker.ImportInfo)

Example 2 with ImportInfo

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;
}
Also used : Import(org.python.pydev.parser.jython.ast.Import) IToken(org.python.pydev.core.IToken) FullRepIterable(org.python.pydev.shared_core.string.FullRepIterable) org.python.pydev.parser.jython.ast.aliasType(org.python.pydev.parser.jython.ast.aliasType) ImportInfo(com.python.pydev.analysis.visitors.ImportChecker.ImportInfo) Found(com.python.pydev.analysis.visitors.Found) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 3 with ImportInfo

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()));
}
Also used : Tuple3(org.python.pydev.shared_core.structure.Tuple3) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) ImportInfo(com.python.pydev.analysis.visitors.ImportChecker.ImportInfo) Found(com.python.pydev.analysis.visitors.Found)

Aggregations

ImportInfo (com.python.pydev.analysis.visitors.ImportChecker.ImportInfo)3 Found (com.python.pydev.analysis.visitors.Found)2 IToken (org.python.pydev.core.IToken)2 IterTokenEntry (org.python.pydev.core.IterTokenEntry)1 Import (org.python.pydev.parser.jython.ast.Import)1 NameTok (org.python.pydev.parser.jython.ast.NameTok)1 org.python.pydev.parser.jython.ast.aliasType (org.python.pydev.parser.jython.ast.aliasType)1 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)1 FullRepIterable (org.python.pydev.shared_core.string.FullRepIterable)1 Tuple3 (org.python.pydev.shared_core.structure.Tuple3)1