Search in sources :

Example 16 with IToken

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

the class CtxParticipant method getCompletionsForMethodParameter.

/**
 * IPyDevCompletionParticipant
 * @throws CompletionRecursionException
 */
@Override
public TokensList getCompletionsForMethodParameter(ICompletionState state, ILocalScope localScope, TokensList interfaceForLocal) throws CompletionRecursionException {
    ArrayList<IToken> ret = new ArrayList<IToken>();
    String qual = state.getQualifier();
    String activationToken = state.getActivationToken();
    FastStack<ISimpleNode> scopeStack = localScope.getScopeStack();
    if (!scopeStack.empty()) {
        Object peek = scopeStack.peek();
        if (peek instanceof FunctionDef) {
            FunctionDef testFuncDef = (FunctionDef) peek;
            String representationString = NodeUtils.getRepresentationString(testFuncDef);
            if (representationString != null && representationString.startsWith("test")) {
                ICodeCompletionASTManager astManager = state.getNature().getAstManager();
                if (astManager != null) {
                    ItemPointer itemPointer = findItemPointerFromPyTestFixture(state.getNature(), state, activationToken);
                    if (itemPointer != null) {
                        TokensList completionsFromItemPointer = getCompletionsFromItemPointer(state, astManager, itemPointer);
                        if (completionsFromItemPointer != null && completionsFromItemPointer.notEmpty()) {
                            return completionsFromItemPointer;
                        }
                    }
                }
            }
        }
    }
    if (qual.length() >= PyCodeCompletionPreferences.getCharsForContextInsensitiveGlobalTokensCompletion()) {
        // at least n characters
        // if we have a parameter, do code-completion with all available tokens, since we don't know what's the type which
        // may actually be received
        boolean useSubstringMatchInCodeCompletion = PyCodeCompletionPreferences.getUseSubstringMatchInCodeCompletion();
        List<IInfo> tokensStartingWith;
        if (useSubstringMatchInCodeCompletion) {
            IFilter nameFilter = PyCodeCompletionUtils.getNameFilter(useSubstringMatchInCodeCompletion, qual);
            try {
                tokensStartingWith = AdditionalProjectInterpreterInfo.getTokensStartingWith("", state.getNature(), AbstractAdditionalTokensInfo.INNER);
            } catch (MisconfigurationException e) {
                Log.log(e);
                return new TokensList(ret);
            }
            for (IInfo info : tokensStartingWith) {
                if (nameFilter.acceptName(info.getName())) {
                    ret.add(new SourceToken(null, info.getName(), null, null, info.getDeclaringModuleName(), info.getType(), info.getNature()));
                }
            }
        } else {
            try {
                tokensStartingWith = AdditionalProjectInterpreterInfo.getTokensStartingWith(qual, state.getNature(), AbstractAdditionalTokensInfo.INNER);
            } catch (MisconfigurationException e) {
                Log.log(e);
                return new TokensList(ret);
            }
            for (IInfo info : tokensStartingWith) {
                ret.add(new SourceToken(null, info.getName(), null, null, info.getDeclaringModuleName(), info.getType(), info.getNature()));
            }
        }
    }
    return new TokensList(ret);
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) IInfo(org.python.pydev.core.IInfo) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IToken(org.python.pydev.core.IToken) IFilter(org.python.pydev.ast.codecompletion.PyCodeCompletionUtils.IFilter) TokensList(org.python.pydev.core.TokensList) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 17 with IToken

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

the class ImportsCompletionParticipant method getImportedNames.

private HashSet<String> getImportedNames(ICompletionState state) {
    TokensList tokenImportedModules = state.getTokenImportedModules();
    HashSet<String> importedNames = new HashSet<String>();
    if (tokenImportedModules != null) {
        for (IterTokenEntry entry : tokenImportedModules) {
            IToken token = entry.getToken();
            importedNames.add(token.getRepresentation());
        }
    }
    return importedNames;
}
Also used : IToken(org.python.pydev.core.IToken) IterTokenEntry(org.python.pydev.core.IterTokenEntry) TokensList(org.python.pydev.core.TokensList) HashSet(java.util.HashSet)

Example 18 with IToken

use of org.python.pydev.core.IToken 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 19 with IToken

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

the class AbstractASTManager method getGlobalCompletions.

/**
 * @see ICodeCompletionASTManager#getGlobalCompletions
 */
@Override
public TokensList getGlobalCompletions(TokensList globalTokens, TokensList importedModules, TokensList wildImportedModules, ICompletionState state, IModule current) {
    if (DebugSettings.DEBUG_CODE_COMPLETION) {
        log("getGlobalCompletions", current, state);
    }
    TokensList completions = new TokensList();
    // in completion with nothing, just go for what is imported and global tokens.
    completions.addAll(globalTokens);
    // now go for the token imports
    completions.addAll(importedModules);
    if (!state.getBuiltinsGotten()) {
        state.setBuiltinsGotten(true);
        if (DebugSettings.DEBUG_CODE_COMPLETION) {
            org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "getBuiltinCompletions");
        }
        // last thing: get completions from module __builtin__
        getBuiltinCompletions(state, completions);
        if (DebugSettings.DEBUG_CODE_COMPLETION) {
            org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "END getBuiltinCompletions");
        }
    }
    // filtered out if they are gotten from a wild import and not from the module itself.
    for (IterTokenEntry entry : wildImportedModules) {
        IToken name = entry.getToken();
        // for wild imports, we must get the global completions with __all__ filtered
        getCompletionsForWildImport(state, current, completions, name);
    }
    return completions;
}
Also used : IToken(org.python.pydev.core.IToken) IterTokenEntry(org.python.pydev.core.IterTokenEntry) TokensList(org.python.pydev.core.TokensList)

Example 20 with IToken

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

the class AbstractASTManager method findOnImportedMods.

/**
 * This function tries to find some activation token defined in some imported module.
 * @return tuple with: the module and the token that should be used from it.
 *
 * @param this is the activation token we have. It may be a single token or some dotted name.
 *
 * If it is a dotted name, such as testcase.TestCase, we need to match against some import
 * represented as testcase or testcase.TestCase.
 *
 * If a testcase.TestCase matches against some import named testcase, the import is returned and
 * the TestCase is put as the module
 *
 * 0: mod
 * 1: tok (string)
 * 2: actual tok
 * @throws CompletionRecursionException
 */
@Override
public Tuple3<IModule, String, IToken> findOnImportedMods(TokensList importedModules, ICompletionState state, String currentModuleName, IModule current) throws CompletionRecursionException {
    FullRepIterable iterable = new FullRepIterable(state.getActivationToken(), true);
    for (String tok : iterable) {
        for (IterTokenEntry entry : importedModules) {
            IToken importedModule = entry.getToken();
            // this is its 'real' representation (alias) on the file (if it is from xxx import a as yyy, it is yyy)
            final String modRep = importedModule.getRepresentation();
            if (modRep.equals(tok)) {
                String act = state.getActivationToken();
                Tuple<IModule, String> r;
                try {
                    r = findOnImportedMods(importedModule, tok, state, act, currentModuleName, current);
                    if (r != null) {
                        return new Tuple3<IModule, String, IToken>(r.o1, r.o2, importedModule);
                    }
                // Note, if r==null, even though the name matched, keep on going (to handle cases of
                // try..except ImportError, as we cannot be sure of which version will actually match).
                } catch (MisconfigurationException e) {
                    Log.log(e);
                }
            }
        }
    }
    return null;
}
Also used : IModule(org.python.pydev.core.IModule) IToken(org.python.pydev.core.IToken) FullRepIterable(org.python.pydev.shared_core.string.FullRepIterable) MisconfigurationException(org.python.pydev.core.MisconfigurationException) Tuple3(org.python.pydev.shared_core.structure.Tuple3) IterTokenEntry(org.python.pydev.core.IterTokenEntry)

Aggregations

IToken (org.python.pydev.core.IToken)120 TokensList (org.python.pydev.core.TokensList)48 IterTokenEntry (org.python.pydev.core.IterTokenEntry)35 Document (org.eclipse.jface.text.Document)34 ArrayList (java.util.ArrayList)29 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)25 IModule (org.python.pydev.core.IModule)20 SimpleNode (org.python.pydev.parser.jython.SimpleNode)20 ICompletionState (org.python.pydev.core.ICompletionState)16 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)15 HashSet (java.util.HashSet)11 MisconfigurationException (org.python.pydev.core.MisconfigurationException)11 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)11 List (java.util.List)10 Found (com.python.pydev.analysis.visitors.Found)9 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)9 File (java.io.File)8 HashMap (java.util.HashMap)8 NameTok (org.python.pydev.parser.jython.ast.NameTok)8 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)8