本文介绍了Eclipse e4 RCP SourceViewer语法着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 为了在eclipse的e4 RCP应用程序中实现语法着色,我已经创建了一个包含SourceViewer控件的部分的基本插件项目。 public class SyntaxColoringTest { / **用于创建编辑器的SourceViewer控件。 * / 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()); 返回扫描仪; } private IRule [] createRules(){显示display = Display.getCurrent(); 颜色蓝色= display.getSystemColor(SWT.COLOR_BLUE); IToken tokenA =新的令牌(新的TextAttribute(蓝色)); IToken tokenB =新的令牌(新的TextAttribute(蓝色)); patternRule = new PatternRule(<,>,tokenA,'\\',false); endOfLineRule = new EndOfLineRule(++,tokenB); 返回新的IRule [] {patternRule,endOfLineRule}; } } 当运行应用程序时,在 ++或在< > 谢谢解决方案此代码适用于我在我自己的e4编辑器之一进行测试。 您没有显示的是源视图的文档的任何设置。如果您没有设置文档,我的测试会显示您看到的行为。设置文档: IDocument doc = new Document(contents); sv.setDocument(doc); 其中内容是文件。 To implement syntax coloring in an eclipse e4 RCP application, I have created a basic plugin project with a Part including a SourceViewer control.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()); }}Being TestSourceViewerConf as follows: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}; }}When running the application nothing is colored when typing after "++ " or in between < >Thanks 解决方案 This code works for me testing in one of my own e4 editors.What you haven't shown is any set up of the document for the source viewer. If you don't set a document my test shows the behavior you are seeing. Set the document with:IDocument doc = new Document(contents);sv.setDocument(doc);where contents is the initial contents of the document. 这篇关于Eclipse e4 RCP SourceViewer语法着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-12 22:47