Search in sources :

Example 1 with AbstractHyperlink

use of org.eclipse.xtext.ui.editor.hyperlinking.AbstractHyperlink in project xtext-eclipse by eclipse.

the class XbaseHyperLinkHelper method createHyperlinksForCrossRef.

protected void createHyperlinksForCrossRef(XtextResource resource, INode crossRefNode, final IHyperlinkAcceptor acceptor) {
    EObject containedElementAt = getEObjectAtOffsetHelper().resolveContainedElementAt(resource, crossRefNode.getOffset());
    if (containedElementAt instanceof XAbstractFeatureCall) {
        IResolvedTypes resolveTypes = typeResolver.resolveTypes(resource);
        XAbstractFeatureCall featureCall = (XAbstractFeatureCall) containedElementAt;
        final JvmIdentifiableElement targetElement = featureCall.getFeature();
        if (targetElement instanceof JvmType || featureCall.getFeature() instanceof JvmEnumerationLiteral) {
            return;
        }
        IJavaElement javaElement = javaElementFinder.findExactElementFor(targetElement);
        if (sourceViewer != null && javaElement != null && (javaElement.getElementType() == IJavaElement.METHOD && canBeOverridden((IMethod) javaElement))) {
            acceptor.accept(new XbaseImplementatorsHyperlink(javaElement, new Region(crossRefNode.getOffset(), crossRefNode.getLength()), sourceViewer, implOpener));
        }
        LightweightTypeReference typeReference = resolveTypes.getActualType(featureCall);
        if (typeReference == null || typeReference.isPrimitive() || typeReference.isPrimitiveVoid()) {
            return;
        }
        final JvmType type = typeReference.getType();
        if (type != null)
            createHyperlinksTo(resource, crossRefNode, type, new IHyperlinkAcceptor() {

                @Override
                public void accept(IHyperlink hyperlink) {
                    if (hyperlink instanceof AbstractHyperlink) {
                        String target = labelForTargetElement(targetElement);
                        ((AbstractHyperlink) hyperlink).setHyperlinkText("Open " + target + " Type - " + type.getSimpleName());
                    }
                    acceptor.accept(hyperlink);
                }

                private String labelForTargetElement(final JvmIdentifiableElement targetElement) {
                    String target = "Return";
                    if (targetElement instanceof JvmField) {
                        target = "Field";
                    } else if (targetElement instanceof JvmFormalParameter) {
                        // special case for variables in switch and for loops
                        if (targetElement.eContainer() instanceof XSwitchExpression || targetElement.eContainer() instanceof XForLoopExpression) {
                            target = "Variable";
                        } else {
                            target = "Parameter";
                        }
                    } else if (targetElement instanceof XVariableDeclaration) {
                        target = "Variable";
                    }
                    return target;
                }
            });
    }
}
Also used : XVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration) IJavaElement(org.eclipse.jdt.core.IJavaElement) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) AbstractHyperlink(org.eclipse.xtext.ui.editor.hyperlinking.AbstractHyperlink) XSwitchExpression(org.eclipse.xtext.xbase.XSwitchExpression) JvmType(org.eclipse.xtext.common.types.JvmType) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall) IHyperlinkAcceptor(org.eclipse.xtext.ui.editor.hyperlinking.IHyperlinkAcceptor) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) EObject(org.eclipse.emf.ecore.EObject) JvmEnumerationLiteral(org.eclipse.xtext.common.types.JvmEnumerationLiteral) Region(org.eclipse.jface.text.Region) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IMethod(org.eclipse.jdt.core.IMethod) JvmField(org.eclipse.xtext.common.types.JvmField) XForLoopExpression(org.eclipse.xtext.xbase.XForLoopExpression)

Example 2 with AbstractHyperlink

use of org.eclipse.xtext.ui.editor.hyperlinking.AbstractHyperlink in project xtext-eclipse by eclipse.

the class XbaseHyperLinkHelper method addOpenInferredTypeHyperlink.

protected void addOpenInferredTypeHyperlink(final XtextResource resource, JvmIdentifiableElement typedElement, ILeafNode node, final IHyperlinkAcceptor acceptor) {
    IResolvedTypes resolveTypes = typeResolver.resolveTypes(resource);
    final LightweightTypeReference type = resolveTypes.getActualType(typedElement);
    if (type != null && !type.isUnknown() && type.getType() != null) {
        createHyperlinksTo(resource, new Region(node.getOffset(), node.getLength()), type.getType(), new IHyperlinkAcceptor() {

            @Override
            public void accept(IHyperlink hyperlink) {
                if (hyperlink instanceof AbstractHyperlink) {
                    AbstractHyperlink abstractHyperlink = (AbstractHyperlink) hyperlink;
                    abstractHyperlink.setHyperlinkText("Open Inferred Type - " + type.getSimpleName());
                    abstractHyperlink.setTypeLabel(SingleHoverShowingHyperlinkPresenter.SHOW_ALWAYS);
                }
                acceptor.accept(hyperlink);
            }
        });
    }
}
Also used : IHyperlinkAcceptor(org.eclipse.xtext.ui.editor.hyperlinking.IHyperlinkAcceptor) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) AbstractHyperlink(org.eclipse.xtext.ui.editor.hyperlinking.AbstractHyperlink) Region(org.eclipse.jface.text.Region) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Aggregations

Region (org.eclipse.jface.text.Region)2 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)2 AbstractHyperlink (org.eclipse.xtext.ui.editor.hyperlinking.AbstractHyperlink)2 IHyperlinkAcceptor (org.eclipse.xtext.ui.editor.hyperlinking.IHyperlinkAcceptor)2 ITextRegion (org.eclipse.xtext.util.ITextRegion)2 TextRegion (org.eclipse.xtext.util.TextRegion)2 IResolvedTypes (org.eclipse.xtext.xbase.typesystem.IResolvedTypes)2 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)2 EObject (org.eclipse.emf.ecore.EObject)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IMethod (org.eclipse.jdt.core.IMethod)1 JvmEnumerationLiteral (org.eclipse.xtext.common.types.JvmEnumerationLiteral)1 JvmField (org.eclipse.xtext.common.types.JvmField)1 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)1 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)1 JvmType (org.eclipse.xtext.common.types.JvmType)1 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)1 XForLoopExpression (org.eclipse.xtext.xbase.XForLoopExpression)1 XSwitchExpression (org.eclipse.xtext.xbase.XSwitchExpression)1 XVariableDeclaration (org.eclipse.xtext.xbase.XVariableDeclaration)1