eclipse - 如何在 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);
}
}
}
}
我有以下问题:
- 不推荐使用 IHighlightedPositionAcceptor
- ISemanticHighlightingCalculator 已弃用
- 可以导入 INode,但是“在这种情况下不允许使用该表达式,因为它不会引起任何副作用”
但是在“语义突出显示”下的官方文档中,他们也使用 INode。
总而言之,我觉得我没有按照预期的方式去做。那么,预期的方式是什么?我应该如何做语义突出显示?
解决方案
可以使用以下方法修复导入:
import org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator;
感谢克里斯蒂安的评论。大多数人在没有导入的情况下发布他们的代码。所以我不确定从哪里进口。
尽管在项目文件夹的上下文菜单中,它们只列出“新 Xtend 类”,但使用 java 似乎更好: INode 错误可以通过这个解决。
推荐阅读
- css - Material-UI Grid - 自动堆叠项目以占用最小垂直空间
- pandas - Pandas - 在不复制数据的情况下计算行数
- css - 2021 年 CSS 动画最佳实践是什么,以确保与所有浏览器兼容?
- swift - iOS 14 TableViewCell 无法设置选定的背景颜色
- arrays - 无法访问 Julia 数组(BoundsError:尝试访问索引 [1] 处的 0 元素 Vector{Int64})
- php - 通过不处理存在数据透视表的查询来排序 laravel 8
- php - 在查询中获取 ACF 后元值
- android - 如何在 Android Studio 的 AlerdDialog 中显示 DatePicker
- android - Android Studio - 启动模拟器前的提示
- html - CSS 允许页脚位置覆盖 @page .margin-bottom