先看效果:
TextEditor提供对文本装饰的方法configureSourceViewerDecorationSupport, 在此方法中为SourceViewerDecorationSupport设置ICharacterPairMatcher. 最终由PairMatcher提供字符串的匹配.
首先在TextEditor中定义两个constraints:
public final static String EDITOR_MATCHING_BRACKETS = "matchingBrackets";
public final static String EDITOR_MATCHING_BRACKETS_COLOR= "matchingBracketsColor";
public final static char[] BRACKETS_CHARS = {'(', ')', '[', ']'};
之后override TextEditor中的configureSourceViewerDecorationSupport方法:
@Override
protected void configureSourceViewerDecorationSupport(SourceViewerDecorationSupport support) {
super.configureSourceViewerDecorationSupport(support);
ICharacterPairMatcher matcher = new DefaultCharacterPairMatcher(BRACKETS_CHARS,
IDocumentExtension3.DEFAULT_PARTITIONING);
support.setCharacterPairMatcher(matcher);
support.setMatchingCharacterPainterPreferenceKeys(EDITOR_MATCHING_BRACKETS,EDITOR_MATCHING_BRACKETS_COLOR);
//Enable bracket highlighting in the preference store
IPreferenceStore store = getPreferenceStore();
store.setDefault(EDITOR_MATCHING_BRACKETS, true);
store.setDefault(EDITOR_MATCHING_BRACKETS_COLOR, "128,128,128");
}
Ok, Done.