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);
}
}
}
}
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);
}
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;
}
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;
}
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;
}
Aggregations