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

先看效果:


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

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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,549评论 0 17
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,776评论 0 33
  • 今晚同圆坡聊天,她聊到她大爸以前担她去烧窑的那,一路边走边笑,让她很幸福,还有小时候半夜给她把尿,,,,,
    x22222阅读 235评论 0 0