Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Hyperlink customization
Hyperlink customization [message #1105832] Tue, 10 September 2013 10:31 Go to next message
Aurélien Lebeau is currently offline Aurélien LebeauFriend
Messages: 16
Registered: September 2013
Junior Member
Hello,

Here is my problem, I am developing a RCP which contains a variables navigator (contained in a database). I want to allow ctrl-clic on a variable in a DSL file and provide an hyperlink to either :
- The declaration of the variable in the same file
- The variable in the navigator

I am already sure that there is only one possibility, the redefinition of variable is not allowed by the validator, so I will have only one choice given a variable name.


I have some trouble customizing the HyperlinkHelper. In some way, I need to default cross-reference to link to variable in the same file, but I also need to override it to link to the navigator. My grammar is cross-reference-free, so I need to reimplement it to link to the same file.


For now, I have this in my UIModule :
	public Class<? extends DefaultHyperlinkDetector> bindDefaultHyperlinkDetector() {
		return DSLLinkDetector.class;
	}
	
	@Override
	public Class<? extends IHyperlinkHelper> bindIHyperlinkHelper() {
		return DSLLinkHelper.class;
	}


My DSLLinkHelper is like this :
@Override
	public void createHyperlinksByOffset(XtextResource resource, int offset,
			IHyperlinkAcceptor acceptor) {
...
// Handle of the link to the navigator
...
// If not link to navigator found, try in the same file

                                final URIConverter uriConverter = resource.getResourceSet()
						.getURIConverter();
				final URI uri = EcoreUtil.getURI(target);

				final URI normalized = uri.isPlatformResource() ? uri
						: uriConverter.normalize(uri);
				//System.out.println("URI = " + normalized);

				XtextHyperlink result = getHyperlinkProvider().get;
				result.setHyperlinkRegion(new Region(adapter.getOffset() -10 ,
						adapter.getLength()));
				result.setURI(normalized);
				result.setHyperlinkText("variable");
				acceptor.accept(result);
...
}

Here, I have an NullPointerException when I do getHyperlinkProvider().get. I suspect that the @Inject did not go well in the XtextHyperlink.


When I instantiate a new XtextHyperlink(); instead, the NullPointerException appears when I "ctrl+clic" and the open() of XtextHyperLink. Again, I think this is because the
@Inject 
private IURIEditorOpener uriEditorOpener;
did not go well because of the inject.



Any idea why ?? Or any idea how to do what I want ?

Thanks !
Re: Hyperlink customization [message #1106135 is a reply to message #1105832] Tue, 10 September 2013 18:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi,

sorry have no idea,
can you come up with a complete example
(grammar ...)


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Hyperlink customization [message #1106525 is a reply to message #1106135] Wed, 11 September 2013 08:02 Go to previous messageGo to next message
Aurélien Lebeau is currently offline Aurélien LebeauFriend
Messages: 16
Registered: September 2013
Junior Member
My grammar is ~1000 lines big, so it won't help.

What I have is something like
variable: IDENTIFIER
variableDeclaration: IDENTIFIER


As my proposal provider and validator are different from the default ones, I could not simply use cross-references since I have to check if the variable is declared in my DB as well as in the code. If I use cross-ref, a variable that is not define in the code will always considered as error even if it present in the DB (and my validator checks for it). In conclusion, I must use simply an IDENTIFIER and implement my own proposal provider and validator. These two work well.

The problem comes for the hyperlinks. I managed to override the default hyperlinkHelper and I could put some code to select the variable in my navigator. My problem is I also want to hyperlink to the declaration in the code. In short, I have to reimplement the default hyperlink helper that is used when cross-references happen.

Re: Hyperlink customization [message #1106565 is a reply to message #1106525] Wed, 11 September 2013 09:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi I thought of a simple example with a dummy grammar

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Hyperlink customization [message #1106687 is a reply to message #1106565] Wed, 11 September 2013 12:04 Go to previous messageGo to next message
Aurélien Lebeau is currently offline Aurélien LebeauFriend
Messages: 16
Registered: September 2013
Junior Member
Ok, let's go with something like this :

program: variableDeclarations? variableAssignationsList ;

variableDeclarations: "VAR" variableDeclarationList "END_VAR";
variableDeclarationsList: variableDeclaration ';' (variableDeclarationsList)?;
variableDeclaration: IDENTIFIER;

variableAssignationsList: variableAssignation ';' (variableAssignationsList)?;
variableAssignation: IDENTIFIER '=' INT;


I want to manually implement cross-references from variableAssignation to variableDeclaration, in a way that the IDENTIFIER must be declared before being assigned. I can't use cross-references in my grammar, because variableDeclaration can be in a database and the use of cross-references gives error for each variable that is not declared in variableDeclaration.

[Updated on: Wed, 11 September 2013 12:06]

Report message to a moderator

Re: Hyperlink customization [message #1106696 is a reply to message #1106687] Wed, 11 September 2013 12:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi as I said I want code I can copy and paste so why don't you copy
and paste create a small example

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Hyperlink customization [message #1106774 is a reply to message #1106696] Wed, 11 September 2013 14:29 Go to previous messageGo to next message
Aurélien Lebeau is currently offline Aurélien LebeauFriend
Messages: 16
Registered: September 2013
Junior Member
Here's my grammar :
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

program: vd=variableDeclarations? va=variableAssignationsList ;

variableDeclarations: {variableDeclarations} "VAR" vdl=variableDeclarationsList "END_VAR";
variableDeclarationsList: {variableDeclarationList} vd=variableDeclaration ';' (vdl=variableDeclarationsList)?;
variableDeclaration: {variableDeclaration} name=ID;

variableAssignationsList: {variableAssignationList} va=variableAssignation ';' (val=variableAssignationsList)?;
variableAssignation: {variableAssignation} name=variableName '=' INT;
variableName: {variableName} name=ID;


Here's my MyDslUIModule :
public class MyDslUiModule extends org.xtext.example.mydsl.ui.AbstractMyDslUiModule {
	public MyDslUiModule(AbstractUIPlugin plugin) {
		super(plugin);
	}
	
	public Class<? extends DefaultHyperlinkDetector> bindDefaultHyperlinkDetector() {
		return MyDslLinkDetector.class;
	}
	
	
	public Class<? extends IHyperlinkHelper> bindIHyperlinkHelper() {
		return MyDslLinkHelper.class;
	}
}


Here's my new LinkDetector :
public class MyDslLinkDetector extends DefaultHyperlinkDetector {
	@Override
	public IHyperlink[] detectHyperlinks(ITextViewer textViewer,
			IRegion region, boolean canShowMultiplehyperlinks) {
		this.setHelper(new MyDslLinkHelper());
		return super.detectHyperlinks(textViewer, region,
				canShowMultiplehyperlinks);
	}

}


Here's my new LinkHelper:
public class MyDslLinkHelper extends HyperlinkHelper {

	@Override
	public void createHyperlinksByOffset(XtextResource resource, int offset,
			IHyperlinkAcceptor acceptor) {


		EObjectAtOffsetHelper eObjectAtOffsetHelper = new EObjectAtOffsetHelper();
		EObject eObject = eObjectAtOffsetHelper.resolveElementAt(resource, offset);

		if (eObject instanceof variableName) {

			variableName variableName = (variableName) eObject;
			CompositeNode adapter = (CompositeNode) NodeModelUtils.getNode(variableName);

			Region region = new Region(adapter.getOffset(), adapter.getLength());
			
			final URIConverter uriConverter = resource.getResourceSet().getURIConverter();
			final String hyperlinkText = "Variable";
			
			final URI uri = EcoreUtil.getURI( *** );
			final URI normalized = uri.isPlatformResource() ? uri : uriConverter.normalize(uri);

			XtextHyperlink result = new XtextHyperlink(); // Does not show link with getHyperlinkProvider().get()
			result.setHyperlinkRegion(region);
			result.setURI(normalized);
			result.setHyperlinkText(hyperlinkText);
			acceptor.accept(result);
		}
	}
}


For the last one, I have two problems. First, I do not know how to get the URI of the variable declaration corresponding to the variable use. And, I can not use the hyperlink provider from HyperLinkHelper (see comment), otherwise the link do not show up. If I use a new Hyperlink provider, the link is shown. But as, there is not URI, there is no effect when I clic on it.
Re: Hyperlink customization [message #1106787 is a reply to message #1106774] Wed, 11 September 2013 14:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi,

without looking at the code:
you can get the URI using EcoreUtil.getURI(object)

if you use new XtextHyperlink() the hyperlink will not have an uriopener right?
this should result in an NPE.


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Hyperlink customization [message #1106791 is a reply to message #1106787] Wed, 11 September 2013 14:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Btw why this:
this.setHelper(new MyDslLinkHelper());

of course you will get an npe !

=> simply use the defaulthyperlink detector.


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Hyperlink customization [message #1107388 is a reply to message #1106791] Thu, 12 September 2013 10:23 Go to previous message
Aurélien Lebeau is currently offline Aurélien LebeauFriend
Messages: 16
Registered: September 2013
Junior Member
Thanks a lot for these remarks, I got it working !!

Very Happy
Previous Topic:import ecore model using nsURI
Next Topic:Eopposite Reference in Xtext
Goto Forum:
  


Current Time: Fri Jul 10 01:04:11 GMT 2020

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

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

Back to the top