Search in sources :

Example 1 with ILocalScope

use of org.python.pydev.core.ILocalScope in project Pydev by fabioz.

the class AbstractASTManager method internalGenerateGetCompletionsForModule.

/**
 * This method should only be accessed from the public getCompletionsForModule (which caches the result).
 */
private TokensList internalGenerateGetCompletionsForModule(IModule module, ICompletionState state, boolean searchSameLevelMods, boolean lookForArgumentCompletion) throws CompletionRecursionException {
    if (DebugSettings.DEBUG_CODE_COMPLETION) {
        log("internalGenerateGetCompletionsForModule", module, state);
    }
    state.checkMaxTimeForCompletion();
    TokensList importedModules = new TokensList();
    ILocalScope localScope = null;
    int line = state.getLine();
    int col = state.getCol();
    if (state.getLocalImportsGotten() == false) {
        // in the first analyzed module, we have to get the local imports too.
        state.setLocalImportsGotten(true);
        if (module != null && line >= 0) {
            localScope = module.getLocalScope(line, col);
            if (localScope != null) {
                importedModules.addAll(localScope.getLocalImportedModules(line + 1, col + 1, module.getName()));
            }
        }
    }
    TokensList builtinsCompletions = getBuiltinsCompletions(state);
    if (builtinsCompletions != null) {
        return builtinsCompletions;
    }
    String act = state.getActivationToken();
    int parI = act.indexOf('(');
    if (parI != -1) {
        state.setFullActivationToken(act);
        act = ParsingUtils.removeCalls(act);
        state.setActivationToken(act);
        state.setLookingFor(ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE);
    }
    if (module != null) {
        // get the tokens (global, imported and wild imported)
        TokensList globalTokens = module.getGlobalTokens();
        TokensList tokenImportedModules = module.getTokenImportedModules();
        importedModules.addAll(tokenImportedModules);
        state.setTokenImportedModules(importedModules);
        TokensList wildImportedModules = module.getWildImportedModules();
        // now, lets check if this is actually a module that is an __init__ (if so, we have to get all
        // the other .py files as modules that are in the same level as the __init__)
        Set<IToken> initial = new HashSet<IToken>();
        if (searchSameLevelMods) {
            // or only folders -- not classes -- in java).
            if (module.isPackage()) {
                HashSet<IToken> gotten = new HashSet<IToken>();
                // the module also decides how to get its submodules
                getAbsoluteImportTokens(module.getPackageFolderName(), gotten, IToken.TYPE_IMPORT, true, null, false);
                for (IToken token : gotten) {
                    if (token.getRepresentation().equals("__init__") == false) {
                        initial.add(token);
                    }
                }
            }
        }
        if (state.getActivationToken().length() == 0) {
            TokensList completions = getGlobalCompletions(globalTokens, importedModules, wildImportedModules, state, module);
            // now find the locals for the module
            if (line >= 0) {
                TokensList localTokens = module.getLocalTokens(line, col, localScope);
                completions.addAll(localTokens);
            }
            // just add all that are in the same level if it was an __init__
            completions.addAll(new TokensList(initial.toArray(EMPTY_ITOKEN_ARRAY)));
            return completions;
        } else {
            // ok, we have a token, find it and get its completions.
            // first check if the token is a module... if it is, get the completions for that module.
            TokensList tokens = findTokensOnImportedMods(importedModules, state, module);
            if (tokens != null && tokens.size() > 0) {
                return decorateWithLocal(tokens, localScope, state);
            }
            // if it is an __init__, modules on the same level are treated as local tokens
            if (searchSameLevelMods) {
                tokens = searchOnSameLevelMods(initial, state);
                if (tokens != null && tokens.size() > 0) {
                    return decorateWithLocal(tokens, localScope, state);
                }
            }
            // wild imports: recursively go and get those completions and see if any matches it.
            for (IterTokenEntry entry : wildImportedModules) {
                IToken name = entry.getToken();
                IModule mod = getModule(name.getAsRelativeImport(module.getName()), state.getNature(), false, // relative (for wild imports this is ok... only a module can be used in wild imports)
                state);
                if (mod == null) {
                    // absolute
                    mod = getModule(name.getOriginalRep(), state.getNature(), false, state);
                }
                if (mod != null) {
                    state.checkFindModuleCompletionsMemory(mod, state.getActivationToken());
                    TokensList completionsForModule = getCompletionsForModule(mod, state);
                    if (completionsForModule.size() > 0) {
                        return decorateWithLocal(completionsForModule, localScope, state);
                    }
                } else {
                // "Module not found:" + name.getRepresentation()
                }
            }
            // it was not a module (would have returned already), so, try to get the completions for a global token defined.
            tokens = module.getGlobalTokens(state, this);
            if (tokens.size() > 0) {
                return decorateWithLocal(tokens, localScope, state);
            }
            // If it was still not found, go to builtins.
            IModule builtinsMod = getBuiltinMod(state.getNature(), state);
            if (builtinsMod != null && builtinsMod != module) {
                tokens = getCompletionsForModule(builtinsMod, state);
                if (tokens.notEmpty()) {
                    if (tokens.getFirst().getRepresentation().equals("ERROR:") == false) {
                        return decorateWithLocal(tokens, localScope, state);
                    }
                }
            }
            // Let's check if we have to unpack it...
            try (NoExceptionCloseable x = state.pushLookingFor(ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE)) {
                if (state.getActivationToken().endsWith(".__getitem__")) {
                    String activationToken = state.getActivationToken();
                    String compoundActivationToken = activationToken.substring(0, activationToken.length() - 12);
                    TokensList ret = getCompletionsUnpackingObject(module, state.getCopyWithActTok(compoundActivationToken), localScope, new UnpackInfo());
                    if (ret != null && ret.size() > 0) {
                        return ret;
                    }
                } else {
                    if (localScope != null) {
                        ISimpleNode foundAtASTNode = localScope.getFoundAtASTNode();
                        if (foundAtASTNode instanceof For) {
                            For for1 = (For) foundAtASTNode;
                            // case where we may have to unpack some iteration
                            // e.g.: for a, b in x.items():
                            TokensList ret = getCompletionsUnpackingForLoop(module, state, localScope, for1);
                            if (ret != null && ret.size() > 0) {
                                return ret;
                            }
                        // Note: we don't bail out here because it's possible that the user has
                        // added the type on the context (because on a for unpacking either we find it
                        // when checking the for loop unpack or the user has to explicitly give
                        // us a hint).
                        } else if (foundAtASTNode instanceof With) {
                            With with = (With) foundAtASTNode;
                            TokensList ret = getCompletionsUnpackingWith(module, state, localScope, with);
                            if (ret != null && ret.size() > 0) {
                                return ret;
                            }
                        }
                    }
                }
            }
            if (lookForArgumentCompletion && localScope != null) {
                TokensList ret = getCompletionsFromTokenInLocalScope(module, state, searchSameLevelMods, lookForArgumentCompletion, localScope);
                if (ret != null && ret.size() > 0) {
                    return ret;
                }
            }
            // nothing worked so far, so, let's look for an assignment...
            return getAssignCompletions(module, state, lookForArgumentCompletion, localScope);
        }
    } else {
        Log.log("Module passed in is null!!");
    }
    return new TokensList();
}
Also used : UnpackInfo(org.python.pydev.core.UnpackInfo) IModule(org.python.pydev.core.IModule) For(org.python.pydev.parser.jython.ast.For) LookingFor(org.python.pydev.core.ICompletionState.LookingFor) IterTokenEntry(org.python.pydev.core.IterTokenEntry) NoExceptionCloseable(org.python.pydev.core.NoExceptionCloseable) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) With(org.python.pydev.parser.jython.ast.With) IToken(org.python.pydev.core.IToken) TokensList(org.python.pydev.core.TokensList) ILocalScope(org.python.pydev.core.ILocalScope) HashSet(java.util.HashSet)

Example 2 with ILocalScope

use of org.python.pydev.core.ILocalScope in project Pydev by fabioz.

the class FindDefinitionModelVisitor method visitNamedExpr.

/**
 * @see org.python.pydev.parser.jython.ast.VisitorBase#visitNamedExpr(org.python.pydev.parser.jython.ast.NamedExpr)
 */
@Override
public Object visitNamedExpr(NamedExpr node) throws Exception {
    ILocalScope scope = new LocalScope(nature, this.defsStack);
    scope.setFoundAtASTNode(node);
    if (foundAsDefinition && !scope.equals(definitionFound.scope)) {
        // if it is found as a definition it is an 'exact' match, so, we do not keep checking it
        return null;
    }
    if (tokenToFind.equals(NodeUtils.getFullRepresentationString(node.target)) && node.value != null) {
        String rep = NodeUtils.getFullRepresentationString(node.value);
        if (rep != null && !rep.isEmpty()) {
            definitions.add(getAssignDefinition(new Assign(new exprType[] { node.target }, node.value, null), rep, 0, line, col, scope, module.get(), -1));
        }
    }
    return super.visitNamedExpr(node);
}
Also used : Assign(org.python.pydev.parser.jython.ast.Assign) ILocalScope(org.python.pydev.core.ILocalScope) ILocalScope(org.python.pydev.core.ILocalScope)

Example 3 with ILocalScope

use of org.python.pydev.core.ILocalScope in project Pydev by fabioz.

the class FindDefinitionModelVisitor method visitNameTok.

@Override
public Object visitNameTok(NameTok node) throws Exception {
    if (node.ctx == NameTok.KeywordName) {
        if (this.line == node.beginLine && this.call.size() > 0) {
            String rep = NodeUtils.getRepresentationString(node);
            if (PySelection.isInside(col, node.beginColumn, rep.length())) {
                foundAsDefinition = true;
                // if it is found as a definition it is an 'exact' match, so, erase all the others.
                ILocalScope scope = new LocalScope(nature, this.defsStack);
                for (Iterator<Definition> it = definitions.iterator(); it.hasNext(); ) {
                    Definition d = it.next();
                    if (!d.scope.equals(scope)) {
                        it.remove();
                    }
                }
                definitions.clear();
                definitionFound = new KeywordParameterDefinition(line, node.beginColumn, rep, node, scope, module.get(), this.call.peek());
                definitions.add(definitionFound);
                throw STOP_VISITING_EXCEPTION;
            }
        }
    }
    return null;
}
Also used : ILocalScope(org.python.pydev.core.ILocalScope) ILocalScope(org.python.pydev.core.ILocalScope)

Example 4 with ILocalScope

use of org.python.pydev.core.ILocalScope in project Pydev by fabioz.

the class FindDefinitionModelVisitor method checkParam.

/**
 * @param node the declaration node we're interested in (class or function)
 * @param name the token that represents the name of that declaration
 */
private void checkParam(Name name) {
    String rep = NodeUtils.getRepresentationString(name);
    if (rep.equals(tokenToFind) && line == name.beginLine && col >= name.beginColumn && col <= name.beginColumn + rep.length()) {
        foundAsDefinition = true;
        // if it is found as a definition it is an 'exact' match, so, erase all the others.
        ILocalScope scope = new LocalScope(this.nature, this.defsStack);
        for (Iterator<Definition> it = definitions.iterator(); it.hasNext(); ) {
            Definition d = it.next();
            if (!d.scope.equals(scope)) {
                it.remove();
            }
        }
        definitionFound = new Definition(line, name.beginColumn, rep, name, scope, module.get());
        definitions.add(definitionFound);
    }
}
Also used : ILocalScope(org.python.pydev.core.ILocalScope) ILocalScope(org.python.pydev.core.ILocalScope)

Example 5 with ILocalScope

use of org.python.pydev.core.ILocalScope in project Pydev by fabioz.

the class FindDefinitionModelVisitor method visitListComp.

@Override
public Object visitListComp(ListComp node) throws Exception {
    exprType elt = node.elt;
    elt = fixMissingAttribute(elt);
    if (this.line == elt.beginLine) {
        ILocalScope scope = new LocalScope(nature, this.defsStack);
        scope.setFoundAtASTNode(node);
        if (foundAsDefinition && !scope.equals(definitionFound.scope)) {
            // if it is found as a definition it is an 'exact' match, so, we do not keep checking it
            return super.visitListComp(node);
        }
        if (this.tokenToFind.equals(NodeUtils.getRepresentationString(elt))) {
            // Something as [a for a in [F(), C()]]
            if (node.generators != null && node.generators.length == 1) {
                comprehensionType comprehensionType = node.generators[0];
                if (comprehensionType instanceof Comprehension) {
                    Comprehension comprehension = (Comprehension) comprehensionType;
                    if (comprehension.iter != null) {
                        if (this.tokenToFind.equals(NodeUtils.getRepresentationString(comprehension.target))) {
                            exprType[] elts = NodeUtils.getEltsFromCompoundObject(comprehension.iter);
                            String rep = "";
                            if (elts != null && elts.length > 0) {
                                rep = NodeUtils.getRepresentationString(elts[0]);
                            }
                            ListCompDefinition definition = new ListCompDefinition(rep, this.tokenToFind, node, line, col, scope, module.get());
                            definitions.add(definition);
                        }
                    }
                }
            }
        } else if (elt instanceof Tuple || elt instanceof List) {
            // something as [(a, b) for (a, b) in [(F(), G()), ...]]
            exprType[] eltsFromCompoundObject = NodeUtils.getEltsFromCompoundObject(elt);
            if (eltsFromCompoundObject != null) {
                int length = eltsFromCompoundObject.length;
                for (int i = 0; i < length; i++) {
                    exprType eltFromCompound = fixMissingAttribute(eltsFromCompoundObject[i]);
                    if (this.tokenToFind.equals(NodeUtils.getRepresentationString(eltFromCompound))) {
                        if (node.generators != null && node.generators.length == 1) {
                            comprehensionType comprehensionType = node.generators[0];
                            if (comprehensionType instanceof Comprehension) {
                                Comprehension comprehension = (Comprehension) comprehensionType;
                                if (comprehension.iter != null) {
                                    exprType target = comprehension.target;
                                    if (target != null) {
                                        exprType[] targetElts = NodeUtils.getEltsFromCompoundObject(target);
                                        if (targetElts != null) {
                                            for (int j = 0; j < targetElts.length; j++) {
                                                exprType targetElt = targetElts[j];
                                                if (this.tokenToFind.equals(NodeUtils.getRepresentationString(targetElt))) {
                                                    exprType[] elts = NodeUtils.getEltsFromCompoundObject(comprehension.iter);
                                                    String rep = "";
                                                    if (elts != null && elts.length > j) {
                                                        rep = NodeUtils.getRepresentationString(elts[j]);
                                                    }
                                                    ListCompDefinition definition = new ListCompDefinition(rep, this.tokenToFind, node, line, col, scope, module.get());
                                                    definitions.add(definition);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return super.visitListComp(node);
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) ArrayList(java.util.ArrayList) List(java.util.List) Comprehension(org.python.pydev.parser.jython.ast.Comprehension) org.python.pydev.parser.jython.ast.comprehensionType(org.python.pydev.parser.jython.ast.comprehensionType) Tuple(org.python.pydev.parser.jython.ast.Tuple) ILocalScope(org.python.pydev.core.ILocalScope) ILocalScope(org.python.pydev.core.ILocalScope)

Aggregations

ILocalScope (org.python.pydev.core.ILocalScope)14 ArrayList (java.util.ArrayList)4 List (java.util.List)4 FindScopeVisitor (org.python.pydev.ast.codecompletion.revisited.visitors.FindScopeVisitor)3 IToken (org.python.pydev.core.IToken)3 ITypeInfo (org.python.pydev.core.ITypeInfo)3 TokensList (org.python.pydev.core.TokensList)3 SimpleNode (org.python.pydev.parser.jython.SimpleNode)3 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)3 ICompletionState (org.python.pydev.core.ICompletionState)2 IModule (org.python.pydev.core.IModule)2 IterTokenEntry (org.python.pydev.core.IterTokenEntry)2 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)2 Assign (org.python.pydev.parser.jython.ast.Assign)2 Tuple (org.python.pydev.parser.jython.ast.Tuple)2 FastStack (org.python.pydev.shared_core.structure.FastStack)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 AbstractASTManager (org.python.pydev.ast.codecompletion.revisited.AbstractASTManager)1 CompletionState (org.python.pydev.ast.codecompletion.revisited.CompletionState)1