简体   繁体   中英

How to implement SemanticHighlightingCalculator in Xtext

I would like to customize the syntax Highlighting for my Domain Specific Language (DSL). I would like to implement YourDslSemanticHighlightingCalculator. I found this code on the Internet:

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);
         }
      }
   }
}

It comes from this side: https://www.eclipse.org/forums/index.php/t/1067057/

As far as I understood already Java is not the language to implement anymore. We should implement in Xtend.

With some furthur research and a look at the documentation: https://www.eclipse.org/Xtext/documentation/310_eclipse_support.html#highlighting I ended up with the following file:

package org.xtext.example.mydsl.ui;

import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;

public class YourDslSemanticHighlightingCalculator implements ISemanticHighlightingCalculator {
    
    override provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor) {
          if (resource == null || resource.getParseResult() == null)
            return;
            
//          INode root = resource.getParseResult().getRootNode();
//              for (INode node : root.getAsTreeIterable()) {
//              if (node.getGrammarElement() instanceof CrossReference) {
//                      acceptor.addPosition(node.getOffset(), node.getLength(), 
//                  MyHighlightingConfiguration.CROSS_REF);
//              }   
        
        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 have the following problems:

  • IHighlightedPositionAcceptor is deprecated
  • ISemanticHighlightingCalculator is depredacted
  • INode can be imported, but "The expression is not allowd in this context, since it doesn't cause any side effects"

But in the official Documentation under "Semantic Highlighting" they also use INode.

Over all I have the feeling that I don't do it the intended way. So, what is the intended way? How I should do Semantic Highlighting?

The imports can be fixed by using:

import org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator;

Thanks Christian for the comment. Most people post their code without imports. So I was not shure where to import from.

And despite in the context menu of your project folders they only list "new Xtend class" it seems to be better to use java: The INode error can be solved by this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM