3、编辑器的语法颜色功能
(1)基本概念
l 首先是定义各种Partition(文档中不重叠的文本),用以区分语法明显不同的部分
l 文档中的每个字符必须属于定义得某个Partition或缺省Partition(IDocument.DEFAULT_CONTENT_TYPE)
l 注释应该属于独立的一种Partition
l 由于log4j.properties中除了注释,每行多是name=value形式,所以分成3种Partition
Ø 注释(以#开始)
Ø Value值(包括=)
Ø 缺省Partition(包括name)
l 需要提供一个Partition扫描器供TextEditor调用,来决定哪些文本属于哪个Partition
l 还需要提供Token扫描器供TextEditor调用
l Token是用来设置颜色的最小文本单元
l 每个Partition需要有唯一的Token扫描器
(2)Partition扫描器
l Partition扫描器通过提供的解析规则(定义Partition)进行解析,将文本区分成不同的Partition
l 本例使用JFace提供的基于规则的扫描器,所以需要在plugin.xml清单编辑器的Dependencies页中添加org.eclipse.jface.text插件
package org.xqtu.log4j.editor; import org.eclipse.jface.text.IDocument;import org.eclipse.jface.text.rules.IPredicateRule;import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;import org.eclipse.jface.text.rules.SingleLineRule;import org.eclipse.jface.text.rules.Token; public class PropertiesPartitionScanner extends RuleBasedPartitionScanner {
public final static String LOG4J_COMMENT = "__log4j_comment";
public final static String LOG4J_VALUE = "__log4j_value"; public PropertiesPartitionScanner() { super(); Token commentPartition = new Token(LOG4J_COMMENT);
Token valuePartition = new Token(LOG4J_VALUE);
SingleLineRule commentRule = new SingleLineRule("#", null,
commentPartition, (char) 0, true);
commentRule.setColumnConstraint(0);
SingleLineRule valueRule = new SingleLineRule("=", null, valuePartition, (char) 0, true);setPredicateRules(new IPredicateRule[] { commentRule, valueRule });
} public static String[] getLegalContentTypes() {
return new String[] { IDocument.DEFAULT_CONTENT_TYPE, PropertiesPartitionScanner.LOG4J_COMMENT, PropertiesPartitionScanner.LOG4J_VALUE }; }}上一页 下一页






