【发布时间】:2020-12-03 16:21:08
【问题描述】:
我想为我的领域特定语言 (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。
总的来说,我觉得我没有按照预期的方式去做。那么,预期的方式是什么?我应该如何做语义高亮?
【问题讨论】:
-
使用 Xtend 还是 Java 由您决定,如果您使用当前的 Xtext 版本,我们鼓励您使用 Java 而不是 Xtend。对于已弃用的类,也有非弃用的类
org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator
标签: eclipse syntax-highlighting dsl xtext