protected void createPackageImportHyperlinksByOffset(XtextResource resource, int offset, IHyperlinkAcceptor acceptor) { INode node = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset); if (node != null && node.getGrammarElement() instanceof RuleCall && node.getSemanticElement() instanceof ImportScope) { NodeModelUtils.findNodesForFeature(node.getSemanticElement(), TypesPackage.Literals.PACKAGE__IMPORT); ImportScope importScope = (ImportScope) node.getSemanticElement(); EList<String> imports = importScope.getImports(); for (String pkgImport : imports) { PackageImport mappedImport = mapper.findPackageImport(resource, pkgImport); final URI targetURI = mappedImport.getUri(); XtextHyperlink result = getHyperlinkProvider().get(); result.setURI(targetURI); Region region = new Region(node.getOffset(), node.getLength()); result.setHyperlinkRegion(region); result.setHyperlinkText(targetURI.toString()); acceptor.accept(result); } } }
@Override public void createHyperlinksByOffset(XtextResource resource, int offset, IHyperlinkAcceptor acceptor) { super.createHyperlinksByOffset(resource, offset, acceptor); EObject element = eObjectAtOffsetHelper.resolveElementAt(resource, offset); if (element!=null && element instanceof Import) { Import imp = (Import) element; INode node = NodeModelUtils.getNode(element); URI publicUri = URI.createURI(imp.getImportURI()); URI uriForPublicUri = getUriForPublicUri(publicUri, resource.getResourceSet()); if (uriForPublicUri!=null) { XtextHyperlink hyperlink = getHyperlinkProvider().get(); Region region = new Region(node.getOffset(), node.getLength()); hyperlink.setHyperlinkRegion(region); hyperlink.setURI(uriForPublicUri); hyperlink.setHyperlinkText(uriForPublicUri.toString()); acceptor.accept(hyperlink); } } }
private EObject getTarget(XtextResource resource, IHyperlink hyperlink) { final ResourceSet resourceSet = resource != null ? resource.getResourceSet() : null; final URI uri = hyperlink instanceof XtextHyperlink ? ((XtextHyperlink) hyperlink).getURI() : null; final EObject target = resourceSet != null && uri != null ? resourceSet.getEObject(uri, true) : null; if (target instanceof SyntaxRelatedTElement) return ((SyntaxRelatedTElement) target).getAstElement(); return target; }
/** * 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)); }