Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 21:04 Go to next message
Torsten Juergeleit is currently offline Torsten JuergeleitFriend
Messages: 18
Registered: July 2009
Junior Member
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 21:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13488
Registered: July 2009
Senior Member
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);
	}

}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
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 22:01 Go to previous messageGo to next message
Torsten Juergeleit is currently offline Torsten JuergeleitFriend
Messages: 18
Registered: July 2009
Junior Member
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 23:03 Go to previous message
Torsten Juergeleit is currently offline Torsten JuergeleitFriend
Messages: 18
Registered: July 2009
Junior Member
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: Tue Jul 07 13:57:51 GMT 2020

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

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

Back to the top