Semantic Highlighting [message #1694493] |
Tue, 05 May 2015 11:47  |
Eclipse User |
|
|
|
I would like to highlight different parts in the grammar wherever they appear. for example I have a language:
Greeting: 'Hello' name=ID ';' ;
Now I want to highlight the ID that comes after the Hello in the rule Greeting. I searched for a good tutorial to help me start with this but they are all using xtext 0.8 and 1.0 and they use a lot of removed objects and utilities. Also, the book is not explaining anything concerning semantic highlighting.
Any help how can I start in Semantic Highlighting?
|
|
|
|
|
Re: Semantic Highlighting [message #1694505 is a reply to message #1694503] |
Tue, 05 May 2015 13:46   |
Eclipse User |
|
|
|
Hi,
the grammar elements are elements of the xtext metamodel and not of your metamodel.
you have basically two options
(1) use node model as starting point
public class MyDslSemanticHighlightingCalculator extends DefaultSemanticHighlightingCalculator{
@Inject
MyDslGrammarAccess ga;
@Override
protected void doProvideHighlightingFor(XtextResource resource,
IHighlightedPositionAcceptor acceptor) {
ICompositeNode rootNode = resource.getParseResult().getRootNode();
for (INode node : rootNode.getAsTreeIterable()) {
if (node.getGrammarElement() == ga.getGreetingAccess().getNameIDTerminalRuleCall_1_0()) {
acceptor.addPosition(node.getOffset(), node.getLength(), DefaultHighlightingConfiguration.KEYWORD_ID);
}
}
super.doProvideHighlightingFor(resource, acceptor);
}
}
(2) use eobject tree as starting point
public class MyDslSemanticHighlightingCalculator extends DefaultSemanticHighlightingCalculator{
@Override
protected void doProvideHighlightingFor(XtextResource resource,
IHighlightedPositionAcceptor acceptor) {
EObject rootObject = resource.getParseResult().getRootASTElement();
for (Greeting g : EcoreUtil2.getAllContentsOfType(rootObject, Greeting.class)) {
for (INode node : NodeModelUtils.findNodesForFeature(g, MyDslPackage.Literals.GREETING__NAME)) {
acceptor.addPosition(node.getOffset(), node.getLength(), DefaultHighlightingConfiguration.COMMENT_ID);
}
}
super.doProvideHighlightingFor(resource, acceptor);
}
}
p.s: it helps to inspect the node model using a debugger to find out what is there
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Semantic Highlighting [message #1695826 is a reply to message #1695823] |
Tue, 19 May 2015 10:39  |
Eclipse User |
|
|
|
Yes and no. That feature is called mark occurences and there is a button on top of eclipse for that. If you would want to do it with sematic highlighting you would do it like before
|
|
|
Powered by
FUDForum. Page generated in 0.06128 seconds