Search in sources :

Example 1 with XbaseImplementatorsHyperlink

use of org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink in project xtext-xtend by eclipse.

the class DetectImplHyperlinksTest method testComputeHyperlink_2.

@Test
public void testComputeHyperlink_2() throws Exception {
    String content = "package foo class Foo { def bar(String a) { fo|o() } def foo(){}}";
    XtextEditor xtextEditor = openEditor(content.replace("|", ""));
    int offset = content.indexOf("|");
    IHyperlink[] detectHyperlinks = hyperlinkDetector.detectHyperlinks(xtextEditor.getInternalSourceViewer(), new Region(offset, 1), true);
    assertEquals(3, detectHyperlinks.length);
    XbaseImplementatorsHyperlink hyperlink = Iterables.filter(Lists.newArrayList(detectHyperlinks), XbaseImplementatorsHyperlink.class).iterator().next();
    assertEquals(offset - 2, hyperlink.getHyperlinkRegion().getOffset());
    assertEquals(3, hyperlink.getHyperlinkRegion().getLength());
    IJavaElement element = hyperlink.getElement();
    assertTrue(element instanceof IMethod);
    assertEquals("foo", element.getElementName());
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XbaseImplementatorsHyperlink(org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink) Region(org.eclipse.jface.text.Region) IMethod(org.eclipse.jdt.core.IMethod) Test(org.junit.Test) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest)

Example 2 with XbaseImplementatorsHyperlink

use of org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink in project xtext-xtend by eclipse.

the class XtendHyperlinkHelper method createHyperlinksByOffset.

@Override
public void createHyperlinksByOffset(XtextResource resource, int offset, IHyperlinkAcceptor acceptor) {
    super.createHyperlinksByOffset(resource, offset, acceptor);
    if (canShowMany(acceptor)) {
        final EObject element = getEObjectAtOffsetHelper().resolveElementAt(resource, offset);
        if (element instanceof XtendField) {
            XtendField member = (XtendField) element;
            ILeafNode node = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset);
            if (isNameNode(member, XtendPackage.Literals.XTEND_FIELD__NAME, node) && member.getType() == null) {
                EObject jvmElement = associations.getPrimaryJvmElement(member);
                if (jvmElement instanceof JvmIdentifiableElement) {
                    addOpenInferredTypeHyperlink(resource, (JvmIdentifiableElement) jvmElement, node, acceptor);
                }
            }
        }
        if (element instanceof XtendFunction) {
            XtendFunction member = (XtendFunction) element;
            ILeafNode node = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset);
            if (isNameNode(member, XtendPackage.Literals.XTEND_FUNCTION__NAME, node)) {
                EObject jvmElement = associations.getPrimaryJvmElement(member);
                if (jvmElement instanceof JvmIdentifiableElement) {
                    JvmIdentifiableElement identifiableElement = (JvmIdentifiableElement) jvmElement;
                    if (member.getReturnType() == null) {
                        addOpenInferredTypeHyperlink(resource, identifiableElement, node, acceptor);
                    }
                    IJavaElement javaElement = javaElementFinder.findExactElementFor(identifiableElement);
                    if (sourceViewer != null && javaElement != null && (javaElement.getElementType() == IJavaElement.METHOD && canBeOverridden((IMethod) javaElement))) {
                        Region region = new Region(node.getOffset(), node.getLength());
                        acceptor.accept(new XbaseImplementatorsHyperlink(javaElement, region, sourceViewer, implOpener));
                    }
                }
            }
        }
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) IJavaElement(org.eclipse.jdt.core.IJavaElement) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) EObject(org.eclipse.emf.ecore.EObject) XbaseImplementatorsHyperlink(org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink) Region(org.eclipse.jface.text.Region) IMethod(org.eclipse.jdt.core.IMethod) XtendField(org.eclipse.xtend.core.xtend.XtendField)

Example 3 with XbaseImplementatorsHyperlink

use of org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink in project xtext-xtend by eclipse.

the class DetectImplHyperlinksTest method testComputeHyperlink_3.

@Test
public void testComputeHyperlink_3() throws Exception {
    String content = "package foo class Foo { def bar(S|tring a) { foo() } def foo(){}}";
    XtextEditor xtextEditor = openEditor(content.replace("|", ""));
    int offset = content.indexOf("|");
    IHyperlink[] detectHyperlinks = hyperlinkDetector.detectHyperlinks(xtextEditor.getInternalSourceViewer(), new Region(offset, 1), true);
    assertEquals(1, detectHyperlinks.length);
    List<XbaseImplementatorsHyperlink> list = Lists.newArrayList(Iterables.filter(Lists.newArrayList(detectHyperlinks), XbaseImplementatorsHyperlink.class));
    assertEquals(0, list.size());
}
Also used : IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XbaseImplementatorsHyperlink(org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink) Region(org.eclipse.jface.text.Region) Test(org.junit.Test) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest)

Example 4 with XbaseImplementatorsHyperlink

use of org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink in project xtext-xtend by eclipse.

the class OpenImplHyperlinksTest method testComputeHyperlink_1.

@Test
public void testComputeHyperlink_1() throws Exception {
    String content = "package foo class Foo implements IBar { override bar(String a) { b|ar('foo') } } interface IBar { def String bar(String a) }";
    XtextEditor xtextEditor = openEditor(content.replace("|", ""));
    int offset = content.indexOf("|");
    IHyperlink[] detectHyperlinks = hyperlinkDetector.detectHyperlinks(xtextEditor.getInternalSourceViewer(), new Region(offset, 1), true);
    XbaseImplementatorsHyperlink casted = null;
    for (IHyperlink iHyperlink : detectHyperlinks) {
        if (iHyperlink instanceof XbaseImplementatorsHyperlink) {
            casted = (XbaseImplementatorsHyperlink) iHyperlink;
        }
    }
    @SuppressWarnings("null") Field field = casted.getClass().getDeclaredField("opener");
    field.setAccessible(true);
    TestJvmImplementationOpener testOpener = new TestJvmImplementationOpener();
    field.set(casted, testOpener);
    casted.open();
    assertTrue(testOpener.isOpenInEditor);
    assertFalse(testOpener.isOpenQuickHierarchy);
}
Also used : Field(java.lang.reflect.Field) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XbaseImplementatorsHyperlink(org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest) Test(org.junit.Test)

Example 5 with XbaseImplementatorsHyperlink

use of org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink in project xtext-xtend by eclipse.

the class OpenImplHyperlinksTest method testComputeHyperlink_2.

@Test
public void testComputeHyperlink_2() throws Exception {
    String content = "package foo interface IBar {def void bar(String a)} class Foo implements IBar { override bar(String a){ val IBar x = null; x.b|ar('foo'} } class Foo2 implements IBar { override bar(String a) {} }";
    XtextEditor xtextEditor = openEditor(content.replace("|", ""));
    int offset = content.indexOf("|");
    IHyperlink[] detectHyperlinks = hyperlinkDetector.detectHyperlinks(xtextEditor.getInternalSourceViewer(), new Region(offset, 1), true);
    XbaseImplementatorsHyperlink casted = null;
    for (IHyperlink iHyperlink : detectHyperlinks) {
        if (iHyperlink instanceof XbaseImplementatorsHyperlink) {
            casted = (XbaseImplementatorsHyperlink) iHyperlink;
        }
    }
    @SuppressWarnings("null") Field field = casted.getClass().getDeclaredField("opener");
    field.setAccessible(true);
    TestJvmImplementationOpener testOpener = new TestJvmImplementationOpener();
    field.set(casted, testOpener);
    casted.open();
    assertFalse(testOpener.isOpenInEditor);
    assertTrue(testOpener.isOpenQuickHierarchy);
}
Also used : Field(java.lang.reflect.Field) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XbaseImplementatorsHyperlink(org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest) Test(org.junit.Test)

Aggregations

Region (org.eclipse.jface.text.Region)5 XbaseImplementatorsHyperlink (org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink)5 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)4 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)4 AbstractEditorTest (org.eclipse.xtext.ui.testing.AbstractEditorTest)4 Test (org.junit.Test)4 Field (java.lang.reflect.Field)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 IMethod (org.eclipse.jdt.core.IMethod)2 IRegion (org.eclipse.jface.text.IRegion)2 EObject (org.eclipse.emf.ecore.EObject)1 XtendField (org.eclipse.xtend.core.xtend.XtendField)1 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)1 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)1 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)1