Search in sources :

Example 1 with IJavaScriptUnit

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

the class JSDTHyperlinkDetector method createHyperlink.

private IHyperlink createHyperlink(IJsTranslation jsTranslation, IJavaScriptElement element, IRegion region, IDocument document) {
    IHyperlink link = null;
    if (region != null) {
        // open local variable in the JSP file...
        if (element instanceof ISourceReference) {
            IFile file = null;
            IPath outsidePath = null;
            int jspOffset = 0;
            IStructuredModel sModel = null;
            // try to locate the file in the workspace
            try {
                sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
                if (sModel != null) {
                    // URIResolver resolver = sModel.getResolver();
                    // if (resolver != null) {
                    // String uriString = resolver.getFileBaseLocation();
                    String uriString = sModel.getBaseLocation();
                    file = getFile(uriString);
                // }
                }
            } finally {
                if (sModel != null) {
                    sModel.releaseFromRead();
                }
            }
            // get Java range, translate coordinate to JSP
            try {
                ISourceRange range = null;
                if (jsTranslation != null) {
                    // link to local variable definitions
                    if (element instanceof ILocalVariable) {
                        range = ((ILocalVariable) element).getNameRange();
                        IJavaScriptElement unit = ((ILocalVariable) element).getParent();
                        IJavaScriptUnit myUnit = jsTranslation.getCompilationUnit();
                        while (!(unit instanceof IJavaScriptUnit || unit instanceof IClassFile || unit == null)) {
                            unit = ((JavaElement) unit).getParent();
                        }
                        if (unit instanceof IJavaScriptUnit) {
                            IJavaScriptUnit cu = (IJavaScriptUnit) unit;
                            if (cu != myUnit) {
                                file = getFile(cu.getPath().toString());
                                if (file == null) {
                                    outsidePath = cu.getPath();
                                }
                            }
                        } else if (unit instanceof IClassFile) {
                            IClassFile cu = (IClassFile) unit;
                            if (cu != myUnit) {
                                file = getFile(cu.getPath().toString());
                                if (file == null) {
                                    outsidePath = cu.getPath();
                                }
                            }
                        }
                    } else // linking to fields of the same compilation unit
                    if (element.getElementType() == IJavaScriptElement.FIELD) {
                        Object cu = ((IField) element).getJavaScriptUnit();
                        if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                        }
                    } else // linking to methods of the same compilation unit
                    if (element.getElementType() == IJavaScriptElement.METHOD) {
                        Object cu = ((IFunction) element).getJavaScriptUnit();
                        if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                        }
                    }
                }
                if (range != null && file != null) {
                    jspOffset = range.getOffset();
                    if (jspOffset >= 0) {
                        link = new WorkspaceFileHyperlink(region, file, new Region(jspOffset, range.getLength()));
                    }
                } else if (range != null && outsidePath != null) {
                    jspOffset = range.getOffset();
                    if (jspOffset >= 0) {
                        link = new ExternalFileHyperlink(region, outsidePath.toFile());
                    }
                }
            } catch (JavaScriptModelException jme) {
                Logger.log(Logger.WARNING_DEBUG, jme.getMessage(), jme);
            }
        }
        if (link == null) {
            link = new JSDTHyperlink(region, element);
        }
    }
    return link;
}
Also used : IFile(org.eclipse.core.resources.IFile) IClassFile(org.eclipse.wst.jsdt.core.IClassFile) IPath(org.eclipse.core.runtime.IPath) JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) IFunction(org.eclipse.wst.jsdt.core.IFunction) ILocalVariable(org.eclipse.wst.jsdt.core.ILocalVariable) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit) ISourceReference(org.eclipse.wst.jsdt.core.ISourceReference) ISourceRange(org.eclipse.wst.jsdt.core.ISourceRange)

Example 2 with IJavaScriptUnit

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

the class JSDTContentAssistantProcessor method computeCompletionProposals.

/**
 * Returns a list of completion proposals based on the specified location
 * within the document that corresponds to the current cursor position
 * within the text viewer.
 *
 * @param viewer
 *            the viewer whose document is used to compute the proposals
 * @param documentPosition
 *            an offset within the document for which completions should be
 *            computed
 * @return an array of completion proposals or <code>null</code> if no
 *         proposals are possible
 */
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int pos) {
    initialize(pos);
    JSDTProposalCollector collector = null;
    IDOMModel xmlModel = null;
    try {
        fViewer = viewer;
        xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(fViewer.getDocument());
        IDOMDocument xmlDoc = xmlModel.getDocument();
        JsTranslationAdapterFactory.setupAdapterFactory(xmlModel);
        JsTranslationAdapter translationAdapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
        if (translationAdapter != null) {
            IJsTranslation translation = translationAdapter.getJsTranslation(true);
            fJavaPosition = translation.getJavaScriptOffset(getDocumentPosition());
            try {
                IJavaScriptUnit cu = translation.getCompilationUnit();
                // or without a valid position
                if (cu == null || -1 == fJavaPosition) {
                    return new ICompletionProposal[0];
                }
                collector = getProposalCollector();
                synchronized (cu) {
                    cu.codeComplete(fJavaPosition, collector, null);
                }
            } catch (CoreException coreEx) {
                // a possible Java Model Exception due to not being a Web
                // (Java) Project
                coreEx.printStackTrace();
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
    // throw out exceptions on code assist.
    } finally {
        if (xmlModel != null) {
            xmlModel.releaseFromRead();
        }
    }
    ICompletionProposal[] results = new ICompletionProposal[0];
    if (collector != null) {
        results = collector.getJSPCompletionProposals();
        if (results == null || results.length < 1) {
            fErrorMessage = JsUIMessages.Java_Content_Assist_is_not_UI_;
        }
    }
    return results;
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) CoreException(org.eclipse.core.runtime.CoreException) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit) CoreException(org.eclipse.core.runtime.CoreException)

Example 3 with IJavaScriptUnit

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

the class AddJavaDocStubAction method run.

public void run(IAction action) {
    IJavaScriptElement[] elements = JsElementActionProxy.getJsElementsFromSelection(selection);
    if (elements == null || elements.length < 1) {
        return;
    }
    IJavaScriptElement parent = elements[0].getParent();
    /* find the cu */
    while (parent != null && !(parent instanceof IJavaScriptUnit)) {
    }
    if (parent != null) {
        ArrayList members = new ArrayList();
        for (int i = 0; i < elements.length; i++) {
            if (elements[i] instanceof IMember) {
                members.add(elements[i]);
            }
        }
        JsJfaceNode[] node = SimpleJSDTActionProxy.getJsJfaceNodesFromSelection(selection);
        /* only should be one node */
        run((IJavaScriptUnit) parent, (IMember[]) members.toArray(new IMember[members.size()]), node[0]);
    }
}
Also used : IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) ArrayList(java.util.ArrayList) JsJfaceNode(org.eclipse.wst.jsdt.web.ui.views.contentoutline.JsJfaceNode) IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit) IMember(org.eclipse.wst.jsdt.core.IMember)

Example 4 with IJavaScriptUnit

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

the class JsTranslation method reconcileCompilationUnit.

/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.web.core.internal.java.IJsTranslation#reconcileCompilationUnit()
	 */
public void reconcileCompilationUnit() {
    // if(true) return;
    IJavaScriptUnit cu = getCompilationUnit();
    if (fCompilationUnit == null) {
        return;
    }
    if (cu != null) {
        try {
            synchronized (fLock) {
                // clear out old validation messages
                WorkingCopyOwner workingCopyOwner = getWorkingCopyOwner();
                JsProblemRequestor problemRequestor = (JsProblemRequestor) workingCopyOwner.getProblemRequestor(cu.getWorkingCopy(getProgressMonitor()));
                if (problemRequestor != null && problemRequestor.getCollectedProblems() != null)
                    problemRequestor.getCollectedProblems().clear();
                cu.reconcile(IJavaScriptUnit.NO_AST, true, true, getWorkingCopyOwner(), getProgressMonitor());
            }
        } catch (JavaScriptModelException e) {
            Logger.logException(e);
        }
    }
}
Also used : WorkingCopyOwner(org.eclipse.wst.jsdt.core.WorkingCopyOwner) JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit)

Example 5 with IJavaScriptUnit

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

the class JsTranslation method getElementsFromJsRange.

/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.web.core.internal.java.IJsTranslation#getElementsFromJsRange(int, int)
	 */
public IJavaScriptElement[] getElementsFromJsRange(int javaPositionStart, int javaPositionEnd) {
    IJavaScriptElement[] EMTPY_RESULT_SET = new IJavaScriptElement[0];
    IJavaScriptElement[] result = EMTPY_RESULT_SET;
    try {
        IJavaScriptUnit cu = getCompilationUnit();
        if (cu != null) {
            synchronized (fLock) {
                int cuDocLength = cu.getBuffer().getLength();
                int javaLength = javaPositionEnd - javaPositionStart;
                if (cuDocLength > 0 && javaPositionStart >= 0 && javaLength >= 0 && javaPositionEnd <= cuDocLength) {
                    result = cu.codeSelect(javaPositionStart, javaLength, getWorkingCopyOwner());
                }
            }
        }
        if (result == null || result.length == 0) {
            return EMTPY_RESULT_SET;
        }
    } catch (JavaScriptModelException x) {
        Logger.logException(x);
    }
    return result;
}
Also used : JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit)

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