How to show syntax errors in an eclipse editor plugin
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?
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.
- For the underlines, do I simply have the scanner return an
ITokenwith a "wriggly underline"
TextAttributeinstead of the normal one, or is there a specific mechanism for marking syntax errors?
- How do I implement the scrollbar markers? Is
IAnnotationModelthe relevant interface here? If so, where do I register the implementation so that the markers appear?
- 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?
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.
Markers are the best solutions to this. A working example of how to use them can be found in the plugin example code in
You should be using Markers.
An example derived from "The Java Developer's Guide to Eclipse" follows:
<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");
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)
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.
Markers are represented in the UI as annotations but Eclipse adds and removes the annotations itself. With direct annotations, you're in control.