我一直试图为Eclipse编写一个插件,用于为自定义泛型语言进行语法突出显示和代码完成。
在氧气项目版本(v4.7)中,change说明了使用泛型文本编辑器并使用想要的特性扩展它的一种新的、简单的方法。甚至提供了代码片段:
使用这个新编辑器,现在可以更容易地充实一个新的通用编辑器,因此您可以为新语言添加相对容易的支持。它正在重用现有的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。 下面是添加一些最低级别语法突出显示支持的简单示例:
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上也不是很坚定。因此,我还无法找到将第二个代码块中的代码放在哪里,以便实际实现编辑器中的更改,以及如何将其与扩展点连接起来。
任何方向的指针,或链接到一些进一步的阅读(理想是非常基本的)或白痴的水平教程,是非常感谢的!谢谢。
发布于 2019-01-09 13:56:00
这里有一个相关的博客文章,其中还包括一个幻灯片共享平台:通用编辑器和语言服务器。
发布于 2019-03-20 23:57:19
Eclipse发行说明中的示例代码与这个展示插件中的GradlePresentationReconciler相似:https://github.com/vogellacompany/codeexamples-ide/tree/dbfa485ca9b6f0aed653b3a466c055e24b01bb90/com.vogella.ide.editor.gradle
我得到的语法突出部分工作的基础上的展示。
https://stackoverflow.com/questions/45566533
复制相似问题