Search in sources :

Example 1 with ImportPartSourceToken

use of org.python.pydev.ast.codecompletion.revisited.visitors.AbstractVisitor.ImportPartSourceToken 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)

Aggregations

SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)1 ImportPartSourceToken (org.python.pydev.ast.codecompletion.revisited.visitors.AbstractVisitor.ImportPartSourceToken)1 SimpleNode (org.python.pydev.parser.jython.SimpleNode)1 Expr (org.python.pydev.parser.jython.ast.Expr)1 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)1 Import (org.python.pydev.parser.jython.ast.Import)1 ImportFrom (org.python.pydev.parser.jython.ast.ImportFrom)1 Name (org.python.pydev.parser.jython.ast.Name)1 NameTok (org.python.pydev.parser.jython.ast.NameTok)1 Pass (org.python.pydev.parser.jython.ast.Pass)1 Str (org.python.pydev.parser.jython.ast.Str)1 org.python.pydev.parser.jython.ast.stmtType (org.python.pydev.parser.jython.ast.stmtType)1