Search in sources :

Example 6 with JdtHyperlink

use of org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink in project xtext-eclipse by eclipse.

the class DomainmodelHyperlinkHelper method createHyperlinksByOffset.

@Override
public void createHyperlinksByOffset(XtextResource resource, int offset, IHyperlinkAcceptor acceptor) {
    super.createHyperlinksByOffset(resource, offset, acceptor);
    EObject eObject = getEObjectAtOffsetHelper().resolveElementAt(resource, offset);
    if (eObject instanceof Entity) {
        List<INode> nodes = NodeModelUtils.findNodesForFeature(eObject, DomainmodelPackage.Literals.ABSTRACT_ELEMENT__NAME);
        if (!nodes.isEmpty()) {
            INode node = nodes.get(0);
            if (node.getOffset() <= offset && node.getOffset() + node.getLength() > offset) {
                String qualifiedJavaName = qualifiedNameConverter.toString(qualifiedNameProvider.getFullyQualifiedName(eObject));
                if (resource.getResourceSet() instanceof XtextResourceSet) {
                    XtextResourceSet resourceSet = (XtextResourceSet) resource.getResourceSet();
                    Object uriContext = resourceSet.getClasspathURIContext();
                    if (uriContext instanceof IJavaProject) {
                        IJavaProject javaProject = (IJavaProject) uriContext;
                        try {
                            IType type = javaProject.findType(qualifiedJavaName);
                            if (type != null) {
                                JdtHyperlink hyperlink = jdtHyperlinkProvider.get();
                                hyperlink.setJavaElement(type);
                                hyperlink.setTypeLabel("Navigate to generated source code.");
                                hyperlink.setHyperlinkText("Go to type " + qualifiedJavaName);
                                hyperlink.setHyperlinkRegion((IRegion) new Region(node.getOffset(), node.getLength()));
                                acceptor.accept(hyperlink);
                            }
                        } catch (JavaModelException e) {
                            logger.error(e.getMessage(), e);
                        }
                    }
                }
            }
        }
    }
}
Also used : Entity(org.eclipse.xtext.example.domainmodel.domainmodel.Entity) INode(org.eclipse.xtext.nodemodel.INode) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) EObject(org.eclipse.emf.ecore.EObject) JdtHyperlink(org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink) IType(org.eclipse.jdt.core.IType)

Aggregations

JdtHyperlink (org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink)6 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)4 Test (org.junit.Test)4 IType (org.eclipse.jdt.core.IType)3 XtextResource (org.eclipse.xtext.resource.XtextResource)3 EObject (org.eclipse.emf.ecore.EObject)2 Region (org.eclipse.jface.text.Region)2 XtextHyperlink (org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink)2 Consumer (java.util.function.Consumer)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IMember (org.eclipse.jdt.core.IMember)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 IRegion (org.eclipse.jface.text.IRegion)1 JavaConstructorBuilder (org.eclipse.xtend.ide.codebuilder.JavaConstructorBuilder)1 JavaFieldBuilder (org.eclipse.xtend.ide.codebuilder.JavaFieldBuilder)1 JavaMethodBuilder (org.eclipse.xtend.ide.codebuilder.JavaMethodBuilder)1 Entity (org.eclipse.xtext.example.domainmodel.domainmodel.Entity)1