如何在eclipse编辑器插件中显示语法错误

时间:2022-09-11 19:52:05

How can I indicate syntax errors (e.g. an illegal sequence of tokens) in an eclipse editor plugin just like in the eclipse Java editor, i.e. by red wriggly underlines, a red marker on the scrollbar that you can jump to, and an explanatory message when you hover over either one?

我如何在eclipse编辑器插件中指示语法错误(例如非法的令牌序列),就像在eclipse Java编辑器中一样,即通过红色蠕动下划线,滚动条上的红色标记,您可以跳转到,以及解释性消息你徘徊在任何一个?

I'm writing an eclipse editor plugin for a custom file format (specifically, the "snake file format" of the Shark3D game engine). I have implemented a scanner to get syntax highlighting, and an outline.

我正在为自定义文件格式(特别是Shark3D游戏引擎的“蛇文件格式”)编写一个eclipse编辑器插件。我已经实现了一个扫描仪来获得语法高亮和一个大纲。

  • For the underlines, do I simply have the scanner return an IToken with a "wriggly underline" TextAttribute instead of the normal one, or is there a specific mechanism for marking syntax errors?
  • 对于下划线,我是否只是让扫描仪返回带有“蠕动下划线”TextAttribute的IToken而不是正常的,或者是否有用于标记语法错误的特定机制?

  • How do I implement the scrollbar markers? Is IAnnotationModel the relevant interface here? If so, where do I register the implementation so that the markers appear?
  • 如何实现滚动条标记? IAnnotationModel在这里是相关的接口吗?如果是这样,我在哪里注册实现以便标记出现?

  • I've only found SourceViewerConfiguration.getAnnotationHover(), which would allow me to implement the hover behaviour, but only for the "annotation", which I suppose means the scrollbar markers - how do I implement the hover behaviour for the text itself?
  • 我只找到了SourceViewerConfiguration.getAnnotationHover(),这将允许我实现悬停行为,但仅适用于“注释”,我想这意味着滚动条标记 - 如何实现文本本身的悬停行为?

I'd be happy specific advice as well as an URL of a tutorial that covers this - the eclipse help documents and examples don't seem to.

我会很高兴具体的建议以及一个涵盖这个的教程的URL - 日食帮助文档和示例似乎没有。

Edit: Markers are the best solutions to this. A working example of how to use them can be found in the plugin example code in org.eclipse.ui.examples.readmetool.AddReadmeMarkerAction

编辑:标记是最好的解决方案。有关如何使用它们的工作示例可以在org.eclipse.ui.examples.readmetool.AddReadmeMarkerAction中的插件示例代码中找到。

2 个解决方案

#1


You should be using Markers.

你应该使用标记。

An example derived from "The Java Developer's Guide to Eclipse" follows:

源自“Java开发人员Eclipse指南”的示例如下:

<extension point="org.eclipse.core.resources.markers"  
            id="snakesyntax"  
            name="Snake syntax error">  
    <super type="org.eclipse.core.resources.problemmarker" />  
    <super type="org.eclipse.core.resources.textmarker" />  
    <persistent value="true" />
<extension>

IMarker marker = res.createMarker("com.ibm.tool.resources.snakesyntax");

marker.setAttribute(IMarker.SEVERITY, 0);
marker.setAttribute(IMarker.CHAR_START, startOfSyntaxError);
marker.setAttribute(IMarker.CHAR_END, endOfSyntaxError);
marker.setAttribute(IMarker.LOCATION, "Snake file");
marker.setAttribute(IMarker.MESSAGE, "Syntax error");

#2


The correct way is using the marker interface.

正确的方法是使用标记界面。

Markers are essentially a model that maps marker objects to locations in your source code, so this makes sense in situations where you can have errors in multiple files. (see the IMarker interface)

标记本质上是一种将标记对象映射到源代码中的位置的模型,因此在多个文件中可能存在错误的情况下这是有意义的。 (参见IMarker界面)

A cheaper option if you want to add markup to your current editor but not to all the project is using Annotations, which you can add an remove yourself.

如果您想要将标记添加到当前编辑器而不是所有项目,则使用Annotations可以选择更便宜的选项,您可以自行添加删除。

Markers are represented in the UI as annotations but Eclipse adds and removes the annotations itself. With direct annotations, you're in control.

标记在UI中表示为注释,但Eclipse会添加和删除注释本身。通过直接注释,您可以控制。

#1


You should be using Markers.

你应该使用标记。

An example derived from "The Java Developer's Guide to Eclipse" follows:

源自“Java开发人员Eclipse指南”的示例如下:

<extension point="org.eclipse.core.resources.markers"  
            id="snakesyntax"  
            name="Snake syntax error">  
    <super type="org.eclipse.core.resources.problemmarker" />  
    <super type="org.eclipse.core.resources.textmarker" />  
    <persistent value="true" />
<extension>

IMarker marker = res.createMarker("com.ibm.tool.resources.snakesyntax");

marker.setAttribute(IMarker.SEVERITY, 0);
marker.setAttribute(IMarker.CHAR_START, startOfSyntaxError);
marker.setAttribute(IMarker.CHAR_END, endOfSyntaxError);
marker.setAttribute(IMarker.LOCATION, "Snake file");
marker.setAttribute(IMarker.MESSAGE, "Syntax error");

#2


The correct way is using the marker interface.

正确的方法是使用标记界面。

Markers are essentially a model that maps marker objects to locations in your source code, so this makes sense in situations where you can have errors in multiple files. (see the IMarker interface)

标记本质上是一种将标记对象映射到源代码中的位置的模型,因此在多个文件中可能存在错误的情况下这是有意义的。 (参见IMarker界面)

A cheaper option if you want to add markup to your current editor but not to all the project is using Annotations, which you can add an remove yourself.

如果您想要将标记添加到当前编辑器而不是所有项目,则使用Annotations可以选择更便宜的选项,您可以自行添加删除。

Markers are represented in the UI as annotations but Eclipse adds and removes the annotations itself. With direct annotations, you're in control.

标记在UI中表示为注释,但Eclipse会添加和删除注释本身。通过直接注释,您可以控制。