Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Syntax Highlighting in Xtext(Syntax Highlighting in Xtext)
Syntax Highlighting in Xtext [message #1092609] Fri, 23 August 2013 02:07 Go to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hello,

I have a grammar like:

"start" Sample{
"details1" det1=STRING
"details2" det2=INT
"details3" det3=STRING
}

And my language will look something like:

start Sample{
details1 "Some details"
details2 20
details3 "Some data"
}

I want to COLOR only keywords such as "details1","details2","details3" in syntax highlighting.
What might be the best solution for it if i have some 30 keywords in my grammar??
Thanks in advance !! Smile Smile


Arshad
Re: Syntax Highlighting in Xtext [message #1092706 is a reply to message #1092609] Fri, 23 August 2013 05:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi keywords are already colored. Aren't they?

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Syntax Highlighting in Xtext [message #1092749 is a reply to message #1092706] Fri, 23 August 2013 06:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Ps: the hook for token based highlighting is AbstractAntlrTokenToAttributeIdMapper/DefaultAntlrTokenToAttributeIdMapper
(the one for semantic is ISemanticHighlightingCalculator)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Syntax Highlighting in Xtext [message #1092752 is a reply to message #1092706] Fri, 23 August 2013 06:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi,

the hook to customize token based highlighting is DefaultAntlrTokenToAttributeIdMapper


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Syntax Highlighting in Xtext [message #1092758 is a reply to message #1092752] Fri, 23 August 2013 06:56 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi,
Yes they are already colored.. But in this case i want to color only the keywords which comes inside "{" and "}" braces.. So default color for "start" and different color for details1, details2, details3.. What is the best way to do it. Thanks in advance


Arshad
Re: Syntax Highlighting in Xtext [message #1092759 is a reply to message #1092706] Fri, 23 August 2013 06:58 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi,
Yes they are already colored.. But in this case i want to color only the keywords which comes inside "{" and "}" braces.. So default color for "start" and different color for details1, details2, details3.. What is the best way to do it. Thanks in advance


Arshad
Re: Syntax Highlighting in Xtext [message #1092760 is a reply to message #1092706] Fri, 23 August 2013 06:59 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi,
Yes they are already colored.. But in this case i want to color only the keywords which comes inside "{" and "}" braces.. So default color for "start" and different color for details1, details2, details3.. What is the best way to do it. Thanks in advance


Arshad
Re: Syntax Highlighting in Xtext [message #1092776 is a reply to message #1092706] Fri, 23 August 2013 07:22 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
but the question is how to take away highlighting of "start", if I interprete the situation correctly
Re: Syntax Highlighting in Xtext [message #1092777 is a reply to message #1092706] Fri, 23 August 2013 07:23 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
But the questiuon seems how to supress this for the keyword "start"
Re: Syntax Highlighting in Xtext [message #1092800 is a reply to message #1092706] Fri, 23 August 2013 08:01 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi,
The keywords are already colored, But I want to give a different color to the keywrods which comes inside "{" and "}" braces.

Like:
start Sample{
details1 "Some details"
details2 20
details3 "Some data"
}

"start " will have default color and some custom color for "details1", "details2" and "details3".

What is the best way to do it? Thanks in advance !! :)Smile


Arshad
Re: Syntax Highlighting in Xtext [message #1092801 is a reply to message #1092706] Fri, 23 August 2013 08:01 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi,
The keywords are already colored, But I want to give a different color to the keywrods which comes inside "{" and "}" braces.

Like:
start Sample{
details1 "Some details"
details2 20
details3 "Some data"
}

"start " will have default color and some custom color for "details1", "details2" and "details3".

What is the best way to do it? Thanks in advance !! :)Smile


Arshad
Re: Syntax Highlighting in Xtext [message #1092802 is a reply to message #1092706] Fri, 23 August 2013 08:02 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi,
The keywords are already colored, But I want to give a different color to the keywrods which comes inside "{" and "}" braces.

Like:
start Sample{
details1 "Some details"
details2 20
details3 "Some data"
}

"start " will have default color and some custom color for "details1", "details2" and "details3".

What is the best way to do it? Thanks in advance !! :)Smile


Arshad
Re: Syntax Highlighting in Xtext [message #1092808 is a reply to message #1092609] Fri, 23 August 2013 08:08 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi,
I want to color the nodes which comes inside "{" and "}" with different color.
What is the best way to do it?

Ex.
start Sample{
details1 "Some details"
details2 20
details3 "Some data"
}

"start " with default color and "details1", "details2", "details3" with different color. Thanks in advance :)Smile


Arshad
Re: Syntax Highlighting in Xtext [message #1092919 is a reply to message #1092706] Fri, 23 August 2013 10:25 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
think it's more the question how to stop "start" beeing highlighted
Re: Syntax Highlighting in Xtext [message #1093060 is a reply to message #1092919] Fri, 23 August 2013 14:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Seems like the forum was broken. See additional answers above


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Syntax Highlighting in Xtext [message #1093069 is a reply to message #1092777] Fri, 23 August 2013 14:15 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
HI Uli and Christian,
Thanks for your reply. Can you suggest me some ideas related to it ??
Thanks in advance Smile


Arshad
Re: Syntax Highlighting in Xtext [message #1093072 is a reply to message #1093069] Fri, 23 August 2013 14:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Did you try to override the mapper?

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Syntax Highlighting in Xtext [message #1093139 is a reply to message #1093072] Fri, 23 August 2013 16:28 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi christian,
Yes. But did not get much idea how to proceed with it.. Sad


Arshad
Re: Syntax Highlighting in Xtext [message #1093151 is a reply to message #1093139] Fri, 23 August 2013 16:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi simply debug the place where keyword is returned.
Then you will see what in input params are to detect if you should be
returning something else in your subclass. Then simply override the
corresponding binding and have fun

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Syntax Highlighting in Xtext [message #1093159 is a reply to message #1093151] Fri, 23 August 2013 17:10 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi Christian,
Thanks a lot for your reply.. I will try with your suggestion.. Smile


Arshad
Re: Syntax Highlighting in Xtext [message #1093547 is a reply to message #1093159] Sat, 24 August 2013 08:13 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi Christian,
I have overridden the:
protected String calculateId(final String tokenName, final int tokenType) {} method from DefaultAntlrTokenToAttributeIdMapper class and now I am able to get all my tokens.
I removed invalid tokens for me like "RULE_STRING", "RULE_INT", all punctuation etc. But still how do I avoid "start" in:
start Sample{
details1 "Some details"
details2 20
details3 "Some data"
}

Do I have any other way than hardcoding this value ?? Because I will have a problem if a string literal is changed in the grammar.
Thanks in advance !! :)Smile

Regards,
Arshad


Arshad

[Updated on: Sat, 24 August 2013 08:14]

Report message to a moderator

Re: Syntax Highlighting in Xtext [message #1093574 is a reply to message #1093547] Sat, 24 August 2013 09:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi of you do lexacal Highlighting the only Chance is Todo it on hardcoding.
For everything Else you have to Digg into Semantic highlighting


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Syntax Highlighting in Xtext [message #1094244 is a reply to message #1092609] Sun, 25 August 2013 10:53 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi,
Can we use both "DefaultAntlrTokenToAttributeIdMapper" and " ISemanticHighlightingCalculator" for syntax highlighting one xtext language?? I tried it, first calculateid() method is getting called from "DefaultAntlrTokenToAttributeIdMapper" and then method present in "ISemanticHighlightingCalculator"... Would it be a good idea? The first one is a class and 2 one is an interface... Can I assume that order of these methods which are getting called would be the same??
Thanks in advance :):)Smile


Arshad
Re: Syntax Highlighting in Xtext [message #1094251 is a reply to message #1094244] Sun, 25 August 2013 11:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
No,

ISemanticHighlightingCalculator overrules DefaultAntlrTokenToAttributeIdMapper,
they are completely separate.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Syntax Highlighting in Xtext [message #1095831 is a reply to message #1094251] Tue, 27 August 2013 15:37 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi,
I am doing it by implementing ISemanticHighlightingCalculator. I am taking all the leaf nodes present.
My language looks something like:

Sample name.details.something {
content goes here
...............
..............
...............
}

ILeafNode treats "." as a different node.
So I get "name", ".", "details", ".", "something" as different nodes where as "name.details.something" is a single node for me.
How can I avoid this ?
Thanks for your help in advance :):)Smile


Arshad
Re: Syntax Highlighting in Xtext [message #1095834 is a reply to message #1095831] Tue, 27 August 2013 15:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi,

depending on your grammar there will be a corresponding ICompositeNode


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Syntax Highlighting in Xtext [message #1098010 is a reply to message #1095834] Fri, 30 August 2013 13:19 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi Christian,
Your suggestions helped me and I did it by implementing ISemantichighlightingCalculator. Thanks a lot !! Smile Smile


Arshad
Re: Syntax Highlighting in Xtext [message #1730286 is a reply to message #1098010] Sat, 23 April 2016 13:42 Go to previous messageGo to next message
RamaRao Nandamuri is currently offline RamaRao NandamuriFriend
Messages: 120
Registered: April 2016
Senior Member
Hi christian,

If I use ISemantichighlightingCalculator, then I need to write code for every rule to highlight them or I can write calculator for selected rule?
Re: Syntax Highlighting in Xtext [message #1730287 is a reply to message #1730286] Sat, 23 April 2016 14:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
yes you can

class MyDslUiModule extends AbstractMyDslUiModule {

def Class<? extends ISemanticHighlightingCalculator> bindISemanticHighlightingCalculator() {
MyDslSemanticHighlightingCalculator
}
}

public class MyDslSemanticHighlightingCalculator extends DefaultSemanticHighlightingCalculator{

@Inject
MyDslGrammarAccess ga;

@Override
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor,
CancelIndicator cancelIndicator) {
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.provideHighlightingFor(resource, acceptor, cancelIndicator);
}

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Syntax Highlighting in Xtext [message #1730355 is a reply to message #1730287] Mon, 25 April 2016 09:08 Go to previous message
RamaRao Nandamuri is currently offline RamaRao NandamuriFriend
Messages: 120
Registered: April 2016
Senior Member
Thank you Christian. It's working like gem
Previous Topic:Project Nature in Xtend
Next Topic:Problem with number sign
Goto Forum:
  


Current Time: Tue Apr 16 09:30:47 GMT 2024

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

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

Back to the top