Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 IToken with a "wriggly underline" TextAttribute instead of the normal one, or is there a specific mechanism for marking syntax errors?
  • 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?
  • 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.

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

like image 678
Michael Borgwardt Avatar asked Dec 30 '22 06:12

Michael Borgwardt


1 Answers

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");
like image 150
Michael Rutherfurd Avatar answered Jan 13 '23 15:01

Michael Rutherfurd