Search in sources :

Example 1 with ASTParser

use of org.eclipse.wst.jsdt.core.dom.ASTParser 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 2 with ASTParser

use of org.eclipse.wst.jsdt.core.dom.ASTParser 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)2 ASTNode (org.eclipse.wst.jsdt.core.dom.ASTNode)2 ASTParser (org.eclipse.wst.jsdt.core.dom.ASTParser)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 JavaScriptUnit (org.eclipse.wst.jsdt.core.dom.JavaScriptUnit)1