Search in sources :

Example 1 with ILocalVariable

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

Aggregations

IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 IRegion (org.eclipse.jface.text.IRegion)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 Region (org.eclipse.jface.text.Region)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 IClassFile (org.eclipse.wst.jsdt.core.IClassFile)1 IFunction (org.eclipse.wst.jsdt.core.IFunction)1 IJavaScriptElement (org.eclipse.wst.jsdt.core.IJavaScriptElement)1 IJavaScriptUnit (org.eclipse.wst.jsdt.core.IJavaScriptUnit)1 ILocalVariable (org.eclipse.wst.jsdt.core.ILocalVariable)1 ISourceRange (org.eclipse.wst.jsdt.core.ISourceRange)1 ISourceReference (org.eclipse.wst.jsdt.core.ISourceReference)1 JavaScriptModelException (org.eclipse.wst.jsdt.core.JavaScriptModelException)1 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)1