Highlighting multiple instances of a token CodeDocument?


#1

When a user selects/hightlights a word, I would like my code editor to highlight all instances of that word. It's a pretty common feature in today's editors. Any ideas how I might do this, or if it is already possible? 


#2

I got this to work(see screen below). In case anyone is interested here’s what I did…

I populate a simple array with positional info about every word that matches the selected one. I store the line number, and where within that line the word appears. In my paint routine, I iterate through this array. To convert the line number to a valid Y position, I multiply it by the getLineHeight(). To convert the character index to a valid X position I multiple the index by getFont().getStringWidth(" "). Having the highlighted regions move up and down with the text when you scroll was a little tricky, but look at how Jules does this for the line numbers in CodeEditorComponent::GutterComponent and you’ll see how it can be done.