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