Search in sources :

Example 11 with SourceToken

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

the class OccurrencesVisitor method visitCompare.

@Override
public Object visitCompare(Compare node) throws Exception {
    Object ret = super.visitCompare(node);
    if (isInTestScope == 0) {
        SourceToken token = AbstractVisitor.makeToken(node, moduleName, this.nature);
        messagesManager.addMessage(IAnalysisPreferences.TYPE_NO_EFFECT_STMT, token);
    }
    return ret;
}
Also used : SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)

Example 12 with SourceToken

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

the class OccurrencesVisitor method onVisitCallFunc.

@Override
protected void onVisitCallFunc(final Call callNode) throws Exception {
    if (!analyzeArgumentsMismatch) {
        super.onVisitCallFunc(callNode);
    } else {
        if (callNode.func instanceof Name) {
            Name name = (Name) callNode.func;
            startRecordFound();
            visitName(name);
            // Check if the name was actually found in some way...
            TokenFoundStructure found = popFound();
            if (found != null && found.token instanceof SourceToken) {
                final SourceToken sourceToken = (SourceToken) found.token;
                if (found.defined) {
                    argumentsChecker.checkNameFound(callNode, sourceToken);
                } else if (found.found != null) {
                    // Still not found: register a callback to be called if it's found later on.
                    found.found.registerCallOnDefined(new ICallbackListener<Found>() {

                        @Override
                        public Object call(Found f) {
                            try {
                                List<GenAndTok> all = f.getAll();
                                for (GenAndTok genAndTok : all) {
                                    if (genAndTok.tok instanceof SourceToken) {
                                        SourceToken sourceToken2 = (SourceToken) genAndTok.tok;
                                        if (sourceToken2.getAst() instanceof FunctionDef || sourceToken2.getAst() instanceof ClassDef) {
                                            argumentsChecker.checkNameFound(callNode, sourceToken2);
                                            return null;
                                        }
                                    }
                                }
                            } catch (Exception e) {
                                Log.log(e);
                            }
                            return null;
                        }
                    });
                }
            }
        } else {
            startRecordFound();
            callNode.func.accept(this);
            TokenFoundStructure found = popFound();
            argumentsChecker.checkAttrFound(callNode, found);
        }
    }
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) ICallbackListener(org.python.pydev.shared_core.callbacks.ICallbackListener) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) Name(org.python.pydev.parser.jython.ast.Name)

Example 13 with SourceToken

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

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

the class AbstractASTManager method findOnImportedMods.

/**
 * Checks if some module can be resolved and returns the module it is resolved to (and to which token).
 * @throws CompletionRecursionException
 * @throws MisconfigurationException
 */
public Tuple<IModule, String> findOnImportedMods(IToken importedModule, String tok, ICompletionState state, String activationToken, String currentModuleName, IModule current) throws CompletionRecursionException, MisconfigurationException {
    Tuple<IModule, String> modTok = null;
    IModule mod = null;
    // ok, check if it is a token for the new import
    IPythonNature nature = state.getNature();
    if (importedModule instanceof SourceToken) {
        SourceToken token = (SourceToken) importedModule;
        if (token.isImportFrom()) {
            ImportFrom importFrom = (ImportFrom) token.getAst();
            int level = importFrom.level;
            if (level > 0) {
                // ok, it must be treated as a relative import
                // ok, it is the import added on python 2.5 (from .. import xxx)
                String parentPackage = token.getParentPackage();
                List<String> moduleParts = StringUtils.dotSplit(parentPackage);
                String relative = null;
                if (moduleParts.size() > level) {
                    relative = FullRepIterable.joinParts(moduleParts, moduleParts.size() - level);
                }
                String modName = ((NameTok) importFrom.module).id;
                if (modName.length() > 0) {
                    // ok, we have to add the other part too, as we have more than the leading dots
                    // from ..bar import
                    relative += "." + modName;
                }
                if (!AbstractVisitor.isWildImport(importFrom)) {
                    tok = FullRepIterable.getLastPart(token.originalRep);
                    relative += "." + tok;
                }
                modTok = findModuleFromPath(relative, nature, false, null, state);
                mod = modTok.o1;
                if (checkValidity(currentModuleName, mod)) {
                    Tuple<IModule, String> ret = fixTok(modTok, tok, activationToken);
                    return ret;
                }
                // ok, it is 'forced' as relative import because it has a level, so, it MUST return here
                return null;
            }
        }
    }
    boolean isAbsoluteImportEnabledx = this.isAbsoluteImportEnabled(current, nature);
    String asRelativeImport = "";
    if (!isAbsoluteImportEnabledx) {
        // check as relative with complete rep
        asRelativeImport = importedModule.getAsRelativeImport(currentModuleName);
        if (!asRelativeImport.startsWith(".")) {
            modTok = findModuleFromPath(asRelativeImport, nature, true, currentModuleName, state);
            mod = modTok.o1;
            if (checkValidity(currentModuleName, mod)) {
                Tuple<IModule, String> ret = fixTok(modTok, tok, activationToken);
                return ret;
            }
        }
    }
    // check if the import actually represents some token in an __init__ file
    String originalWithoutRep = importedModule.getOriginalWithoutRep();
    if (originalWithoutRep.length() > 0) {
        if (!originalWithoutRep.endsWith("__init__")) {
            originalWithoutRep = originalWithoutRep + ".__init__";
        }
        modTok = findModuleFromPath(originalWithoutRep, nature, true, null, state);
        mod = modTok.o1;
        if (modTok.o2.endsWith("__init__") == false && checkValidity(currentModuleName, mod)) {
            if (mod.isInGlobalTokens(importedModule.getRepresentation(), nature, false, state)) {
                // then this is the token we're looking for (otherwise, it might be a module).
                Tuple<IModule, String> ret = fixTok(modTok, tok, activationToken);
                if (ret.o2.length() == 0) {
                    ret.o2 = importedModule.getRepresentation();
                } else {
                    ret.o2 = importedModule.getRepresentation() + "." + ret.o2;
                }
                return ret;
            }
        }
    }
    // the most 'simple' case: check as absolute with original rep
    modTok = findModuleFromPath(importedModule.getOriginalRep(), nature, false, null, state);
    mod = modTok.o1;
    if (checkValidity(currentModuleName, mod)) {
        Tuple<IModule, String> ret = fixTok(modTok, tok, activationToken);
        return ret;
    }
    if (!isAbsoluteImportEnabledx) {
        // ok, one last shot, to see a relative looking in folders __init__
        modTok = findModuleFromPath(asRelativeImport, nature, false, null, state);
        mod = modTok.o1;
        if (checkValidity(currentModuleName, mod, true)) {
            Tuple<IModule, String> ret = fixTok(modTok, tok, activationToken);
            // if we didn't find it in an __init__ module, all should be ok
            if (!mod.getName().endsWith("__init__")) {
                return ret;
            } else // if none of this situations was found, we probably just found the same token we had when we started (unless I'm mistaken...)
            if (activationToken.length() == 0 || ret.o2.equals(activationToken) == false || mod.isInGlobalTokens(activationToken, nature, false, state)) {
                return ret;
            }
        }
    }
    return null;
}
Also used : IModule(org.python.pydev.core.IModule) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) IPythonNature(org.python.pydev.core.IPythonNature) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 15 with SourceToken

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

the class AbstractASTManager method getTokToSearchInOtherModule.

/**
 * When we have an import, we have one token which we used to find it and another which is the
 * one we refer to at the current module. This method will get the way it's referred at the
 * actual module and not at the current module (at the current module it's modTok.o2).
 */
public static String getTokToSearchInOtherModule(Tuple3<IModule, String, IToken> modTok) {
    String tok = modTok.o2;
    String tokForSearchInOtherModule = tok;
    if (tok.length() > 0) {
        IToken sourceToken = modTok.o3;
        if (sourceToken instanceof SourceToken) {
            SourceToken sourceToken2 = (SourceToken) sourceToken;
            if (sourceToken2.getAst() instanceof ImportFrom) {
                ImportFrom importFrom = (ImportFrom) sourceToken2.getAst();
                if (importFrom.names.length > 0 && importFrom.names[0].asname != null) {
                    String originalRep = sourceToken.getOriginalRep();
                    tokForSearchInOtherModule = FullRepIterable.getLastPart(originalRep);
                }
            }
        }
    }
    return tokForSearchInOtherModule;
}
Also used : IToken(org.python.pydev.core.IToken) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) 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