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,915評論 18 139
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,765評論 18 399
  • ¥開啟¥ 【iAPP實現進入界面執行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,511評論 0 17
  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,769評論 0 33
  • 今晚同圓坡聊天,她聊到她大爸以前擔她去燒窯的那,一路邊走邊笑,讓她很幸福,還有小時候半夜給她把尿,,,,,
    x22222閱讀 235評論 0 0