Search in sources :

Example 6 with IJavaScriptUnit

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

the class JsTranslation method createCompilationUnit.

/**
 * Originally from ReconcileStepForJava. Creates an IJavaScriptUnit from
 * the contents of the JSP document.
 *
 * @return an IJavaScriptUnit from the contents of the JSP document
 */
private IJavaScriptUnit createCompilationUnit() throws JavaScriptModelException {
    IPackageFragmentRoot root = getDocScope(true);
    // $NON-NLS-1$
    IJavaScriptUnit cu = root.getPackageFragment("").getJavaScriptUnit(getMangledName() + JsDataTypes.BASE_FILE_EXTENSION).getWorkingCopy(getWorkingCopyOwner(), getProgressMonitor());
    IBuffer buffer;
    try {
        buffer = cu.getBuffer();
    } catch (JavaScriptModelException e) {
        e.printStackTrace();
        buffer = null;
    }
    if (buffer != null) {
        getTranslator().setBuffer(buffer);
    }
    return cu;
}
Also used : JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit) IBuffer(org.eclipse.wst.jsdt.core.IBuffer) IPackageFragmentRoot(org.eclipse.wst.jsdt.core.IPackageFragmentRoot)

Example 7 with IJavaScriptUnit

use of org.eclipse.wst.jsdt.core.IJavaScriptUnit 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)

Aggregations

IJavaScriptUnit (org.eclipse.wst.jsdt.core.IJavaScriptUnit)7 JavaScriptModelException (org.eclipse.wst.jsdt.core.JavaScriptModelException)4 IJavaScriptElement (org.eclipse.wst.jsdt.core.IJavaScriptElement)3 ArrayList (java.util.ArrayList)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IRegion (org.eclipse.jface.text.IRegion)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 Region (org.eclipse.jface.text.Region)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 IBuffer (org.eclipse.wst.jsdt.core.IBuffer)1 IClassFile (org.eclipse.wst.jsdt.core.IClassFile)1 IFunction (org.eclipse.wst.jsdt.core.IFunction)1 ILocalVariable (org.eclipse.wst.jsdt.core.ILocalVariable)1 IMember (org.eclipse.wst.jsdt.core.IMember)1