Gangmax Blog

How to "highlight matching brackets" for your custom editor in Eclipse

This implementation comes from here. The code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public final static String EDITOR_MATCHING_BRACKETS = "matchingBrackets";
public final static String EDITOR_MATCHING_BRACKETS_COLOR= "matchingBracketsColor";

@Override
protected void configureSourceViewerDecorationSupport (SourceViewerDecorationSupport support) {
super.configureSourceViewerDecorationSupport(support);

char[] matchChars = {'(', ')', '[', ']'}; //which brackets to match
ICharacterPairMatcher matcher = new DefaultCharacterPairMatcher(matchChars ,
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");
}

The effect looks like this:

highlight_matching_brackets

Comments