Komodo Hacks: Find References
UPDATE: Trent Mick posted a simpler method for getting the BRM package into Komodo. I've modified my original procedure below to include his suggestion.
Komodo is my favorite Python IDE. Since ActiveState has recently released their new Komodo 4.0, I've decided to publish a full week of Komodo Toolbox commands and macros that should prove useful for Python developers. (Note that I'm primarily a Windows user, but most of these commands/macros will be able to be used on other platforms as well with little or no modification.)
I'm starting off the hacks with a "Find References" macro that compliments the new "Go To Definition" feature in the latest Komodo. The idea is to be able to highlight a variable or method in your Python code and then double-click Find References in the toolbox to see all uses of that variable/method throughout your code.
I use the Bicycle Repair Man (BRM) refactoring tool, written by Phil Dawes, to implement this macro. That means the first order of business is to install BRM into Komodo's Python directory so that the module can be called from within a macro.
How to install Bicycle Repair Man into Komodo:
The usage is pretty simple: highlight a variable or method in your code and then double-click the Find References entry in the Toolbox. The macro will scan your code for a second or two (or longer depending on the length of your code and the number of imports) and then output the results in the Command Output tab. You can then double-click on any line in the output to jump to the corresponding location in the code.
A few items of note:
Komodo is my favorite Python IDE. Since ActiveState has recently released their new Komodo 4.0, I've decided to publish a full week of Komodo Toolbox commands and macros that should prove useful for Python developers. (Note that I'm primarily a Windows user, but most of these commands/macros will be able to be used on other platforms as well with little or no modification.)
I'm starting off the hacks with a "Find References" macro that compliments the new "Go To Definition" feature in the latest Komodo. The idea is to be able to highlight a variable or method in your Python code and then double-click Find References in the toolbox to see all uses of that variable/method throughout your code.
I use the Bicycle Repair Man (BRM) refactoring tool, written by Phil Dawes, to implement this macro. That means the first order of business is to install BRM into Komodo's Python directory so that the module can be called from within a macro.
How to install Bicycle Repair Man into Komodo:
- Download the BRM nightly build. I used the bicyclerepair-nightly.tar.gz.tmp file dated October 9, 2006. Rename this file to bicyclerepair-nightly.tar. (I know it looks like a gzipped file, but trust me, it's really a TAR file.)
- Extract the build into a convenient directory. WinZip is one of the easiest Windows tools to do this. On a Unix platform, you can use tar xvf bicyclerepair-nightly.tar
- Put a "bike.pth" text file in [komodo-install-dir]/lib/mozilla/python. This file should contain the path to the bicyclerepair source directory. For example, if you had extracted the bicyclerepair folder to the root of your C: drive, your "bike.pth" would contain the single line "c:\bicyclerepair". (Again, I'm sure the Unix users can figure out how this would look on their system.)
- Re-start Komodo for it to pick up on the new .pth file.
The usage is pretty simple: highlight a variable or method in your code and then double-click the Find References entry in the Toolbox. The macro will scan your code for a second or two (or longer depending on the length of your code and the number of imports) and then output the results in the Command Output tab. You can then double-click on any line in the output to jump to the corresponding location in the code.
A few items of note:
- Find References may not work like you expect in certain cases; for example, when finding references for a class instance variable like 'self.foo', just highlight 'foo' - don't include the 'self.' prefix.
- Due to a limitation of Komodo's Python macros, Find References has to use a 'helper' Run Command to populate the Command Output tab. I placed this Run Command inside a separate 'Helper Commands' folder to keep it out of the way; make sure you don't change it or delete it by mistake.
Labels: hacks, ide, komodo, python, refactoring

7 Comments:
Hi - just a heads up, apparently you need to have Python 2.4 installed for this to work. I've set the required path as you show here, but Windows complains that it can't find python24.dll.
By
sean, At
12:10 AM
Thanks for the heads up. I do have another install of Python 2.4 on my machine, but I didn't realize it may have been a necessary dependency for this hack to work. I'll look at it some more.
By
Brandon Corfman, At
8:19 AM
Brandon,
Very cool. Any easier way to handle getting the "bike" packages on Komodo's Python path might be to suggest that people us a ".pth" file as follows:
1. Unpack the bicyclerepair source somewhere (in my case "C:\trentm\src\bicyclerepair").
2. Put a "bike.pth" file somewhere on Komodo's PythonPath that contains the path to the bicyclerepair source dir. A good place in "(komodo-install-dir)/lib/mozilla/python/bike.pth". (In my case 'bike.pth' would have one line: "C:\trentm\src\bicyclerepair").
3. Re-start Komodo for it to pick up on the new .pth file.
By
Trent Mick, At
12:28 PM
Thanks, Trent, I'll try that ... I have more BRM macros to share, so that will be helpful.
By
Brandon Corfman, At
2:17 PM
This is a very useful macro, but you will need to edit it for it to work on Linux or OS X. Right click on the macro and select "Properties" to edit it and towards the bottom of the macro will be the line:
cmd = "type %s" % path
which needs to be changed to:
cmd = "cat %s" % path
By
David Mellor, At
4:38 PM
I was trying to figure out why this wasn't working and I looked in findreferences.kpz. I see your importing xpcom. What about people who don't have xpcom?
mmmwww
By
Anonymous, At
1:58 PM
XPCOM is built into the Komodo install, so you should have it on your system.
By
Brandon Corfman, At
10:12 PM
Post a Comment
<< Home