首页 > 解决方案 > 如何在 Xtext 中实现 SemanticHighlightingCalculator

问题描述

我想为我的领域特定语言 (DSL) 自定义语法突出显示。我想实现 YourDslSemanticHighlightingCalculator。我在网上找到了这段代码:

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

它来自这边: https ://www.eclipse.org/forums/index.php/t/1067057/

据我了解,Java 不再是实现的语言。我们应该在 Xtend 中实现。

通过一些进一步的研究并查看文档: https ://www.eclipse.org/Xtext/documentation/310_eclipse_support.html#highlighting 我最终得到了以下文件:

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

我有以下问题:

但是在“语义突出显示”下的官方文档中,他们也使用 INode。

总而言之,我觉得我没有按照预期的方式去做。那么,预期的方式是什么?我应该如何做语义突出显示?

标签: eclipsesyntax-highlightingdslxtext

解决方案


可以使用以下方法修复导入:

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

感谢克里斯蒂安的评论。大多数人在没有导入的情况下发布他们的代码。所以我不确定从哪里进口。

尽管在项目文件夹的上下文菜单中,它们只列出“新 Xtend 类”,但使用 java 似乎更好: INode 错误可以通过这个解决。


推荐阅读