gpt4 book ai didi

java - Eclipse IDE - 通过通用文本编辑器扩展点突出显示 Costum 语言语法

转载 作者:行者123 更新时间:2023-12-01 19:33:57 24 4
gpt4 key购买 nike

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

在 oxygen 项目版本 (v4.7) 中,更改说明使用扩展点宣布了一种使用通用文本编辑器并使用所需功能扩展它的新方法和简单方法。甚至提供了代码片段:

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

}

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

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

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

非常感谢任何指向某个方向的指针,或指向一些进一步阅读(最好是非常基础的)或白痴级别教程的链接!谢谢。

最佳答案

这是一篇相关的博文,其中还包含一个幻灯片分享平台: Generic editor & language servers

关于java - Eclipse IDE - 通过通用文本编辑器扩展点突出显示 Costum 语言语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45566533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com