【问题标题】:Eclipse IDE - Costum language syntax highlighting through generic text editor extension pointsEclipse IDE - 通过通用文本编辑器扩展点突出显示 Costum 语言语法
【发布时间】:2018-01-15 22:02:06
【问题描述】:

我一直在尝试为 Eclipse IDE 编写一个插件,为自定义通用语言执行语法突出显示和代码完成。

在氧气项目版本 (v4.7) 更改说明中,使用扩展点宣布了一种据称是使用通用文本编辑器并使用所需功能扩展它的新且简单的方法。甚至提供了代码sn-ps:

有了这个新的编辑器,现在可以更容易地丰富一个新的通用编辑器,这样您就可以相对容易地添加对新语言的支持。它重用了现有的 Eclipse 编辑器基础结构,但使用通用编辑器,您不需要实现编辑器来为新的文件内容类型提供功能。相反,您通过扩展点使通用编辑器更智能。 以下示例展示了如何通过扩展为通用编辑器贡献功能:

<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>

这些新的扩展点接收常规平台类(IPresentationReconcilier、ITextHover、ICompletionProposalComputer)作为参数,以向通用编辑器添加行为。不需要新的 Java API。

这是一个添加一些最小 Gradle 语法高亮支持的简单示例:

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);
    }

}

(原文来源:https://www.eclipse.org/eclipse/news/4.7/M3/#generic-editor

我已经成功地打开了一个新的插件项目,它已经实现了通用文本编辑器。自动生成的 plugin.xml 文件已经包含上面引用的第一个代码块(即扩展点定义,如果我没记错的话)。

我对 Eclipse 插件真的很陌生,对 Java 也不是很坚定。所以我一直无法弄清楚将第二个代码块中的代码放在哪里,以便在编辑器中实际实现更改以及如何将其与扩展点连接。

非常感谢任何指向方向的指针,或指向一些进一步阅读(理想情况下非常基础)或白痴级别教程的链接! 谢谢。

【问题讨论】:

    标签: java eclipse eclipse-plugin


    【解决方案1】:

    Eclipse 发行说明中的​​示例代码类似于此展示插件中的GradlePresentationReconcilerhttps://github.com/vogellacompany/codeexamples-ide/tree/dbfa485ca9b6f0aed653b3a466c055e24b01bb90/com.vogella.ide.editor.gradle

    我得到了基于展示的语法高亮部分。

    【讨论】:

      【解决方案2】:

      这是一个相关的博客文章,其中还包括一个幻灯片共享平台: Generic editor & language servers

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-23
        • 1970-01-01
        • 2011-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-01
        相关资源
        最近更新 更多