Search in sources :

Example 1 with Str

use of org.python.pydev.parser.jython.ast.Str 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 2 with Str

use of org.python.pydev.parser.jython.ast.Str in project Pydev by fabioz.

the class NodeUtils method getLineEnd.

public static int getLineEnd(SimpleNode v) {
    if (v instanceof Expr) {
        Expr expr = (Expr) v;
        v = expr.value;
    }
    if (v instanceof ImportFrom) {
        ImportFrom f = (ImportFrom) v;
        FindLastLineVisitor findLastLineVisitor = new FindLastLineVisitor();
        try {
            f.accept(findLastLineVisitor);
            SimpleNode lastNode = findLastLineVisitor.getLastNode();
            ISpecialStr lastSpecialStr = findLastLineVisitor.getLastSpecialStr();
            if (lastSpecialStr != null && lastSpecialStr.toString().equals(")")) {
                // it was an from xxx import (euheon, utehon)
                return lastSpecialStr.getBeginLine();
            } else {
                return lastNode.beginLine;
            }
        } catch (Exception e) {
            Log.log(e);
        }
    }
    if (v instanceof Import) {
        Import f = (Import) v;
        FindLastLineVisitor findLastLineVisitor = new FindLastLineVisitor();
        try {
            f.accept(findLastLineVisitor);
            SimpleNode lastNode = findLastLineVisitor.getLastNode();
            return lastNode.beginLine;
        } catch (Exception e) {
            Log.log(e);
        }
    }
    if (v instanceof Str) {
        String s = ((Str) v).s;
        int found = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '\n') {
                found += 1;
            }
        }
        return getLineDefinition(v) + found;
    }
    return getLineDefinition(v);
}
Also used : Str(org.python.pydev.parser.jython.ast.Str) ISpecialStr(org.python.pydev.parser.jython.ISpecialStr) Import(org.python.pydev.parser.jython.ast.Import) Expr(org.python.pydev.parser.jython.ast.Expr) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) ISpecialStr(org.python.pydev.parser.jython.ISpecialStr) SyntaxErrorException(org.python.pydev.core.docutils.SyntaxErrorException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode)

Example 3 with Str

use of org.python.pydev.parser.jython.ast.Str in project Pydev by fabioz.

the class NodeUtils method getRepresentationString.

/**
 * @param node this is the node from whom we want to get the representation
 * @return A suitable String representation for some node.
 */
public static String getRepresentationString(SimpleNode node, boolean useTypeRepr) {
    if (node instanceof NameTok) {
        NameTok tok = (NameTok) node;
        return tok.id;
    }
    if (node instanceof Name) {
        Name name = (Name) node;
        return name.id;
    }
    if (node instanceof aliasType) {
        aliasType type = (aliasType) node;
        return ((NameTok) type.name).id;
    }
    if (node instanceof Attribute) {
        Attribute attribute = (Attribute) node;
        return discoverRep(attribute.attr);
    }
    if (node instanceof keywordType) {
        keywordType type = (keywordType) node;
        return discoverRep(type.arg);
    }
    if (node instanceof ClassDef) {
        ClassDef def = (ClassDef) node;
        return ((NameTok) def.name).id;
    }
    if (node instanceof FunctionDef) {
        FunctionDef def = (FunctionDef) node;
        return ((NameTok) def.name).id;
    }
    if (node instanceof Call) {
        Call call = ((Call) node);
        return getRepresentationString(call.func, useTypeRepr);
    }
    if (node instanceof org.python.pydev.parser.jython.ast.List || node instanceof ListComp) {
        String val = "[]";
        if (useTypeRepr) {
            val = getBuiltinType(val);
        }
        return val;
    }
    if (node instanceof org.python.pydev.parser.jython.ast.Dict || node instanceof org.python.pydev.parser.jython.ast.DictComp) {
        String val = "{}";
        if (useTypeRepr) {
            val = getBuiltinType(val);
        }
        return val;
    }
    if (node instanceof BinOp) {
        BinOp binOp = (BinOp) node;
        if (binOp.left instanceof Str && binOp.op == BinOp.Mod) {
            node = binOp.left;
        // Just change the node... the check below will work with the Str already.
        }
    }
    if (node instanceof Str) {
        String val;
        if (useTypeRepr) {
            val = getBuiltinType("''");
        } else {
            val = "'" + ((Str) node).s + "'";
        }
        return val;
    }
    if (node instanceof Tuple) {
        StringBuffer buf = new StringBuffer();
        Tuple t = (Tuple) node;
        for (exprType e : t.elts) {
            buf.append(getRepresentationString(e, useTypeRepr));
            buf.append(", ");
        }
        if (t.elts.length > 0) {
            int l = buf.length();
            buf.deleteCharAt(l - 1);
            buf.deleteCharAt(l - 2);
        }
        String val = "(" + buf + ")";
        if (useTypeRepr) {
            val = getBuiltinType(val);
        }
        return val;
    }
    if (node instanceof Num) {
        String val = ((Num) node).n.toString();
        if (useTypeRepr) {
            val = getBuiltinType(val);
        }
        return val;
    }
    if (node instanceof Import) {
        aliasType[] names = ((Import) node).names;
        for (aliasType n : names) {
            if (n.asname != null) {
                return ((NameTok) n.asname).id;
            }
            return ((NameTok) n.name).id;
        }
    }
    if (node instanceof commentType) {
        commentType type = (commentType) node;
        return type.id;
    }
    if (node instanceof excepthandlerType) {
        excepthandlerType type = (excepthandlerType) node;
        return type.name.toString();
    }
    return null;
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) Import(org.python.pydev.parser.jython.ast.Import) Attribute(org.python.pydev.parser.jython.ast.Attribute) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) DictComp(org.python.pydev.parser.jython.ast.DictComp) Name(org.python.pydev.parser.jython.ast.Name) Str(org.python.pydev.parser.jython.ast.Str) ISpecialStr(org.python.pydev.parser.jython.ISpecialStr) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) org.python.pydev.parser.jython.ast.commentType(org.python.pydev.parser.jython.ast.commentType) org.python.pydev.parser.jython.ast.keywordType(org.python.pydev.parser.jython.ast.keywordType) Call(org.python.pydev.parser.jython.ast.Call) Dict(org.python.pydev.parser.jython.ast.Dict) Num(org.python.pydev.parser.jython.ast.Num) ListComp(org.python.pydev.parser.jython.ast.ListComp) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) org.python.pydev.parser.jython.ast.aliasType(org.python.pydev.parser.jython.ast.aliasType) org.python.pydev.parser.jython.ast.excepthandlerType(org.python.pydev.parser.jython.ast.excepthandlerType) BinOp(org.python.pydev.parser.jython.ast.BinOp) NameTok(org.python.pydev.parser.jython.ast.NameTok) Tuple(org.python.pydev.parser.jython.ast.Tuple)

Example 4 with Str

use of org.python.pydev.parser.jython.ast.Str in project Pydev by fabioz.

the class NodeUtils method printAst.

/**
 * Copied from {@link PyTextHover} when that class was deprecated.
 */
public static String printAst(IPyEdit edit, SimpleNode astToPrint) {
    String str = null;
    if (astToPrint != null) {
        IIndentPrefs indentPrefs;
        if (edit != null) {
            indentPrefs = edit.getIndentPrefs();
        } else {
            indentPrefs = DefaultIndentPrefs.get(null);
        }
        Str docStr = getNodeDocStringNode(astToPrint);
        if (docStr != null) {
            docStr.s = PyStringUtils.fixWhitespaceColumnsToLeftFromDocstring(docStr.s, indentPrefs.getIndentationString());
        }
        PrettyPrinterPrefsV2 prefsV2 = PrettyPrinterV2.createDefaultPrefs(edit, indentPrefs, LINE_DELIM);
        PrettyPrinterV2 prettyPrinterV2 = new PrettyPrinterV2(prefsV2);
        try {
            str = prettyPrinterV2.print(astToPrint);
        } catch (IOException e) {
            Log.log(e);
        }
    }
    return str;
}
Also used : Str(org.python.pydev.parser.jython.ast.Str) ISpecialStr(org.python.pydev.parser.jython.ISpecialStr) PrettyPrinterPrefsV2(org.python.pydev.parser.prettyprinterv2.PrettyPrinterPrefsV2) IIndentPrefs(org.python.pydev.core.IIndentPrefs) PrettyPrinterV2(org.python.pydev.parser.prettyprinterv2.PrettyPrinterV2) IOException(java.io.IOException)

Example 5 with Str

use of org.python.pydev.parser.jython.ast.Str in project Pydev by fabioz.

the class NodeUtils method getNodeDocStringNode.

public static Str getNodeDocStringNode(SimpleNode node) {
    Str s = null;
    stmtType[] body = null;
    if (node instanceof FunctionDef) {
        FunctionDef def = (FunctionDef) node;
        body = def.body;
    } else if (node instanceof ClassDef) {
        ClassDef def = (ClassDef) node;
        body = def.body;
    }
    if (body != null && body.length > 0) {
        if (body[0] instanceof Expr) {
            Expr e = (Expr) body[0];
            if (e.value instanceof Str) {
                s = (Str) e.value;
            }
        }
    }
    return s;
}
Also used : Str(org.python.pydev.parser.jython.ast.Str) ISpecialStr(org.python.pydev.parser.jython.ISpecialStr) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) Expr(org.python.pydev.parser.jython.ast.Expr) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef)

Aggregations

Str (org.python.pydev.parser.jython.ast.Str)25 SimpleNode (org.python.pydev.parser.jython.SimpleNode)11 ISpecialStr (org.python.pydev.parser.jython.ISpecialStr)10 ArrayList (java.util.ArrayList)8 Attribute (org.python.pydev.parser.jython.ast.Attribute)8 Name (org.python.pydev.parser.jython.ast.Name)8 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)8 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)6 Expr (org.python.pydev.parser.jython.ast.Expr)6 Import (org.python.pydev.parser.jython.ast.Import)6 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)6 SequencialASTIteratorVisitor (org.python.pydev.parser.visitors.scope.SequencialASTIteratorVisitor)6 Call (org.python.pydev.parser.jython.ast.Call)5 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)5 BinOp (org.python.pydev.parser.jython.ast.BinOp)4 For (org.python.pydev.parser.jython.ast.For)4 If (org.python.pydev.parser.jython.ast.If)4 ImportFrom (org.python.pydev.parser.jython.ast.ImportFrom)4 Module (org.python.pydev.parser.jython.ast.Module)4 NameTok (org.python.pydev.parser.jython.ast.NameTok)4