Xtext2 keyword hovers
Tuesday, February 12th, 2013The current default implementation restricts hovers to significant region of an object. Think of them as the region of the name of the object. However, you may want also want to provide information for keywords (e.g. explanations for complicated modifiers).
The first step towards keyword hovers is overriding the binding for IEObjectHover.
public class MydslEObjectHover extends DispatchingEObjectTextHover {
@Inject
MyDslGrammarAccess grammarAccess;
@Override
protected Pair<EObject, IRegion> getXtextElementAt(XtextResource resource,
int offset) {
Pair<EObject, IRegion> temp = super.getXtextElementAt(resource, offset);
if(temp==null){
ILeafNode node = NodeModelUtils.findLeafNodeAtOffset(
resource.getParseResult().getRootNode(), offset);
if(node.getGrammarElement() instanceof Keyword){
IRegion region=new Region(node.getOffset(), node.getLength());
temp= Tuples.create(node.getGrammarElement(), region);
}
}
return temp;
}
@Override
public Object getHoverInfo(EObject first, ITextViewer textViewer,
IRegion hoverRegion) {
if(first instanceof Keyword){
return getHoverInfoForKeyword((Keyword)first);
}else{
return super.getHoverInfo(first, textViewer, hoverRegion);
}
}
private Object getHoverInfoForKeyword(final Keyword keyword){
//use grammarAccess here to see which Keyword you are dealing with
//and determine the text to show
// if(keyword==grammarAccess.getGreetingAccess().getHelloKeyword_0()){
// //...
// }
return keyword.getValue();
}
}
The second step is providing the information for the keyword hover, the third making it look nice.