Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Semantic Highlighting
Semantic Highlighting [message #1694493] Tue, 05 May 2015 15:47 Go to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
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 #1694497 is a reply to message #1694493] Tue, 05 May 2015 16:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i dont know any resource on that but it has not really changed since the old days of 0.8 and 1.0
the point that has changed is the node model that is (often) used to do the highlighting.

so what you basically do is to traverse the node model and assign configIds to the nodes.
the domain model example contains a starting point for that


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Semantic Highlighting [message #1694503 is a reply to message #1694497] Tue, 05 May 2015 16:59 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
I tried it and it worked for keywords and terminals, I tried to do it with Semantic Highlighting but here is were I got lost:

I created my calculator that extends DefaultSemanticHighlightingCalculator and override provideHighlightingFor:

   @Override
   public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor) {
      // TODO Auto-generated method stub
      if(resource == null || resource.getParseResult() == null) {
         return;
      }
      
      INode root = resource.getParseResult().getRootNode();
      for(INode node : root.getAsTreeIterable()){
         if(node.getGrammarElement() instanceof Greeting){
            acceptor.addPosition(node.getOffset(), node.getLength(), SyntaxHighlightConfigurations.GREETING);
         }
      }
      
   }


How can I access the feature name in Greeting by nodes iterator so I can change its color?
Re: Semantic Highlighting [message #1694505 is a reply to message #1694503] Tue, 05 May 2015 17:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Semantic Highlighting [message #1694529 is a reply to message #1694505] Wed, 06 May 2015 02:11 Go to previous messageGo to next message
Bob Tao is currently offline Bob TaoFriend
Messages: 23
Registered: March 2015
Junior Member
Hi, Christian!
where should I add "MyDslSemanticHighlightingCalculator" and let it run?
I can't found the method to be override in "MyDslUiModule".

////// I solve it.
@Override
public void configure(Binder binder) {
// TODO Auto-generated method stub
super.configure(binder);
binder.bind(DefaultSemanticHighlightingCalculator.class).to(UclSemanticHighlightingCalculator.class);
}

[Updated on: Wed, 06 May 2015 02:47]

Report message to a moderator

Re: Semantic Highlighting [message #1694530 is a reply to message #1694529] Wed, 06 May 2015 03:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
You can add a method to the U.IModule

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Semantic Highlighting [message #1694547 is a reply to message #1694530] Wed, 06 May 2015 08:28 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
Thanks lot christian Smile Smile It worked Smile
Re: Semantic Highlighting [message #1694558 is a reply to message #1694547] Wed, 06 May 2015 09:55 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
I would like to ask if I can change the selection color. for example when I select a node in the outline tree I woule like to have the part that represents the selected node to be colored (background and text). Is this also possible? if yes, where is the starting point for this?
Re: Semantic Highlighting [message #1694564 is a reply to message #1694558] Wed, 06 May 2015 11:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
sorry have no idea for that (is not connected to semantic highlighting at all).
do you use toggle with outline feature? there is already stuff highlighted.
=> you have only to find out where these colors come from.
maybe they can be configured in the preferences


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Semantic Highlighting [message #1694565 is a reply to message #1694564] Wed, 06 May 2015 11:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
The one you are after are probably in General -> Editors -> Text Editors ===> Then Appearance Color options -> Current line highlight & Selection background


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Semantic Highlighting [message #1694568 is a reply to message #1694565] Wed, 06 May 2015 11:36 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
Thanks, I thought there was a programmatical way of changing this colors as well and to add them as options to the same preference menu with the syntax highlighting. I know about changing it from Text Editors appearance color options.
Re: Semantic Highlighting [message #1694569 is a reply to message #1694568] Wed, 06 May 2015 11:38 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
Is there a way to change the syntax highlight in the templates preference menu?
i.e. I want to change the color of ${name} in the Preview of the templates section in the preferences.
Re: Semantic Highlighting [message #1694571 is a reply to message #1694569] Wed, 06 May 2015 11:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
sry have no idea on that.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Semantic Highlighting [message #1695816 is a reply to message #1694571] Tue, 19 May 2015 14:14 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
how can I highlight an EObject with its references?
Re: Semantic Highlighting [message #1695817 is a reply to message #1695816] Tue, 19 May 2015 14:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
What do you mean by that

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Semantic Highlighting [message #1695823 is a reply to message #1695817] Tue, 19 May 2015 14:24 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
for example I have
Integer: 'int' name=ID ';' ;
expression: leftop=[Integer|ID] (op=operator rightop=[Integer|ID]) ;

Is there a way to set a highlight for the EObject Integer and then the highlight reflects on its usages in the expression? i.e I set the highlight of EObject Integer to red then when I type an expression like this a + b + c where a and b are integers they are highlighted by red as well
Re: Semantic Highlighting [message #1695826 is a reply to message #1695823] Tue, 19 May 2015 14:39 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:NoSuchMethodException
Next Topic:Formatting2 (2.8.1): AbstractRegionAccess.regionForEObject
Goto Forum:
  


Current Time: Fri Apr 19 02:53:24 GMT 2024

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

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

Back to the top