Search in sources :

Example 26 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class OccurrencesVisitor method onFoundTokenAs.

@Override
protected void onFoundTokenAs(IToken token, Found foundAs) {
    if (analyzeArgumentsMismatch) {
        boolean reportFound = true;
        try {
            if (foundAs.importInfo != null) {
                IDefinition[] definition = foundAs.importInfo.getDefinitions(nature, completionCache);
                for (IDefinition iDefinition : definition) {
                    Definition d = (Definition) iDefinition;
                    if (d.ast instanceof FunctionDef || d.ast instanceof ClassDef) {
                        SourceToken tok = AbstractVisitor.makeToken(d.ast, token.getRepresentation(), d.module != null ? d.module.getName() : "", d.module != null ? d.module.getNature() : null);
                        tok.setDefinition(d);
                        onPushToRecordedFounds(tok);
                        reportFound = false;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            Log.log(e);
        }
        if (reportFound) {
            onPushToRecordedFounds(token);
        }
    }
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) IDefinition(org.python.pydev.core.IDefinition) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)

Example 27 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class AbstractScopeAnalyzerVisitor method visitAttribute.

/**
 * visiting some attribute, as os.path or math().val or (10,10).__class__
 *
 * @see org.python.pydev.parser.jython.ast.VisitorIF#visitAttribute(org.python.pydev.parser.jython.ast.Attribute)
 */
@Override
public Object visitAttribute(Attribute node) throws Exception {
    SourceToken token = AbstractVisitor.makeFullNameToken(node, moduleName, nature);
    unhandled_node(node);
    visitingAttributeStackI += 1;
    if (visitingAttributeStackI == 1) {
        // Mostly a cache helper so that we don't incur the cost of creating a new one every time we find
        // an attribute, as only one will be active at any time when we start checking an attribute.
        visitingAttributeStack = visitingAttributeStackCache;
    }
    String representation = token.getRepresentation();
    int curr = visitingAttributeStack.increment(representation);
    try {
        boolean doReturn = visitNeededAttributeParts(node, this);
        if (representation.length() == 0) {
            return null;
        }
        if (doReturn) {
            return null;
        }
        String fullRep = representation;
        if (node.ctx == Attribute.Store || node.ctx == Attribute.NamedStore || node.ctx == Attribute.Param || node.ctx == Attribute.KwOnlyParam || node.ctx == Attribute.AugStore) {
            // in a store attribute, the first part is always a load
            int i = fullRep.indexOf('.', 0);
            String sub = fullRep;
            if (i > 0) {
                sub = fullRep.substring(0, i);
            }
            markRead(token, sub, true, false);
        } else if (node.ctx == Attribute.Load) {
            Iterator<String> it = new FullRepIterable(fullRep).iterator();
            boolean found = false;
            while (it.hasNext()) {
                String sub = it.next();
                if (it.hasNext()) {
                    if (markRead(token, sub, false, false)) {
                        found = true;
                    }
                } else {
                    // I.e.: if we're in a structure where an attribute visits another attribute, we'll only
                    // report as not defined if it's the last one (i.e.: the last one in the stack we created).
                    boolean addToNotDefined = !found && visitingAttributeStack.get(representation) == curr;
                    // only set it to add to not defined if it was still not found
                    markRead(token, fullRep, addToNotDefined, true);
                }
            }
        }
    } finally {
        visitingAttributeStackI -= 1;
        if (visitingAttributeStackI == 0) {
            visitingAttributeStack.clear();
            visitingAttributeStack = null;
        }
    }
    return null;
}
Also used : FullRepIterable(org.python.pydev.shared_core.string.FullRepIterable) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)

Example 28 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken 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)

Example 29 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class AbstractScopeAnalyzerVisitor method findNameTok.

protected IToken findNameTok(IToken token, String tokToCheck) {
    if (token instanceof SourceToken) {
        SourceToken s = (SourceToken) token;
        SimpleNode ast = s.getAst();
        String searchFor = FullRepIterable.getLastPart(tokToCheck);
        while (ast instanceof Attribute) {
            Attribute a = (Attribute) ast;
            if (((NameTok) a.attr).id.equals(searchFor)) {
                return new SourceToken(a.attr, searchFor, "", "", token.getParentPackage(), nature);
            } else if (a.value.toString().equals(searchFor)) {
                return new SourceToken(a.value, searchFor, "", "", token.getParentPackage(), nature);
            }
            ast = a.value;
        }
    }
    return token;
}
Also used : Attribute(org.python.pydev.parser.jython.ast.Attribute) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 30 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class AbstractScopeAnalyzerVisitor method visitName.

/**
 * Visiting some name
 *
 * @see org.python.pydev.parser.jython.ast.VisitorIF#visitName(org.python.pydev.parser.jython.ast.Name)
 */
@Override
public Object visitName(Name node) throws Exception {
    unhandled_node(node);
    // when visiting the global namespace, we don't go into any inner scope.
    SourceToken token = AbstractVisitor.makeToken(node, moduleName, nature);
    boolean found = true;
    // on aug assign, it has to enter both, the load and the read (but first the load, because it can be undefined)
    if (node.ctx == Name.Load || node.ctx == Name.Del || node.ctx == Name.AugStore) {
        found = markRead(token);
    }
    if (node.ctx == Name.Store || node.ctx == Attribute.NamedStore || node.ctx == Name.Param || node.ctx == Name.KwOnlyParam || (node.ctx == Name.AugStore && found)) {
        // if it was undefined on augstore, we do not go on to creating the token
        String rep = token.getRepresentation();
        if (checkCurrentScopeForAssignmentsToBuiltins()) {
            if (builtinTokens.contains(rep)) {
                // Overriding builtin...
                onAddAssignmentToBuiltinMessage(token, rep);
            }
        }
        org.python.pydev.shared_core.structure.Tuple<IToken, Found> foundInNamesToIgnore = findInNamesToIgnore(rep, token);
        if (foundInNamesToIgnore == null) {
            if (!rep.equals("self") && !rep.equals("cls")) {
                scope.addToken(token, token);
                if (node.ctx == Attribute.NamedStore) {
                    markRead(token);
                }
            } else {
                // ignore self
                addToNamesToIgnore(node, false, false);
            }
        }
    }
    return token;
}
Also used : IToken(org.python.pydev.core.IToken) Found(com.python.pydev.analysis.visitors.Found) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)

Aggregations

SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)50 SimpleNode (org.python.pydev.parser.jython.SimpleNode)26 IToken (org.python.pydev.core.IToken)25 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)13 TokensList (org.python.pydev.core.TokensList)12 ArrayList (java.util.ArrayList)11 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)11 IModule (org.python.pydev.core.IModule)9 NameTok (org.python.pydev.parser.jython.ast.NameTok)9 ICompletionState (org.python.pydev.core.ICompletionState)8 ImportFrom (org.python.pydev.parser.jython.ast.ImportFrom)8 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)8 Import (org.python.pydev.parser.jython.ast.Import)7 ISimpleNode (org.python.pydev.shared_core.model.ISimpleNode)7 IterTokenEntry (org.python.pydev.core.IterTokenEntry)6 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)6 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)6 HashSet (java.util.HashSet)5 Attribute (org.python.pydev.parser.jython.ast.Attribute)5 Found (com.python.pydev.analysis.visitors.Found)4