Search in sources :

Example 1 with ASTNode

use of org.eclipse.wst.jsdt.core.dom.ASTNode in project webtools.sourceediting by eclipse.

the class RunCodeHandler method getObjectLiteralField.

static ObjectLiteralField getObjectLiteralField(String source) {
    String js = "x.prototype = {" + source + "}";
    JavaScriptUnit jsu = getAST(js);
    ExpressionStatement stmt = (ExpressionStatement) jsu.statements().get(0);
    ObjectLiteral lit = (ObjectLiteral) ((Assignment) stmt.getExpression()).getRightHandSide();
    ObjectLiteralField field = (ObjectLiteralField) lit.fields().get(0);
    ASTNode node = ASTNode.copySubtree(jsu.getAST(), field);
    return (ObjectLiteralField) node;
}
Also used : IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit) JavaScriptUnit(org.eclipse.wst.jsdt.core.dom.JavaScriptUnit) ObjectLiteral(org.eclipse.wst.jsdt.core.dom.ObjectLiteral) ExpressionStatement(org.eclipse.wst.jsdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.wst.jsdt.core.dom.ASTNode) ObjectLiteralField(org.eclipse.wst.jsdt.core.dom.ObjectLiteralField)

Example 2 with ASTNode

use of org.eclipse.wst.jsdt.core.dom.ASTNode in project webtools.sourceediting by eclipse.

the class RunCodeHandler method doExecute.

protected void doExecute(IFile jsFile) {
    IProject project = jsFile.getProject();
    // String namespace = CodeGenUtil.getNameSpace(project);
    // final String className = IPCCommandUtil.getPortletHelperClassName(jsFile, namespace);
    final IJavaScriptUnit jsUnit = (IJavaScriptUnit) JavaScriptCore.create(jsFile);
    if (jsUnit == null)
        return;
    IProgressMonitor monitor = null;
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    // //////////////////////////////////////////////////////////////////////////////
    // this part may not be of interest for what you're trying to do
    // IType t = null;
    // try{
    // for (org.eclipse.wst.jsdt.core.IFunction function : jsUnit.getFunctions()) {
    // // functions declared in the file outside of any types
    // org.eclipse.wst.jsdt.core.ISourceRange sourceRange = function.getSourceRange();
    // String source = jsUnit.getBuffer().getText(sourceRange.getOffset(), sourceRange.getLength());
    // }
    // 
    // // start with the types in the file
    // IType[] types = jsUnit.getTypes();
    // 
    // for (IType type: types) {
    // //				if (type.getDisplayName().equals(className)) {
    // t = type;
    // for (IFunction function : type.getFunctions()) {
    // // functions declared in a type
    // if(function.getDisplayName().equals("sayHello") && !function.isConstructor()) {
    // ISourceRange sourceRange = function.getSourceRange();
    // String source = jsUnit.getBuffer().getText(sourceRange.getOffset(), sourceRange.getLength());
    // }
    // }
    // //				}
    // }
    // }catch (JavaScriptModelException e) {
    // // TODO: handle exception
    // //;
    // }
    // It would be more accurate to walk the AST of the file instead of searching just the top-level children:
    // SourceType st = null;
    // if(t instanceof SourceType)
    // st = (SourceType) t;
    // try {
    // if(st!=null)
    // st.createMethod("function hello(args){}", null, false, null);
    // 
    // } catch (JavaScriptModelException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    ASTParser parser = org.eclipse.wst.jsdt.core.dom.ASTParser.newParser(AST.JLS3);
    parser.setSource(jsUnit);
    ASTNode ast = parser.createAST(null);
    ASTVisitor visitor = new MyNodeVisitor(jsUnit, /*className*/
    null, ast);
    ast.accept(visitor);
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ASTNode(org.eclipse.wst.jsdt.core.dom.ASTNode) IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit) ASTParser(org.eclipse.wst.jsdt.core.dom.ASTParser) IProject(org.eclipse.core.resources.IProject) ASTVisitor(org.eclipse.wst.jsdt.core.dom.ASTVisitor)

Example 3 with ASTNode

use of org.eclipse.wst.jsdt.core.dom.ASTNode in project webtools.sourceediting by eclipse.

the class RunCodeHandler method getAST.

static JavaScriptUnit getAST(String source) {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(source.toCharArray());
    ASTNode ast = parser.createAST(null);
    return (JavaScriptUnit) ast;
}
Also used : IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit) JavaScriptUnit(org.eclipse.wst.jsdt.core.dom.JavaScriptUnit) ASTNode(org.eclipse.wst.jsdt.core.dom.ASTNode) ASTParser(org.eclipse.wst.jsdt.core.dom.ASTParser)

Aggregations

IJavaScriptUnit (org.eclipse.wst.jsdt.core.IJavaScriptUnit)3 ASTNode (org.eclipse.wst.jsdt.core.dom.ASTNode)3 ASTParser (org.eclipse.wst.jsdt.core.dom.ASTParser)2 JavaScriptUnit (org.eclipse.wst.jsdt.core.dom.JavaScriptUnit)2 IProject (org.eclipse.core.resources.IProject)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ASTVisitor (org.eclipse.wst.jsdt.core.dom.ASTVisitor)1 ExpressionStatement (org.eclipse.wst.jsdt.core.dom.ExpressionStatement)1 ObjectLiteral (org.eclipse.wst.jsdt.core.dom.ObjectLiteral)1 ObjectLiteralField (org.eclipse.wst.jsdt.core.dom.ObjectLiteralField)1