Eclipse IDE - Costum language syntax highlighting through generic text editor extension points

Viewed 1385

I've been trying to write a plugin for the Eclipse IDE that does syntax highlighting and code completion for a custom generic language.

In the oxygen project version (v4.7) change notes a supposedly new and easy way to use a generic text editor and extend it with the wanted features is announced using extension points. Even code snippets are provided:

With this new editor it is now much easier to enrich a new generic editor so you can add support relatively easy for new languages. It is reusing the existing Eclipse editor infrastructure but with the generic editor you don't need to implement an editor to supply functionality for a new file content-type. Instead you make the generic editor smarter by extension points. The following example shows how to contribute features to the generic editor via extensions:

<extension point="org.eclipse.ui.genericeditor.contentAssistProcessors">
   <contentAssistProcessor
         class="org.eclipse.ui.genericeditor.examples.dotproject.NaturesAndProjectsContentAssistProcessor"
         contentType="org.eclipse.ui.genericeditor.examples.dotproject">
   </contentAssistProcessor>
</extension>
<extension point="org.eclipse.ui.genericeditor.hoverProviders">
   <hoverProvider
         class="org.eclipse.ui.genericeditor.examples.dotproject.NatureLabelHoverProvider"
         contentType="org.eclipse.ui.genericeditor.examples.dotproject"
         id="natureLabelHoverProvider">
   </hoverProvider>
</extension>
<extension point="org.eclipse.ui.genericeditor.presentationReconcilers">
   <presentationReconciler
         class="org.eclipse.ui.genericeditor.examples.dotproject.BlueTagsPresentationReconciler"
         contentType="org.eclipse.ui.genericeditor.examples.dotproject">
   </presentationReconciler>
</extension>

Those new extension points receive as arguments regular Platform classes (IPresentationReconcilier, ITextHover, ICompletionProposalComputer) to add behavior to the generic editor. No new Java API is necessary.

Here is a simple example of adding some minimal Gradle syntax highlighting support:

public class GradlePR extends PresentationReconciler {

    private IToken quoteToken = new Token(new TextAttribute(new Color(Display.getCurrent(), new RGB(139, 69, 19))));
    private IToken numberToken = new Token(new TextAttribute(new Color(Display.getCurrent(), new RGB(0, 0, 255))));
    private IToken commentToken = new Token(new TextAttribute(new Color(Display.getCurrent(), new RGB(0, 100, 0))));

    public GradlePR() {
        RuleBasedScanner scanner = new RuleBasedScanner();

        IRule[] rules = new IRule[5];
        rules[0] = new SingleLineRule("'", "'", quoteToken);
        rules[1] = new SingleLineRule("\"","\"", quoteToken);
        rules[2] = new PatternRule("//", null, commentToken, (char)0, true);
        rules[3] = new NumberRule(numberToken);

        rules[4] = new GradleWordRule();

        scanner.setRules(rules);

        DefaultDamagerRepairer dr = new DefaultDamagerRepairer(scanner);
        this.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
        this.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
    }

}

(original source: https://www.eclipse.org/eclipse/news/4.7/M3/#generic-editor)

I've managed to open a new Plug-in Project with the generic text editor already implemented. The automatically generated plugin.xml file already contains the first code block quoted above (i.e. the extension point definition if I'm not mistaken).

I'm really new to Eclipse plugins and not very firm on Java neither. So I haven't been able to figure out where to put the code from the second code block so as to actually implement the changes in the editor and how to connect it with the extension points.

Any pointers in a direction, or links to some further reading (ideally very basic) or an idiot's level tutorial is much appreciated! Thanks.

2 Answers
Related