use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.
the class NodeUtils method getReturnTypeFromStaticTyping.
public static TypeInfo getReturnTypeFromStaticTyping(SimpleNode node) {
if (node instanceof FunctionDef) {
FunctionDef functionDef = (FunctionDef) node;
exprType returns = functionDef.returns;
if (returns != null) {
return new TypeInfo(NodeUtils.extractOptionalValueSubscript(returns));
}
}
return null;
}
use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.
the class NodeUtils method getNodeArgs.
/**
* @param node a function definition (if other will return an empty string)
* @return a string with the representation of the parameters of the function
*/
public static String getNodeArgs(SimpleNode node) {
if (node instanceof ClassDef) {
node = getClassDefInit((ClassDef) node);
}
if (node instanceof FunctionDef) {
FunctionDef f = (FunctionDef) node;
String startPar = "( ";
FastStringBuffer buffer = new FastStringBuffer(startPar, 40);
for (int i = 0; i < f.args.args.length; i++) {
if (buffer.length() > startPar.length()) {
buffer.append(", ");
}
buffer.append(getRepresentationString(f.args.args[i]));
}
buffer.append(" )");
return buffer.toString();
}
return "";
}
use of org.python.pydev.parser.jython.ast.FunctionDef 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;
}
use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.
the class PyParser25Test method testSuiteLineNumber.
public void testSuiteLineNumber() throws Exception {
setDefaultVersion(IPythonNature.GRAMMAR_PYTHON_VERSION_2_5);
String str = "" + "class Process:\n" + "\n" + " def Foo(self):\n" + " if a == 1:\n" + " pass\n" + " elif a == 1:\n" + " pass\n" + "\n" + "";
SimpleNode ast = parseLegalDocStr(str);
stmtType[] body = ((Module) ast).body;
assertEquals(1, body.length);
ClassDef classFound = (ClassDef) body[0];
body = classFound.body;
assertEquals(1, body.length);
FunctionDef func = (FunctionDef) body[0];
If ifFound = (If) func.body[0];
assertEquals(6, ifFound.orelse.beginLine);
}
use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.
the class PyParser39Test method testDecorator1.
public void testDecorator1() {
SimpleNode node = parseLegalDocStr("buttons = []\n" + "\n" + "@buttons[0].clicked().connect()\n" + "def eggs():\n" + " pass");
assertTrue(node instanceof Module);
Module m = (Module) node;
assertEquals(2, m.body.length);
assertTrue(m.body[0] instanceof Assign);
Assign a = (Assign) m.body[0];
assertTrue(a.value instanceof org.python.pydev.parser.jython.ast.List);
assertTrue(m.body[1] instanceof FunctionDef);
FunctionDef f = (FunctionDef) m.body[1];
assertTrue(f.decs != null);
assertEquals(1, f.decs.length);
decoratorsType dec = f.decs[0];
exprType it = dec.func;
boolean valid = false;
// i.e.; search for `buttons[0]` in decorator
while (true) {
if (it == null) {
break;
}
if (it instanceof Attribute) {
it = ((Attribute) it).value;
} else if (it instanceof Call) {
it = ((Call) it).func;
} else if (it instanceof Subscript) {
if ("buttons".equals(NodeUtils.getFullRepresentationString(it))) {
valid = true;
}
break;
} else {
break;
}
}
assertTrue(valid);
}
Aggregations