Skip to main content



      Home
Home » Modeling » TMF (Xtext) » How to implement hyperlinking for URI of importURI grammar element?(For references Xtext provides hyperlinking support out-of-the box. How can similar hyperlinking implemented for the URI in a importURI grammar element?)
icon5.gif  How to implement hyperlinking for URI of importURI grammar element? [message #1226453] Wed, 01 January 2014 16:04 Go to next message
Eclipse UserFriend
I'm using the importURI approach for referencing elements in other model files in a Xtext grammar:

DslImport :
	'import' importURI=STRING;


For references to elements in the imported model Xtexts hyperlinking support is working great.
Now I'm wondering how to implement hyperlinking for the importURI to open the corresponding model file.
Any pointers?

/Torsten
Re: How to implement hyperlinking for URI of importURI grammar element? [message #1226465 is a reply to message #1226453] Wed, 01 January 2014 16:49 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

i think you have todo this yourself
you may use this (quick and dirty) code as starting point. (maybe googling reveals better code

public class MyDslHyperlinkHelper extends HyperlinkHelper {
	
	@Inject
	MyDslGrammarAccess ga;
	
	@Inject
	ImportUriResolver r;
	
	@Inject
	private Provider<XtextHyperlink> hyperlinkProvider;
	
	@Inject
	ImportUriGlobalScopeProvider gsp;
	
	@Override
	public void createHyperlinksByOffset(XtextResource resource, int offset,
			IHyperlinkAcceptor acceptor) {
		INode node = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset);
		if (node != null && node.getGrammarElement() instanceof RuleCall && node.getSemanticElement() instanceof Import) {
			if (ga.getSTRINGRule().equals(((RuleCall)node.getGrammarElement()).getRule())) {
				Import i = (Import) node.getSemanticElement();
				String uriString = i.getImportURI();
				URI uri = URI.createURI(uriString);
				final URIConverter uriConverter = resource.getResourceSet().getURIConverter();
				final URI normalized = uri.isPlatformResource() ? uri : uriConverter.normalize(uri);
				final URI targetURI = gsp.getResourceDescriptions(resource, Lists.newArrayList(normalized)).getResourceDescription(normalized).getURI();
				XtextHyperlink result = hyperlinkProvider.get();
				result.setURI(targetURI);
				Region region = new Region(node.getOffset(), node.getLength());
				result.setHyperlinkRegion(region);
				result.setHyperlinkText(uriString);
				acceptor.accept(result);
			}
		}
		super.createHyperlinksByOffset(resource, offset, acceptor);
	}

}
icon14.gif  Re: How to implement hyperlinking for URI of importURI grammar element? [message #1226469 is a reply to message #1226465] Wed, 01 January 2014 17:01 Go to previous messageGo to next message
Eclipse UserFriend
Hi Christian,

thanks for the fast response. Your code snippet is perfect.

Cheers,
/Torsten
Re: How to implement hyperlinking for URI of importURI grammar element? [message #1226489 is a reply to message #1226469] Wed, 01 January 2014 18:03 Go to previous message
Eclipse UserFriend
And it's working like a charm Smile

index.php/fa/17149/0/
Previous Topic:ANTLR replacement
Next Topic:xtext validation on tabular inputs
Goto Forum:
  


Current Time: Wed Jul 23 19:27:42 EDT 2025

Powered by FUDForum. Page generated in 0.04537 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top