Search in sources :

Example 1 with ScopeItems

use of com.python.pydev.analysis.visitors.ScopeItems in project Pydev by fabioz.

the class AbstractScopeAnalyzerVisitor method endScope.

/**
 * finalizes the current scope
 * @param reportUnused: defines whether we should report unused things found (we may not want to do that
 * when we have an abstract method)
 */
protected void endScope(SimpleNode node) {
    onBeforeEndScope(node);
    // clear the last scope
    ScopeItems m = scope.endScope();
    for (Iterator<Found> it = probablyNotDefined.iterator(); it.hasNext(); ) {
        Found n = it.next();
        final GenAndTok probablyNotDefinedFirst = n.getSingle();
        IToken tok = probablyNotDefinedFirst.tok;
        String rep = tok.getRepresentation();
        // we also get a last pass to the unused to see if they might have been defined later on the higher scope
        List<Found> foundItems = find(m, rep);
        boolean setUsed = false;
        for (Found found : foundItems) {
            // the scope where it is defined must be an outer scope so that we can say it was defined later...
            final GenAndTok foundItemFirst = found.getSingle();
            if ((probablyNotDefinedFirst.scopeFound.getScopeType() & Scope.ACCEPTED_METHOD_AND_LAMBDA) != 0 && m.getScopeType() != Scope.SCOPE_TYPE_CLASS) {
                if (foundItemFirst.scopeId < probablyNotDefinedFirst.scopeId) {
                    found.setUsed(true);
                    setUsed = true;
                }
            }
        }
        if (setUsed) {
            it.remove();
        }
    }
    // point
    if (scope.size() == 0) {
        onLastScope(m);
    }
    onAfterEndScope(node, m);
}
Also used : IToken(org.python.pydev.core.IToken) ScopeItems(com.python.pydev.analysis.visitors.ScopeItems) Found(com.python.pydev.analysis.visitors.Found) GenAndTok(com.python.pydev.analysis.visitors.GenAndTok)

Example 2 with ScopeItems

use of com.python.pydev.analysis.visitors.ScopeItems in project Pydev by fabioz.

the class AbstractScopeAnalyzerVisitor method markRead.

/**
 * marks a token as read given its representation
 *
 * @param token the token to be added
 * @param rep the token representation
 * @param addToNotDefined determines if it should be added to the 'not defined tokens' stack or not
 * @return true if it was found
 */
protected boolean markRead(IToken token, String rep, boolean addToNotDefined, boolean checkIfIsValidImportToken) {
    boolean found = false;
    Found foundAs = null;
    String foundAsStr = null;
    int acceptedScopes = 0;
    ScopeItems currScopeItems = scope.getCurrScopeItems();
    if ((currScopeItems.getScopeType() & Scope.ACCEPTED_METHOD_AND_LAMBDA) != 0) {
        acceptedScopes = Scope.ACCEPTED_METHOD_SCOPES;
    } else {
        acceptedScopes = Scope.ACCEPTED_ALL_SCOPES;
    }
    if ("locals".equals(rep)) {
        // if locals() is accessed, all the tokens currently found are marked as 'used'
        // use case:
        // 
        // def f2():
        // a = 1
        // b = 2
        // c = 3
        // f1(**locals())
        currScopeItems.setAllUsed();
        return true;
    }
    Iterator<String> it = new FullRepIterable(rep, true).iterator();
    // search for it
    while (found == false && it.hasNext()) {
        String nextTokToSearch = it.next();
        foundAs = scope.findFirst(nextTokToSearch, true, acceptedScopes);
        found = foundAs != null;
        if (found) {
            foundAsStr = nextTokToSearch;
            foundAs.getSingle().references.add(token);
            onFoundTokenAs(token, foundAs);
        }
    }
    if (!found) {
        // this token might not be defined... (still, might be in names to ignore)
        int i;
        if ((i = rep.indexOf('.')) != -1) {
            // if it is an attribute, we have to check the names to ignore just with its first part
            rep = rep.substring(0, i);
        }
        if (addToNotDefined) {
            org.python.pydev.shared_core.structure.Tuple<IToken, Found> foundInNamesToIgnore = findInNamesToIgnore(rep, token);
            if (foundInNamesToIgnore == null) {
                Found foundForProbablyNotDefined = makeFound(token);
                if (scope.size() > 1) {
                    // if we're not in the global scope, it might be defined later
                    // we are not in the global scope, so it might be defined later...
                    probablyNotDefined.add(foundForProbablyNotDefined);
                    onAddToProbablyNotDefined(token, foundForProbablyNotDefined);
                } else {
                    // it is in the global scope, so, it is undefined.
                    onAddUndefinedMessage(token, foundForProbablyNotDefined);
                }
            } else {
                IToken tokenInNamesToIgnore = foundInNamesToIgnore.o1;
                onFoundInNamesToIgnore(token, tokenInNamesToIgnore);
            }
        }
    } else if (checkIfIsValidImportToken) {
        // really exists in xxx, if it was found as an import)
        try {
            if (foundAs.isImport() && !rep.equals(foundAsStr) && foundAs.importInfo != null && foundAs.importInfo.wasResolved) {
                // the foundAsStr equals the module resolved in the Found tok
                IModule m = foundAs.importInfo.mod;
                String tokToCheck;
                if (foundAs.isWildImport()) {
                    tokToCheck = foundAsStr;
                } else {
                    String tok = foundAs.importInfo.rep;
                    tokToCheck = rep.substring(foundAsStr.length() + 1);
                    if (tok.length() > 0) {
                        tokToCheck = tok + "." + tokToCheck;
                    }
                }
                for (String repToCheck : new FullRepIterable(tokToCheck)) {
                    int inGlobalTokens = m.isInGlobalTokens(repToCheck, nature, true, true, this.completionCache);
                    if (inGlobalTokens == IModule.NOT_FOUND) {
                        if (!isDefinitionUnknown(m, repToCheck)) {
                            // Check if there's some hasattr (if there is, we'll consider that the token which
                            // had the hasattr checked will actually have it).
                            TokensList interfaceForLocal = this.currentLocalScope.getInterfaceForLocal(foundAsStr, false, true);
                            boolean foundInHasAttr = false;
                            for (IterTokenEntry entry : interfaceForLocal) {
                                IToken iToken = entry.getToken();
                                if (iToken.getRepresentation().equals(repToCheck)) {
                                    foundInHasAttr = true;
                                    break;
                                }
                            }
                            if (!foundInHasAttr) {
                                IToken foundTok = findNameTok(token, repToCheck);
                                onAddUndefinedVarInImportMessage(foundTok, foundAs);
                            }
                        }
                        // no need to keep checking once one is not defined
                        break;
                    } else if (inGlobalTokens == IModule.FOUND_BECAUSE_OF_GETATTR) {
                        break;
                    }
                }
            } else if (foundAs.isImport() && (foundAs.importInfo == null || !foundAs.importInfo.wasResolved)) {
                // import was not resolved
                onFoundUnresolvedImportPart(token, rep, foundAs);
            }
        } catch (Exception e) {
            Log.log("Error checking for valid tokens (imports) for " + moduleName, e);
        }
    }
    return found;
}
Also used : IModule(org.python.pydev.core.IModule) IterTokenEntry(org.python.pydev.core.IterTokenEntry) Found(com.python.pydev.analysis.visitors.Found) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IToken(org.python.pydev.core.IToken) FullRepIterable(org.python.pydev.shared_core.string.FullRepIterable) ScopeItems(com.python.pydev.analysis.visitors.ScopeItems) TokensList(org.python.pydev.core.TokensList)

Example 3 with ScopeItems

use of com.python.pydev.analysis.visitors.ScopeItems in project Pydev by fabioz.

the class AbstractScopeAnalyzerVisitor method addToNamesToIgnore.

/**
 * used so that the token is added to the names to ignore...
 */
protected void addToNamesToIgnore(SimpleNode node, boolean finishClassScope, boolean checkBuiltins) {
    SourceToken token = AbstractVisitor.makeToken(node, "", nature);
    if (checkBuiltins) {
        if (checkCurrentScopeForAssignmentsToBuiltins()) {
            String rep = token.getRepresentation();
            if (builtinTokens.contains(rep)) {
                // Overriding builtin...
                onAddAssignmentToBuiltinMessage(token, rep);
            }
        }
    }
    ScopeItems currScopeItems = scope.getCurrScopeItems();
    Found found = new Found(token, token, scope.getCurrScopeId(), scope.getCurrScopeItems());
    org.python.pydev.shared_core.structure.Tuple<IToken, Found> tup = new org.python.pydev.shared_core.structure.Tuple<IToken, Found>(token, found);
    addToNamesToIgnore(token, currScopeItems, tup);
    // in the 'probably not defined' stack.
    for (Iterator<Found> it = probablyNotDefined.iterator(); it.hasNext(); ) {
        Found n = it.next();
        GenAndTok single = n.getSingle();
        int foundScopeType = single.scopeFound.getScopeType();
        // ok, if we are in a scope method, we may not get things that were defined in a class scope.
        if (((foundScopeType & Scope.ACCEPTED_METHOD_AND_LAMBDA) != 0) && scope.getCurrScopeItems().getScopeType() == Scope.SCOPE_TYPE_CLASS) {
            continue;
        }
        IToken tok = single.tok;
        String firstPart = FullRepIterable.getFirstPart(tok.getRepresentation());
        if (firstPart.equals(token.getRepresentation())) {
            if (finishClassScope && scope.getCurrScopeId() < single.scopeFound.getScopeId() && (!futureAnnotationsImported && foundScopeType == Scope.SCOPE_TYPE_CLASS || (!futureAnnotationsImported && foundScopeType == Scope.SCOPE_TYPE_ANNOTATION))) {
                it.remove();
                onAddUndefinedMessage(tok, found);
            } else {
                it.remove();
                onNotDefinedFoundLater(n, found);
            }
        }
    }
}
Also used : Found(com.python.pydev.analysis.visitors.Found) GenAndTok(com.python.pydev.analysis.visitors.GenAndTok) IToken(org.python.pydev.core.IToken) ScopeItems(com.python.pydev.analysis.visitors.ScopeItems) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) Tuple(org.python.pydev.parser.jython.ast.Tuple)

Aggregations

Found (com.python.pydev.analysis.visitors.Found)3 ScopeItems (com.python.pydev.analysis.visitors.ScopeItems)3 IToken (org.python.pydev.core.IToken)3 GenAndTok (com.python.pydev.analysis.visitors.GenAndTok)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)1 IModule (org.python.pydev.core.IModule)1 IterTokenEntry (org.python.pydev.core.IterTokenEntry)1 TokensList (org.python.pydev.core.TokensList)1 Tuple (org.python.pydev.parser.jython.ast.Tuple)1 FullRepIterable (org.python.pydev.shared_core.string.FullRepIterable)1