Search in sources :

Example 1 with JdtHyperlink

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

the class HyperlinkingTest method testReturnTypeLabel.

@Test
public void testReturnTypeLabel() throws Exception {
    String modelAsString = "class Baz {def Baz meth(Baz baz, String str) { var vv='' x = baz j = str vv=j x.meth(baz,str) } var x = new Baz var j = '' }";
    XtextResource resource = (XtextResource) testHelper.xtendFile("Baz", modelAsString).eResource();
    int indexOf_x_FieldRef = modelAsString.indexOf("x");
    IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_x_FieldRef, true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - x : Baz", hyperlinks[0].getHyperlinkText());
    assertEquals("Open Field Type - Baz", hyperlinks[1].getHyperlinkText());
    int indexOf_Baz_ParamRef = modelAsString.indexOf("baz", indexOf_x_FieldRef);
    hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_Baz_ParamRef, true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - Baz baz", hyperlinks[0].getHyperlinkText());
    assertEquals("Open Parameter Type - Baz", hyperlinks[1].getHyperlinkText());
    int indexOf_j_FieldRef = modelAsString.indexOf("j", indexOf_Baz_ParamRef);
    hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_j_FieldRef, true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - j : String", hyperlinks[0].getHyperlinkText());
    assertEquals("Open Field Type - String", hyperlinks[1].getHyperlinkText());
    assertEquals("String", ((JdtHyperlink) hyperlinks[1]).getJavaElement().getElementName());
    int indexOf_vv_VarRef = modelAsString.indexOf("vv", indexOf_j_FieldRef);
    hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_vv_VarRef, true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - String vv", hyperlinks[0].getHyperlinkText());
    assertEquals("Open Variable Type - String", hyperlinks[1].getHyperlinkText());
    int indexOf_meth_MethodeCall = modelAsString.indexOf("meth", indexOf_vv_VarRef);
    hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_meth_MethodeCall, true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - meth(Baz, String) : Baz", hyperlinks[0].getHyperlinkText());
    assertEquals("Open Return Type - Baz", hyperlinks[1].getHyperlinkText());
}
Also used : IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextResource(org.eclipse.xtext.resource.XtextResource) JdtHyperlink(org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink) Test(org.junit.Test)

Example 2 with JdtHyperlink

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

the class HyperlinkingTest method testDispatchMethod.

@Test
public void testDispatchMethod() throws Exception {
    String modelAsString = "class Foo {\n" + "  def bar() { foo(new Object()) }\n" + "  def dispatch foo(Object o) { null }\n" + "  def dispatch foo(String s) { null }\n" + "  def dispatch foo(Number n) { null }\n" + "}";
    XtextResource resource = (XtextResource) testHelper.xtendFile("Foo", modelAsString).eResource();
    IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, modelAsString.indexOf("foo"), true);
    assertEquals(4, hyperlinks.length);
    assertEquals("Open Declaration - foo(Number) : Object", hyperlinks[0].getHyperlinkText());
    assertTrue(((XtextHyperlink) hyperlinks[0]).getURI().isPlatformResource());
    assertEquals("Open Declaration - foo(String) : Object", hyperlinks[1].getHyperlinkText());
    assertTrue(((XtextHyperlink) hyperlinks[1]).getURI().isPlatformResource());
    assertEquals("Open Declaration - foo(Object) : Object", hyperlinks[2].getHyperlinkText());
    assertTrue(((XtextHyperlink) hyperlinks[2]).getURI().isPlatformResource());
    assertEquals("Open Return Type - Object", hyperlinks[3].getHyperlinkText());
    assertEquals("Object", ((JdtHyperlink) hyperlinks[3]).getJavaElement().getElementName());
}
Also used : IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextHyperlink(org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink) XtextResource(org.eclipse.xtext.resource.XtextResource) JdtHyperlink(org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink) Test(org.junit.Test)

Example 3 with JdtHyperlink

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

the class HyperlinkingTest method testPlainMethod.

@Test
public void testPlainMethod() throws Exception {
    String modelAsString = "class Foo {\n" + "  def bar() { foo() }\n" + "  def foo() { null }\n" + "}";
    XtextResource resource = (XtextResource) testHelper.xtendFile("Foo", modelAsString).eResource();
    IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, modelAsString.indexOf("foo"), true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - foo() : Object", hyperlinks[0].getHyperlinkText());
    assertTrue(((XtextHyperlink) hyperlinks[0]).getURI().isPlatformResource());
    assertEquals("Open Return Type - Object", hyperlinks[1].getHyperlinkText());
    assertEquals("Object", ((JdtHyperlink) hyperlinks[1]).getJavaElement().getElementName());
}
Also used : IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextHyperlink(org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink) XtextResource(org.eclipse.xtext.resource.XtextResource) JdtHyperlink(org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink) Test(org.junit.Test)

Example 4 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 = eObjectAtOffsetHelper.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(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) EObject(org.eclipse.emf.ecore.EObject) JdtHyperlink(org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink) IType(org.eclipse.jdt.core.IType)

Example 5 with JdtHyperlink

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

the class DetectImplHyperlinksTest method testComputeHyperlink_1.

@Test
public void testComputeHyperlink_1() throws Exception {
    String content = "package foo class Foo { def b|ar(String a) {} }";
    XtextEditor xtextEditor = openEditor(content.replace("|", ""));
    int offset = content.indexOf("|");
    IHyperlink[] detectHyperlinks = hyperlinkDetector.detectHyperlinks(xtextEditor.getInternalSourceViewer(), new Region(offset, 1), true);
    assertEquals(2, detectHyperlinks.length);
    IHyperlink hyperlink = detectHyperlinks[0];
    assertTrue(hyperlink instanceof JdtHyperlink);
    JdtHyperlink casted = (JdtHyperlink) hyperlink;
    assertEquals(offset - 1, casted.getHyperlinkRegion().getOffset());
    assertEquals(3, casted.getHyperlinkRegion().getLength());
    IJavaElement element = ((JdtHyperlink) hyperlink).getJavaElement();
    assertTrue(element instanceof IType);
    assertEquals("Object", element.getElementName());
    assertEquals("Open Inferred Type - Object", casted.getHyperlinkText());
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) Region(org.eclipse.jface.text.Region) JdtHyperlink(org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest)

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 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 INode (org.eclipse.xtext.nodemodel.INode)1