美文网首页
Eclipse开发 - TextEditor中高亮匹配的括号

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

作者: lvjian700 | 来源:发表于2015-03-09 23:27 被阅读741次

    先看效果:


    括号匹配效果图括号匹配效果图

    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中高亮匹配的括号

          本文链接:https://www.haomeiwen.com/subject/tjwvxttx.html