在public class XMLPartitionScanner中可以添加一些规则以保证对“//”和“/*....*/”做出相应。做起来很简单,只是应该明白其中的运行原理。本人经过实验可以达到最终的结果,但是对于原理不是很清楚。这次主要说明做法,原理下次待我研究研究再说。
1. 在public class XMLPartitionScanner中添加两个字符串常量,作为单行注释与多行注释的标识:
public class XMLPartitionScanner extends RuleBasedPartitionScanner {
public final static String XML_SINGLELINE_COMMENT = "__xml_singleline_comment";
public final static String XML_MULTILINE_COMMENT = "__xml_multiline_comment";
然后添加IToken:
IToken xmlSingleLineComment = new Token(XML_SINGLELINE_COMMENT);
IToken xmlMultiLineComment = new Token(XML_MULTILINE_COMMENT);
最后为他们添加规则:
rules[0] = new MultiLineRule("//", "\n", xmlSingleLineComment);
rules[1] = new MultiLineRule("/*", "*/", xmlMultiLineComment);
2. 在public class XMLDocumentProvider中添加testeditor.editors.XMLPartitionScanner:
new String[] {
XMLPartitionScanner.XML_TAG,
XMLPartitionScanner.XML_SINGLELINE_COMMENT,
XMLPartitionScanner.XML_MULTILINE_COMMENT
3. 在public class XMLConfiguration的getConfiguredContentTypes(ISourceViewer sourceViewer)中添加返回字符串:
XMLPartitionScanner.XML_SINGLELINE_COMMENT,
XMLPartitionScanner.XML_MULTILINE_COMMENT,
在getPresentationReconciler(ISourceViewer sourceViewer)中添加响应:
reconciler.setDamager(ndr, XMLPartitionScanner.XML_MULTILINE_COMMENT);
reconciler.setRepairer(ndr, XMLPartitionScanner.XML_MULTILINE_COMMENT);
reconciler.setDamager(ndr, XMLPartitionScanner.XML_SINGLELINE_COMMENT);
reconciler.setRepairer(ndr, XMLPartitionScanner.XML_SINGLELINE_COMMENT);
4. 最后run as eclipse application,new一个file,后缀.xml,open with sample xml editor即可进行调试。