Search in sources :

Example 1 with SourceToken

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

the class AnalysisPlugin method getDefinitionFromIInfo.

/**
 * @param pointers the list where the pointers will be added (if null, a new one will be created).
 * @param manager the manager to be used to get the definition.
 * @param nature the nature to be used.
 * @param info the info that we are looking for.
 * @param force whether we should force getting the ItemPointer if it's not readily available.
 * @return whether we actually tried to look for a completion or just bailed out due to force being == false.
 */
public static boolean getDefinitionFromIInfo(List<ItemPointer> pointers, ICodeCompletionASTManager manager, IPythonNature nature, IInfo info, ICompletionState completionCache, boolean requireIDefinition, boolean force) {
    if (pointers == null) {
        pointers = new ArrayList<>();
    }
    if (!requireIDefinition) {
        String file = info.getFile();
        if (file != null) {
            File f = new File(file);
            int line = info.getLine();
            int col = info.getCol();
            if (line > 0 && col > 0) {
                // 0 is invalid.
                ItemPointer itemPointer = new ItemPointer(f, new Location(line - 1, col - 1), new Location(line - 1, col - 1), null, null, f.toURI());
                pointers.add(itemPointer);
                return true;
            }
        }
    }
    if (!force) {
        return false;
    }
    IModule mod;
    String tok;
    mod = manager.getModule(info.getDeclaringModuleName(), nature, true, completionCache);
    if (mod != null) {
        if (info.getType() == IInfo.MOD_IMPORT_TYPE) {
            Definition definition = new Definition(1, 1, "", null, null, mod);
            PyRefactoringFindDefinition.getAsPointers(pointers, new Definition[] { definition });
            return true;
        }
        // ok, now that we found the module, we have to get the actual definition
        tok = "";
        String path = info.getPath();
        if (path != null && path.length() > 0) {
            tok = path + ".";
        }
        tok += info.getName();
        try {
            IDefinition[] definitions = mod.findDefinition(CompletionStateFactory.getEmptyCompletionState(tok, nature, completionCache), -1, -1, nature);
            if ((definitions == null || definitions.length == 0) && path != null && path.length() > 0) {
                // this can happen if we have something as an attribute in the path:
                // class Bar(object):
                // def __init__(self):
                // self.xxx = 10
                // 
                // so, we'de get a find definition for Bar.__init__.xxx which is something we won't find
                // for now, let's simply return a match in the correct context (although the correct way of doing
                // it would be analyzing that context to find the match)
                IDefinition[] contextDefinitions = mod.findDefinition(CompletionStateFactory.getEmptyCompletionState(path, nature, completionCache), -1, -1, nature);
                if (contextDefinitions != null && contextDefinitions.length > 0) {
                    for (IDefinition iDefinition : contextDefinitions) {
                        if (iDefinition instanceof Definition) {
                            Definition definition = (Definition) iDefinition;
                            if (definition.ast instanceof FunctionDef) {
                                FunctionDef functionDef = (FunctionDef) definition.ast;
                                if (functionDef.args != null) {
                                    exprType[] args = functionDef.args.args;
                                    if (args != null && args.length > 0) {
                                        // I.e.: only analyze functions with at least one argument (for self or cls).
                                        Map<String, SourceToken> repToTokenWithArgs = new HashMap<String, SourceToken>();
                                        HeuristicFindAttrs heuristicFindAttrs = new HeuristicFindAttrs(HeuristicFindAttrs.WHITIN_ANY, HeuristicFindAttrs.IN_ASSIGN, "", definition.module.getName(), null, repToTokenWithArgs, nature);
                                        heuristicFindAttrs.visitFunctionDef(functionDef);
                                        List<IToken> tokens = heuristicFindAttrs.getTokens();
                                        List<IDefinition> newDefs = new ArrayList<>();
                                        for (IToken iToken : tokens) {
                                            if (info.getName().equals(iToken.getRepresentation())) {
                                                newDefs.add(new Definition(iToken, definition.scope, definition.module));
                                            }
                                        }
                                        definitions = newDefs.toArray(new IDefinition[newDefs.size()]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            PyRefactoringFindDefinition.getAsPointers(pointers, definitions);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return true;
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) IModule(org.python.pydev.core.IModule) HashMap(java.util.HashMap) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) PyRefactoringFindDefinition(org.python.pydev.ast.refactoring.PyRefactoringFindDefinition) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) IDefinition(org.python.pydev.core.IDefinition) IToken(org.python.pydev.core.IToken) File(java.io.File) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer) Location(org.python.pydev.shared_core.structure.Location) HeuristicFindAttrs(org.python.pydev.ast.codecompletion.revisited.visitors.HeuristicFindAttrs)

Example 2 with SourceToken

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

the class ImportChecker method visitImportToken.

/**
 * This is so that we can use it without actually being in some visit.
 */
public static ImportInfo visitImportToken(boolean reportUndefinedImports, IToken token, String moduleName, IPythonNature nature, AbstractScopeAnalyzerVisitor visitor, ICompletionCache completionCache) {
    // try to find it as a relative import
    boolean wasResolved = false;
    Tuple3<IModule, String, IToken> modTok = null;
    String checkForToken = "";
    if (token instanceof SourceToken) {
        ICodeCompletionASTManager astManager = nature.getAstManager();
        ICompletionState state = CompletionStateFactory.getEmptyCompletionState(token.getRepresentation(), nature, completionCache);
        try {
            modTok = astManager.findOnImportedMods(new TokensList(token), state, moduleName, visitor.current);
        } catch (CompletionRecursionException e1) {
            // unable to resolve it
            modTok = null;
        }
        if (modTok != null && modTok.o1 != null) {
            checkForToken = modTok.o2;
            if (modTok.o2.length() == 0) {
                wasResolved = true;
            } else {
                try {
                    checkForToken = AbstractASTManager.getTokToSearchInOtherModule(modTok);
                    if (astManager.getRepInModule(modTok.o1, checkForToken, nature) != null) {
                        wasResolved = true;
                    }
                } catch (CompletionRecursionException e) {
                // not resolved...
                }
            }
        }
        if (!wasResolved && moduleName != null && moduleName.length() > 0) {
            if (moduleName.equals(token.getRepresentation()) || moduleName.equals(token.getRepresentation() + ".__init__")) {
                wasResolved = true;
                modTok = new Tuple3<IModule, String, IToken>(visitor.current, "", token);
                checkForToken = modTok.o2;
            }
        }
        // if it got here, it was not resolved
        if (!wasResolved && reportUndefinedImports) {
            visitor.onAddUnresolvedImport(token);
        }
    }
    // might still return a modTok, even if the token we were looking for was not found.
    if (modTok != null) {
        return new ImportInfo(modTok.o1, checkForToken, modTok.o3, wasResolved);
    } else {
        return new ImportInfo(null, null, null, wasResolved);
    }
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IModule(org.python.pydev.core.IModule) IToken(org.python.pydev.core.IToken) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) ICompletionState(org.python.pydev.core.ICompletionState) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) TokensList(org.python.pydev.core.TokensList)

Example 3 with SourceToken

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

the class MessagesManager method addUnusedMessage.

/**
 * adds a message for something that was not used
 *
 * @param node the node representing the scope being closed when adding the
 *             unused message
 */
public void addUnusedMessage(SimpleNode node, Found f) {
    List<GenAndTok> all = f.getAll();
    int len = all.size();
    for (int i = 0; i < len; i++) {
        GenAndTok g = all.get(i);
        if (g.generator instanceof SourceToken) {
            SimpleNode ast = ((SourceToken) g.generator).getAst();
            // it can be an unused import
            boolean isFromImport = ast instanceof ImportFrom;
            if (isFromImport || ast instanceof Import) {
                if (isFromImport && AbstractVisitor.isWildImport((ImportFrom) ast)) {
                    addMessage(IAnalysisPreferences.TYPE_UNUSED_WILD_IMPORT, g.generator, g.tok);
                } else if (!(g.generator instanceof ImportPartSourceToken)) {
                    addMessage(IAnalysisPreferences.TYPE_UNUSED_IMPORT, g.generator, g.tok);
                }
                // finish it...
                continue;
            }
        }
        // we have to check if this is a name we should ignore
        if (startsWithNamesToIgnore(g)) {
            int type = IAnalysisPreferences.TYPE_UNUSED_VARIABLE;
            if (g.tok instanceof SourceToken) {
                SourceToken t = (SourceToken) g.tok;
                SimpleNode ast = t.getAst();
                if (ast instanceof NameTok) {
                    NameTok n = (NameTok) ast;
                    if (n.ctx == NameTok.KwArg || n.ctx == NameTok.VarArg || n.ctx == NameTok.KeywordName) {
                        type = IAnalysisPreferences.TYPE_UNUSED_PARAMETER;
                    }
                } else if (ast instanceof Name) {
                    Name n = (Name) ast;
                    if (n.ctx == Name.Param || n.ctx == Name.KwOnlyParam) {
                        type = IAnalysisPreferences.TYPE_UNUSED_PARAMETER;
                    }
                }
            }
            boolean addMessage = true;
            if (type == IAnalysisPreferences.TYPE_UNUSED_PARAMETER) {
                if (node instanceof FunctionDef) {
                    addMessage = false;
                    FunctionDef def = (FunctionDef) node;
                    for (stmtType b : def.body) {
                        if (b instanceof Pass) {
                            continue;
                        }
                        if (b instanceof Expr) {
                            Expr expr = (Expr) b;
                            if (expr.value instanceof Str) {
                                continue;
                            }
                        }
                        addMessage = true;
                        break;
                    }
                }
            }
            if (addMessage) {
                addMessage(type, g.generator, g.tok);
            }
        }
    }
}
Also used : Import(org.python.pydev.parser.jython.ast.Import) ImportPartSourceToken(org.python.pydev.ast.codecompletion.revisited.visitors.AbstractVisitor.ImportPartSourceToken) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Name(org.python.pydev.parser.jython.ast.Name) Str(org.python.pydev.parser.jython.ast.Str) Pass(org.python.pydev.parser.jython.ast.Pass) Expr(org.python.pydev.parser.jython.ast.Expr) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) ImportPartSourceToken(org.python.pydev.ast.codecompletion.revisited.visitors.AbstractVisitor.ImportPartSourceToken) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 4 with SourceToken

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

the class NoSelfChecker method creteMessagesForStack.

/**
 * @param stack
 * @param shouldBeDefined
 */
private void creteMessagesForStack(FastStack<HashMap<String, Tuple<Expected, FunctionDef>>> stack) {
    HashMap<String, Tuple<Expected, FunctionDef>> noDefinedItems = stack.pop();
    for (Map.Entry<String, Tuple<Expected, FunctionDef>> entry : noDefinedItems.entrySet()) {
        Expected expected = entry.getValue().o1;
        if (!expected.expected.equals(expected.received)) {
            SourceToken token = AbstractVisitor.makeToken(entry.getValue().o2, moduleName, null);
            messagesManager.addMessage(IAnalysisPreferences.TYPE_NO_SELF, token, new Object[] { token, entry.getValue().o1.expected });
        }
    }
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) Tuple(org.python.pydev.shared_core.structure.Tuple) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)

Example 5 with SourceToken

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

the class AbstractMessage method getStartCol.

/**
 * @return the starting column for the given token (1-based)
 */
public static int getStartCol(IToken generator, IDocument doc, String shortMessage, boolean returnAsName) {
    // not import...
    if (!generator.isImport()) {
        return generator.getColDefinition();
    }
    // ok, it is an import... (can only be a source token)
    SourceToken s = (SourceToken) generator;
    SimpleNode ast = s.getAst();
    if (ast instanceof ImportFrom) {
        ImportFrom i = (ImportFrom) ast;
        // if it is a wild import, it starts on the module name
        if (AbstractVisitor.isWildImport(i)) {
            return i.module.beginColumn;
        } else {
            // no wild import, let's check the 'as name'
            return getNameForRepresentation(i, shortMessage, returnAsName).beginColumn;
        }
    } else if (ast instanceof Import) {
        return getNameForRepresentation(ast, shortMessage, returnAsName).beginColumn;
    } else {
        throw new RuntimeException("It is not an import");
    }
}
Also used : Import(org.python.pydev.parser.jython.ast.Import) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

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