Friday, 13 March 2015

Eclipse Tip: Highlight Occurrences

I thought I would write a quick post about taking advantage of a useful feature in Eclipse when writing code - highlighting everywhere a selected item appears in the current file. 

This is most useful for fields, variables and parameters, but can also be used for method/function names. 

First, go to "Preferences", then either search for "annotations", or navigate through General -> Editors -> Text Editors -> Annotations. 

The two to change (for Java) are "Occurrences" and "Write Occurrences". The former will apply to any time the highlighted word is used in the file and the latter will apply to any time the highlighted word has a new value written to it. There are similar C/C++ specific types. 

I usually change "Occurrences" to something bright to make it stand out. 

"Write Occurrences" is changed in the same way. You could use the same colour, but I find it beneficial to be able to differentiate them, so use a different bright colour (just not red as that is used for errors). 

Now if you select a field/variable/etc. in the code, it will be highlighted one of the two colours you selected depending on how it is used in that line. It will also mark the space next to the scroll bar with additional occurrences. As shown below, there is an additional Write Occurrence for the field at the top of the class (circled in blue).