Eclipse RCP开发如何为PresentationReconciler添加多个RuleBasedScanner
方案1:单Scanner多规则(优先推荐,无需修改现有结构)
如果只是要实现多规则高亮,不一定需要新增多个Scanner,同一个RuleBasedScanner支持传入多个IRule实现多规则匹配,这是更轻量化的方案,示例代码:
public class PropertiesReconciler extends PresentationReconciler { // 不同语法元素的样式定义 private final TextAttribute keyAttribute = new TextAttribute(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN)); private final TextAttribute valueAttribute = new TextAttribute(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE)); private final TextAttribute commentAttribute = new TextAttribute(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY)); public PropertiesReconciler() { RuleBasedScanner scanner = new RuleBasedScanner(); // 按优先级添加多个规则,匹配顺序从上到下 IRule[] rules = new IRule[] { new PropertyCommentRule(new Token(commentAttribute)), // 注释规则 new PropertyNameRule(new Token(keyAttribute)), // 属性名规则 new PropertyValueRule(new Token(valueAttribute)) // 属性值规则 }; scanner.setRules(rules); DefaultDamagerRepairer dr = new DefaultDamagerRepairer(scanner); this.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); this.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); } }
该方案不需要修改plugin.xml配置。
方案2:多Scanner绑定不同文档分区
如果你的编辑器做了文档分区拆分(比如注释、属性名属于不同的内容分区),可以给每个分区绑定独立的Scanner,步骤如下:
- 先实现自定义的文档分区常量,给不同内容定义专属的分区ID:
// 自定义分区常量定义 public interface IPropertiesPartitions { String PROPERTIES_COMMENT = "com.vogella.ide.editor.properties.comment"; String PROPERTIES_KEY = "com.vogella.ide.editor.properties.key"; String PROPERTIES_VALUE = "com.vogella.ide.editor.properties.value"; String[] ALL_PARTITIONS = new String[] {PROPERTIES_COMMENT, PROPERTIES_KEY, PROPERTIES_VALUE}; }
- 修改
PropertiesReconciler,给每个分区创建独立的Scanner和DamagerRepairer:
public class PropertiesReconciler extends PresentationReconciler { private final TextAttribute keyAttribute = new TextAttribute(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN)); private final TextAttribute valueAttribute = new TextAttribute(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE)); private final TextAttribute commentAttribute = new TextAttribute(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY)); public PropertiesReconciler() { // 1. 注释分区对应的Scanner RuleBasedScanner commentScanner = new RuleBasedScanner(); commentScanner.setRules(new IRule[] {new PropertyCommentRule(new Token(commentAttribute))}); DefaultDamagerRepairer commentDr = new DefaultDamagerRepairer(commentScanner); this.setDamager(commentDr, IPropertiesPartitions.PROPERTIES_COMMENT); this.setRepairer(commentDr, IPropertiesPartitions.PROPERTIES_COMMENT); // 2. 属性名分区对应的Scanner RuleBasedScanner keyScanner = new RuleBasedScanner(); keyScanner.setRules(new IRule[] {new PropertyNameRule(new Token(keyAttribute))}); DefaultDamagerRepairer keyDr = new DefaultDamagerRepairer(keyScanner); this.setDamager(keyDr, IPropertiesPartitions.PROPERTIES_KEY); this.setRepairer(keyDr, IPropertiesPartitions.PROPERTIES_KEY); // 3. 属性值分区对应的Scanner RuleBasedScanner valueScanner = new RuleBasedScanner(); valueScanner.setRules(new IRule[] {new PropertyValueRule(new Token(valueAttribute))}); DefaultDamagerRepairer valueDr = new DefaultDamagerRepairer(valueScanner); this.setDamager(valueDr, IPropertiesPartitions.PROPERTIES_VALUE); this.setRepairer(valueDr, IPropertiesPartitions.PROPERTIES_VALUE); // 兜底默认分区配置 DefaultDamagerRepairer defaultDr = new DefaultDamagerRepairer(new RuleBasedScanner()); this.setDamager(defaultDr, IDocument.DEFAULT_CONTENT_TYPE); this.setRepairer(defaultDr, IDocument.DEFAULT_CONTENT_TYPE); } }
该方案不需要修改原有plugin.xml的presentationReconcilers扩展配置,只需要确保你的编辑器文档正确绑定了自定义分区器即可。
内容的提问来源于stack exchange,提问作者steven
相关产品推荐
相关产品推荐

