Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Highlighting of hidden elements is not as expected(With a custom highlighting calculator, hidden text (comments) is sometimes highlighted not as expected)
icon9.gif  Highlighting of hidden elements is not as expected [message #1697058] Mon, 01 June 2015 09:15 Go to next message
Jean Cauvin is currently offline Jean CauvinFriend
Messages: 2
Registered: June 2015
Junior Member
Hello experts,
I use XText for several weeks and have basic understanding of the framework. My question is this: In my grammar I have rules like these (it's only an excert from a more complex grammar). In fact, this is a slight modification of the default comment rules (which are hidden along with whitespaces) to introduce some sort of my own comments containing Doxygen documentation. These rules are not hidden.

/* documentation comment */
DocCommentElement:
   text = ( SL_DOC_COMMENT | ML_DOC_COMMENT )

terminal ML_DOC_COMMENT: '<#' -> '#>';
terminal SL_DOC_COMMENT: '#' !('\n'|'\r')* ('\r'? '\n')?;


Now I want to highlight them in a custom color. I successfully achieve this by providing my own implementation of ISemanticHighlightingCalculator and IHighlightingConfiguration. This is the code:

public class MyHighlightingConfiguration extends DefaultHighlightingConfiguration
{
   // provide ID strings for the highlighting calculator
   public static final String DOCUMENTATION_COMMENT_ID = "documentation comment";
   
   @Override
   public void configure(IHighlightingConfigurationAcceptor acceptor)
   {
      // let the default implementation first do the job
      super.configure(acceptor);
      
      // here our custom styles are used
      acceptor.acceptDefaultHighlighting(DOCUMENTATION_COMMENT_ID, "Documentation comment", documentationCommentTextStyle());
   }
   
   public TextStyle documentationCommentTextStyle() {
      TextStyle textStyle = defaultTextStyle().copy();
      textStyle.setColor(new RGB(63, 95, 191)); // default Eclipse JavaDoc color
      return textStyle;
   }
}


public class MySemanticHighlightingCalculator implements ISemanticHighlightingCalculator
{

   @Override
   public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor)
   {
      if (resource == null || resource.getParseResult() == null)
         return;

      INode root = resource.getParseResult().getRootNode();
      for (INode node : root.getAsTreeIterable())
      {
         if (node.getSemanticElement() instanceof DocCommentElement)
         {
            acceptor.addPosition(node.getOffset(), node.getLength(),
                  MyHighlightingConfiguration.DOCUMENTATION_COMMENT_ID);
         }
      }
   }
}


I notice a problem in a particular case and I don't know - is it my own mistake or a bug in the framework. Expected behavior is:

// default comment (should be green)
# my own comment (should be blue)

In this case I get the following result:

// default comment (should be green)
# my own comment (should be blue)

It seems, that if hidden text is immediately before my own comment, it takes its color. In other cases everything works as expected, colors are correct. Can you point me to an error in my grammar/implementation or confirm this as a bug in the framework? Thank you in advance!

P.S. I use this environment:
Eclipse Version: Luna SR2 (4.4.2) M20150204-1700
Xtext: 2.8.1.v201503230617
Re: Highlighting of hidden elements is not as expected [message #1697092 is a reply to message #1697058] Mon, 01 June 2015 14:01 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Jean,

this code looks bogus:

for (INode node : root.getAsTreeIterable())
{
if (node.getSemanticElement() instanceof DocCommentElement)
{
acceptor.addPosition(node.getOffset(), node.getLength(),
MyHighlightingConfiguration.DOCUMENTATION_COMMENT_ID);
}
}

If you put a breakpoint into the acceptor.addPosition line (or add a
System.out.println(node.getText) ) you'll see that you color the hidden
comment, too. That's because #getSemanticElement() will walk up the tree
to find the closest semantic element that covers the current
(potentially hidden) node. You may want to add a check like node.isHidden().
Also please note that you'll visit several regions multiple times
because you're traversing all nodes of the tree (leafs and their
parents). You may want to use root.getLeafNodes() instead.

Best,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Find help at http://xtext.itemis.com or xtext(@)itemis.com
Blog: zarnekow.blogspot.com
Twitter: @szarnekow
Google+: https://www.google.com/+SebastianZarnekow
Re: Highlighting of hidden elements is not as expected [message #1697103 is a reply to message #1697092] Mon, 01 June 2015 15:27 Go to previous messageGo to next message
Jean Cauvin is currently offline Jean CauvinFriend
Messages: 2
Registered: June 2015
Junior Member
Hi Sebastian,
thank you so much for your quick response and valuable hints! I changed the code like this, and now it works just great!

for (ILeafNode node : root.getLeafNodes())
 {
         if ( node.getSemanticElement() instanceof DocCommentElement && !node.isHidden())
         {
            acceptor.addPosition(node.getOffset(), node.getLength(),
                  MyHighlightingConfiguration.DOCUMENTATION_COMMENT_ID);
         }
}
Re: Highlighting of hidden elements is not as expected [message #1697161 is a reply to message #1697092] Tue, 02 June 2015 06:25 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Isn't there an easier way than using a semantic highlighter?

He wants to color terminmal rules so so he could simply replace the
DefaultAntlrTokenToAttributeIdMapper with a subclass who maps the
RULE_ML_DOC_COMMENT and RULE_SL_DOC_COMMENT to
MyHighlightingConfiguration.DOCUMENTATION_COMMENT_ID

Something like this:

public class MyAntlrTokenToAttributeIdMapper extends
DefaultAntlrTokenToAttributeIdMapper {
@Override
protected String calculateId(String tokenName, int tokenType) {
if( "RULE_ML_DOC_COMMENT".equals(tokenName) ||
"RULE_SL_DOC_COMMENT".equals(tokenName)) {
return MyHighlightingConfiguration.DOCUMENTATION_COMMENT_ID;
}
return super.calculateId(tokenName, tokenType);
}
}

If I understand highlighting correctly this is much more performant than
semantic highlighting because it can be applied directly while semantic
highlighting is done in an extra step async by the eclipse text framework.

Tom

On 01.06.15 16:01, Sebastian Zarnekow wrote:
> Hi Jean,
>
> this code looks bogus:
>
> for (INode node : root.getAsTreeIterable())
> {
> if (node.getSemanticElement() instanceof DocCommentElement)
> {
> acceptor.addPosition(node.getOffset(), node.getLength(),
> MyHighlightingConfiguration.DOCUMENTATION_COMMENT_ID);
> }
> }
>
> If you put a breakpoint into the acceptor.addPosition line (or add a
> System.out.println(node.getText) ) you'll see that you color the hidden
> comment, too. That's because #getSemanticElement() will walk up the tree
> to find the closest semantic element that covers the current
> (potentially hidden) node. You may want to add a check like
> node.isHidden().
> Also please note that you'll visit several regions multiple times
> because you're traversing all nodes of the tree (leafs and their
> parents). You may want to use root.getLeafNodes() instead.
>
> Best,
> Sebastian
Re: Highlighting of hidden elements is not as expected [message #1697165 is a reply to message #1697161] Tue, 02 June 2015 06:52 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Tom,

you're right, this is another solution to the task.

Best,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Find help at http://xtext.itemis.com or xtext(@)itemis.com
Blog: zarnekow.blogspot.com
Twitter: @szarnekow
Google+: https://www.google.com/+SebastianZarnekow
Previous Topic:How to update node adapters when xtext's EMF model changes
Next Topic:Parse XExpressions
Goto Forum:
  


Current Time: Tue Apr 16 10:59:48 GMT 2024

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

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

Back to the top