Eclipse开发 - TextEditor中高亮匹配的括号

先看效果:


Eclipse开发 - TextEditor中高亮匹配的括号_第1张图片
括号匹配效果图

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.

你可能感兴趣的:(Eclipse开发 - TextEditor中高亮匹配的括号)