org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink Java Examples

The following examples show how to use org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PackageJsonHyperlinkHelperExtension.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public IHyperlink[] getHyperlinks(XtextResource resource, int offset) {
	EObject eObject = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
	Pair<SafeURI<?>, Region> linkedProjectWithRegion = getUriRegionPair(eObject);

	if (linkedProjectWithRegion != null) {
		URI uri = linkedProjectWithRegion.getFirst().toURI();
		IRegion region = linkedProjectWithRegion.getSecond();
		IN4JSProject uriProject = model.findProject(uri).orNull();

		String lnkName = uriProject == null ? ProjectDescriptionUtils.deriveN4JSProjectNameFromURI(uri)
				: uriProject.getProjectName().getRawName();

		XtextHyperlink hyperlink = hyperlinkProvider.get();
		hyperlink.setHyperlinkRegion(region);
		hyperlink.setURI(uri);
		hyperlink.setHyperlinkText(lnkName);

		return new IHyperlink[] { hyperlink };
	}

	return null;
}
 
Example #2
Source File: SolidityHyperlinkHelper.java    From solidity-ide with Eclipse Public License 1.0 6 votes vote down vote up
protected void createImportedNamespacesHyperlinksByOffset(XtextResource resource, int offset,
		IHyperlinkAcceptor acceptor) {
	INode node = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset);
	if (node != null) {			
		List<INode> importNodes = NodeModelUtils.findNodesForFeature(node.getSemanticElement(), SolidityPackage.Literals.IMPORT_DIRECTIVE__IMPORTED_NAMESPACE);
		if (importNodes != null && !importNodes.isEmpty()) {
			for (INode importNode : importNodes) {
				ImportDirective importElement = (ImportDirective) importNode.getSemanticElement();
				URI targetURI = getFileURIOfImport(importElement);
				XtextHyperlink result = getHyperlinkProvider().get();
				result.setURI(targetURI);
				Region region = new Region(importNode.getOffset(), importNode.getLength());
				result.setHyperlinkRegion(region);
				result.setHyperlinkText(targetURI.toString());
				acceptor.accept(result);
			}
		}
	}
}
 
Example #3
Source File: XtextHyperlinkHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void createLinksToBase(ITextRegion nameLocation,	AbstractRule rule, IHyperlinkAcceptor acceptor) {
	Set<AbstractRule> visited = Sets.newHashSet();
	Grammar grammar = GrammarUtil.getGrammar(rule);
	for(Grammar used: grammar.getUsedGrammars()) {
		String ruleName = rule.getName();
		AbstractRule overwritten = GrammarUtil.findRuleForName(used, ruleName);
		if (overwritten != null && visited.add(overwritten)) {
			URIConverter uriConverter = rule.eResource().getResourceSet().getURIConverter();
			String hyperlinkText = getLabelProvider().getText(rule) + " - " + GrammarUtil.getGrammar(overwritten).getName();
			URI uri = uriConverter.normalize(EcoreUtil.getURI(overwritten));

			XtextHyperlink result = getHyperlinkProvider().get();
			deprecatedSetRegion(result, toRegion(nameLocation));
			result.setURI(uri);
			result.setHyperlinkText(hyperlinkText);
			result.setTypeLabel("Go To Base");
			acceptor.accept(result);
		}
	}
}
 
Example #4
Source File: HyperlinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@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());
}
 
Example #5
Source File: HyperlinkHelperTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testCreateHyperlinksByOffset_06() {
	assertNull(helper.createHyperlinksByOffset(resource, model.indexOf(":EObject]"), true));
	int idx = model.indexOf("EObject]");
	int length = "EObject".length();
	for(int i = idx; i <= length; i++) {
		IHyperlink[] hyperlinks = helper.createHyperlinksByOffset(resource, i, true);
		assertNotNull(hyperlinks);
		assertEquals(1, hyperlinks.length);
		assertTrue(hyperlinks[0] instanceof XtextHyperlink);
		XtextHyperlink hyperLink = (XtextHyperlink) hyperlinks[0];
		assertEquals("EObject".length(), hyperLink.getHyperlinkRegion().getLength());
		assertEquals(
				grammar.eResource().getResourceSet().getURIConverter().normalize(
				EcoreUtil.getURI(EcorePackage.eINSTANCE.getEObject())), hyperLink.getURI());
	}
}
 
Example #6
Source File: PackageImportHyperlinkHelper.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
protected IHyperlink createHyperlink(INode node, final PackageImport pkgImport) {
	XtextHyperlink result = getHyperlinkProvider().get();
	result.setURI(pkgImport.getFileURI());
	Region region = new Region(node.getOffset(), node.getLength());
	result.setHyperlinkRegion(region);
	result.setHyperlinkText(pkgImport.getUri().toString());
	return result;
}
 
Example #7
Source File: AbstractHyperlinkHelperTest.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks if there is a hyperlink on the object marked by the given tag and that it points to the target URI.
 *
 * @param tag
 *          the tag marking the object where the hyperlink should be
 * @param target
 *          the target URI pointed to by the hyperlink of the given tag
 */
protected void assertHasHyperlinks(final int tag, final URI target) {
  Collection<URI> actualTargets = Sets.newLinkedHashSet();
  for (IHyperlink hyperlink : getHyperlinks(tag)) {
    if (hyperlink instanceof XtextHyperlink) {
      actualTargets.add(((XtextHyperlink) hyperlink).getURI());
    }
  }
  Assert.assertThat(actualTargets, CoreMatchers.hasItem(target));
}
 
Example #8
Source File: HyperlinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testConstructor() throws Exception {
	String modelAsString =
		"class Foo {\n"
		+ "  def bar() { new Foo() }\n"
		+ "}";
	XtextResource resource = (XtextResource) testHelper.xtendFile("Foo", modelAsString).eResource();
	IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, modelAsString.indexOf("Foo", 10), true);
	assertEquals(1, hyperlinks.length);
	assertEquals("Open Declaration - Foo", hyperlinks[0].getHyperlinkText());
	assertTrue(((XtextHyperlink)hyperlinks[0]).getURI().isPlatformResource());
}
 
Example #9
Source File: HyperlinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #10
Source File: AbstractHyperlinkingTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String target(IHyperlink hyperlink) {
	if (hyperlink instanceof JdtHyperlink) {
		return _target((JdtHyperlink) hyperlink);
	} else if (hyperlink instanceof XtextHyperlink) {
		return _target((XtextHyperlink) hyperlink);
	} else {
		return _target(hyperlink);
	}
}
 
Example #11
Source File: HyperlinkHelperTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCreateHyperlinksByOffset_07() {
	assertNull(helper.createHyperlinksByOffset(resource, model.indexOf("Element*") - 1, true));
	IHyperlink[] hyperlinks = helper.createHyperlinksByOffset(resource, model.indexOf("lement*"), true);
	assertNotNull(hyperlinks);
	assertEquals(1, hyperlinks.length);
	assertTrue(hyperlinks[0] instanceof XtextHyperlink);
	XtextHyperlink hyperLink = (XtextHyperlink) hyperlinks[0];
	assertEquals(model.indexOf("Element*"), hyperLink.getHyperlinkRegion().getOffset());
	assertEquals("Element".length(), hyperLink.getHyperlinkRegion().getLength());
	assertEquals(
			grammar.eResource().getResourceSet().getURIConverter().normalize(
			EcoreUtil.getURI(GrammarUtil.findRuleForName(grammar, "Element"))), hyperLink.getURI());
}
 
Example #12
Source File: HyperlinkHelperTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCreateHyperlinksByOffset_04() {
	IHyperlink[] links = helper.createHyperlinksByOffset(resource, model.indexOf("TRING r"), true);
	assertNotNull(links);
	assertEquals(1, links.length);
	assertTrue(links[0] instanceof XtextHyperlink);
	XtextHyperlink hyperLink = (XtextHyperlink) links[0];
	assertEquals(model.indexOf("STRING r"), hyperLink.getHyperlinkRegion().getOffset());
	assertEquals("STRING".length(), hyperLink.getHyperlinkRegion().getLength());
	assertEquals(
			grammar.eResource().getResourceSet().getURIConverter().normalize(
			EcoreUtil.getURI(GrammarUtil.findRuleForName(grammar.getUsedGrammars().get(0), "STRING"))), hyperLink.getURI());
}
 
Example #13
Source File: HyperlinkHelperTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCreateHyperlinksByOffset_01() {
	IHyperlink[] links = helper.createHyperlinksByOffset(resource, model.indexOf("common.Terminals"), true);
	assertNotNull(links);
	assertEquals(1, links.length);
	assertTrue(links[0] instanceof XtextHyperlink);
	assertEquals(model.indexOf("org.eclipse.xtext.common.Terminals"), links[0].getHyperlinkRegion().getOffset());
	assertEquals("org.eclipse.xtext.common.Terminals".length(), links[0].getHyperlinkRegion().getLength());
	checkHyperlink((XtextHyperlink) links[0]);
}
 
Example #14
Source File: N4JSHyperlinkHelper.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private void superCreateHyperlinksTo(IRegion region, EObject target, IHyperlinkAcceptor acceptor, URI normalized) {
	String hyperlinkText = labelProvider.getText(target);
	XtextHyperlink result = hyperlinkProvider.get();
	result.setHyperlinkRegion(region);
	result.setURI(normalized);
	result.setHyperlinkText(hyperlinkText);
	acceptor.accept(result);
}
 
Example #15
Source File: AbstractHyperlinkingTest.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected String _target(XtextHyperlink hyperlink) {
	ResourceSet resourceSet = resourceSetProvider.get(project);
	EObject eObject = resourceSet.getEObject(hyperlink.getURI(), true);
	return qualifiedNameProvider.getFullyQualifiedName(eObject).toString();
}
 
Example #16
Source File: XtextHyperlinkHelper.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Deprecated
private void deprecatedSetRegion(XtextHyperlink link, Region region) {
	link.setHyperlinkRegion(region);
}
 
Example #17
Source File: ImportURINavigationTest.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected void doTestNavigation(IUnitOfWork<URI, IFile> uriComputation, boolean expectFQN) throws Exception {
	IJavaProject project = JavaProjectSetupUtil.createJavaProject("importuriuitestlanguage.project");
	try {
		IFile first = project.getProject().getFile("src/first.importuriuitestlanguage");
		first.create(new StringInputStream("type ASimpleType"), true, null);
		
		ResourceSet resourceSet = resourceSetProvider.get(project.getProject());
		
		Resource resource = resourceFactory.createResource(URI.createURI("synthetic://second.importuriuitestlanguage"));
		resourceSet.getResources().add(resource);
		String model = "import '" + uriComputation.exec(first) + "' type MyType extends ASimpleType";
		resource.load(new StringInputStream(model), null);
		EcoreUtil.resolveAll(resource);
		Assert.assertTrue(resource.getErrors().isEmpty());
		
		IHyperlink[] hyperlinks = helper.createHyperlinksByOffset((XtextResource) resource, model.indexOf("SimpleType"), false);
		Assert.assertEquals(1, hyperlinks.length);
		IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage();
		Assert.assertNull(activePage.getActiveEditor());
		if (expectFQN) {
			Assert.assertEquals(URI.createURI(first.getLocationURI().toString()), ((XtextHyperlink)hyperlinks[0]).getURI().trimFragment());
		} else {
			Assert.assertEquals(URI.createPlatformResourceURI(first.getFullPath().toString(), true), ((XtextHyperlink)hyperlinks[0]).getURI().trimFragment());
		}
		hyperlinks[0].open();
		IEditorPart editor = activePage.getActiveEditor();
		Assert.assertNotNull(editor);
		IXtextDocument document = xtextDocumentUtil.getXtextDocument(editor);
		document.readOnly(new IUnitOfWork.Void<XtextResource>() {
			@Override
			public void process(XtextResource state) throws Exception {
				Assert.assertEquals("platform:/resource/importuriuitestlanguage.project/src/first.importuriuitestlanguage", state.getURI().toString());
			}
		});
		Assert.assertEquals("type ASimpleType", document.get());
		IEditorPart newPart = IDE.openEditor(activePage, first);
		Assert.assertEquals(1, activePage.getEditorReferences().length);
		Assert.assertEquals(editor, newPart);
	} finally {
		project.getProject().delete(true, null);
	}
}
 
Example #18
Source File: HyperlinkXpectMethod.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
private URI getURI(IHyperlink hyperlink) {
	final URI uri = hyperlink instanceof XtextHyperlink ? ((XtextHyperlink) hyperlink).getURI() : null;
	return uri;
}