Eclipse e4 RCP SourceViewer语法着色

时间:2016-06-14 10:59:36

标签: eclipse eclipse-rcp syntax-highlighting text-editor e4

为了在eclipse e4 RCP应用程序中实现语法着色,我创建了一个基本的插件项目,其中包含一个包含SourceViewer控件的Part。

public class SyntaxColoringTest {

    /** The SourceViewer control to create the editor. */
    public SourceViewer sv = null;

    @Inject
    public SyntaxColoringTest() {
    }

    @PostConstruct
    public void postConstruct(Composite parent) {
            IVerticalRuler  verticalRuler = new VerticalRuler(10);
            OverviewRuler overviewRuler = new OverviewRuler(null, 20, null);
            sv = new SourceViewer(parent, verticalRuler, overviewRuler, true, SWT.MULTI | SWT.V_SCROLL |SWT.H_SCROLL);
            sv.configure(new TestSourceViewerConf());
    }
}

如下所示成为TestSourceViewerConf:

public class TestSourceViewerConf extends SourceViewerConfiguration {
    public ITokenScanner tokenScanner;
    public IRule patternRule;
    public IRule endOfLineRule;

    public TestSourceViewerConf(){
        tokenScanner = createTokenScanner();
    }
    public IPresentationReconciler getPresentationReconciler(ISourceViewer viewer) {
         PresentationReconciler reconciler= new PresentationReconciler();
         DefaultDamagerRepairer defDamagerRepairer= new DefaultDamagerRepairer(tokenScanner);
         reconciler.setDamager(defDamagerRepairer, IDocument.DEFAULT_CONTENT_TYPE);
         reconciler.setRepairer(defDamagerRepairer, IDocument.DEFAULT_CONTENT_TYPE);
         return reconciler;
    }
    private ITokenScanner createTokenScanner() {
         RuleBasedScanner scanner= new RuleBasedScanner();
         scanner.setRules(createRules());
         return scanner;
    }
    private IRule[] createRules() {
         Display display = Display.getCurrent();
         Color blue = display.getSystemColor(SWT.COLOR_BLUE);
         IToken tokenA= new Token(new TextAttribute(blue));
         IToken tokenB= new Token(new TextAttribute(blue));
         patternRule= new PatternRule("<", ">", tokenA, '\\', false);
         endOfLineRule = new EndOfLineRule("++ ", tokenB);
         return new IRule[] {patternRule, endOfLineRule};
    }
}

运行应用程序时,在&#34; ++&#34;之后输入时没有任何颜色。或者在&之间&GT; 感谢

1 个答案:

答案 0 :(得分:2)

此代码适用于我在我自己的e4编辑器中进行测试。

您未显示的内容是源查看器的任何文档设置。如果您没有设置文档,我的测试会显示您所看到的行为。使用以下命令设置文档:

IDocument doc = new Document(contents);

sv.setDocument(doc);

其中contents是文档的初始内容。