Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Creating hyperlinks in Xtext (Hyperlinking of importURI grammar element)
Creating hyperlinks in Xtext [message #1763487] Tue, 16 May 2017 16:01 Go to next message
Nouredine Hamri is currently offline Nouredine HamriFriend
Messages: 3
Registered: May 2017
Junior Member
Hi,
As part of the development of the alloy4eclipse plugin, I'm using the importURI approach for referencing elements in other model files in a Xtext grammar:

Open:
    "private"?  openName=OpenName .....

OpenName:
    "open" importURI=Name;

Name:
    ("this" | ID) ("/" ID)*;



I am wondering how to implement a hyperlink for the importURI to open the corresponding template file, knowing that references to imported items work very well.
So I want to make a Ctrl + Click on "Name" to navigate to the corresponding template file.

thank you,
Re: Creating hyperlinks in Xtext [message #1763522 is a reply to message #1763487] Wed, 17 May 2017 04:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
Subclass HyperlinkHelper and use ImportUriResolver (or its logic) should do the tick

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Creating hyperlinks in Xtext [message #1763539 is a reply to message #1763522] Wed, 17 May 2017 08:23 Go to previous messageGo to next message
Nouredine Hamri is currently offline Nouredine HamriFriend
Messages: 3
Registered: May 2017
Junior Member
Hi,
thank you for the answer,
Here is my HyperlinkHelper class, but it does not work with,
public class AlsHyperlinkHelper extends HyperlinkHelper {
	@Inject
	private Provider<XtextHyperlink> hyperlinkProvider;
	
	@Inject 
	private EObjectAtOffsetHelper eObjectAtOffsetHelper;

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

		EObject eObject = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
		if (eObject instanceof OpenName) {
			List<INode> nodes = NodeModelUtils.findNodesForFeature(eObject,
					AlsPackage.Literals.OPEN_NAME__IMPORT_URI);
			if (!nodes.isEmpty()) {
				INode node = nodes.get(0);
				XtextHyperlink hyperlink = hyperlinkProvider.get();
				hyperlink.setHyperlinkRegion(new Region(node.getOffset(), node.getLength()));
				hyperlink.setHyperlinkText("Open included file");
				IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
				IProject myProject = root.getProject(eObject.eResource().getURI().segment(1));
				String location;
				if (myProject.getFile(
						"/" + ((OpenName) eObject).getImportURI() + ".als").exists()) {
					location = "platform:/resource/"
							+ eObject.eResource().getURI().segment(1) + "/"
							+ ((OpenName) eObject).getImportURI() + ".als";

				} else {
					location = "platform:/resource/" + "A4 models library"
							+ "/" + ((OpenName) eObject).getImportURI()
							+ ".als";
				}
				URI importUri = URI.createURI(location);
				hyperlink.setURI(importUri);
				acceptor.accept(hyperlink);
			}
		}
	}

}

thank you,


Re: Creating hyperlinks in Xtext [message #1763545 is a reply to message #1763539] Wed, 17 May 2017 09:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
sorry your code works fine for me. sure it is called and did not miss the binding?

@FinalFieldsConstructor
class MyDslUiModule extends AbstractMyDslUiModule {

def Class<? extends IHyperlinkHelper> bindIHyperlinkHelper() {
AlsHyperlinkHelper
}

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Creating hyperlinks in Xtext [message #1763567 is a reply to message #1763545] Wed, 17 May 2017 13:23 Go to previous messageGo to next message
Nouredine Hamri is currently offline Nouredine HamriFriend
Messages: 3
Registered: May 2017
Junior Member
Hi,
At first, I was mistaken and I called from "AlsRuntimeModule" but the hyperlink does not work.
I changed this and I have called since "AlsUiModule" as you asked me, but this time my ".als" files do not even open.
thank you,
Re: Creating hyperlinks in Xtext [message #1763570 is a reply to message #1763567] Wed, 17 May 2017 13:48 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
did you debug the code?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Previous Topic:How do I customize grammar for my topology feature?
Next Topic:Correct usage of 'import'/'include' of files
Goto Forum:
  


Current Time: Mon Nov 04 15:48:02 GMT 2024

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

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

Back to the top