ActiveState Community

highlight variable

Posted by tone77 on 2008-05-21 18:16
OS: All / Any

I would like to create an extension that would highlight all occurrences of the variable currently under the cursor. So if I had the following code, and the cursor was on $test, all the $test variables would be highlighted.

$test = 1 + 2;
$temp = $test * 2;
echo $test;

I would greatly appreciate any pointers anyone out there could give me.

ericp | Thu, 2008-05-22 10:00

First, you can't easily bold arbitrary sequences of text.
We assign each syntactic element a style number (e.g., for
PHP variables get value 48, operators 46, strings 43, and
these values are subject to change). We then map a set
of styles to each (language number, style number) tuple.
So you can't override some variables.

However you can apply an indicator to them, from this list:

val INDIC_PLAIN=0
val INDIC_SQUIGGLE=1
val INDIC_TT=2
val INDIC_DIAGONAL=3
val INDIC_STRIKE=4
val INDIC_HIDDEN=5
val INDIC_BOX=6
val INDIC_ROUNDBOX=7

There's no bold item, but you can specify the color of the indicator.
See the entries in http://grok.openkomodo.com/source/xref/openkomodo/trunk/contrib/scintill... that start with
"Indic" for an idea of the indicator API.

Next, have a look at the various event handlers in
http://grok.openkomodo.com/source/xref/openkomodo/trunk/src/chrome/komod...

In particular, you need to identify which events make sense to track to update
which parts of the current buffer should be indicated. The onPosChanged event
would be a good candidate, except currently we don't pass those events on to
Komodo for optimization reasons. You can always modify the 'ifdef' to 'ifndef'
at http://grok.openkomodo.com/source/xref/openkomodo/trunk/src/SciMoz/nsSci...
of OpenKomodo and build your own if you're up to it.

If you'd rather write an extension for stock Komodo IDE/Edit, I would hook on
both the onModified and onDwellEnd events. For an example of a hook, see my
undoslider extension at
http://community.activestate.com/komodo-extension/komodo-undoslider.
Look for the addModifiedHandler method in undoslider.js

Good luck and have fun.

- Eric

tone77 | Thu, 2008-05-22 11:52

Eric, thanks for the great tips.

ericp | Thu, 2008-05-22 11:57

If you modify nsSciMoz.cxx, you want to do both parts, in
the ifdef and the else-part.