Hyperlink customization [message #1105832] |
Tue, 10 September 2013 10:31 |
Aurélien Lebeau 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 #1106687 is a reply to message #1106565] |
Wed, 11 September 2013 12:04 |
Aurélien Lebeau 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 #1106774 is a reply to message #1106696] |
Wed, 11 September 2013 14:29 |
Aurélien Lebeau 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.
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03633 seconds