Tor Norbye's Weblog
"Creator" on this page refers to Sun Java Studio Creator.
« Ruby Screenshot of... | Main | Ruby Screenshot of... »

20070605 Tuesday June 05, 2007

Ruby Screenshot of the Week #13: Refactoring

As of this evening there are two new features in the builds: Find Usages, and Rename. Let's start with Find Usages. Right click on a symbol and choose "Find Usages" (or use the keyboard shortcut). Let's say I search for the usages of the UserMail class in the Mephisto Rails blogging application (click for full size):

This feature also knows about .rhtml and .erb files - it parses the embedded Ruby and analyzes it. Here's an example where I've searched for the usages of a @comments field in a controller - notice the .rhtml matches:

(Sorry, I know these images might be a bit large on some screens; I should have taken the screenshots at a smaller screen resolution than my usual 1680x1050.)

In the Find usages dialog I can also ask NetBeans to find subtypes of the class rather than usages. Here's an example of what I get if I search for subclasses of the ApplicationController class:

Next, Rename refactoring. Let's say that I want to rename the @comments field in my Rails application controller. I right click on it, choose a new name and hit OK. I then click "Preview", and in the bottom window I get a list of refactoring operations, along with diffs for the currently selected item. I can (and should!) walk through the changes with the Up/Down arrows, and I can unselect any changes that I don't like before I click the Refactor button to apply the changes. Click on the image for full size (I'm showing both the dialog and the results window here; in reality you'd first get the dialog, and when you hit Preview it disappears and you see the bottom window.)

Again, notice how the renaming operation includes changes in .rhtml files. The advantage of this approach over a regular Search/Replace editor operation is that by using parse trees, we have a lot more confidence in the matches. The IDE will not confuse a local variable reference to foo with a method named foo. It does however still have difficulties knowing whether symbols and method names that occur in multiple places refer to say the same method, so at this point it errs on the side of optimism and presents them all as potential uses.

WARNING: This feature is definitely preliminary!! Hopefully the bold red warning in both the Rename and Find Usages dialog makes this really clear. The Refactor button will be disabled and Preview required shortly.

However, this feature should be improving rapidly as I get feedback and implement some more things on my todo-list. Find Usages at least should be quite useful as a navigation tool. If you're going to use the Rename feature, please make a backup of your code first!

(This feature requires the very latest builds - try the Ruby IDE from nbextras.org, or for fuller instructions, see the wiki page. Auto updating from M9 isn't working any more because I'm relying on private APIs that have changed incompatibly.)

P.S. A great big thank you to all of you who have tried the NetBeans Ruby support, and especially to those of you who have provided feedback!

(2007-06-05 19:48:27.0) Permalink Comments [32]

Comments:

Looks good. A big thanks.

Posted by hemant kumar on June 05, 2007 at 11:01 PM PDT #

Just curious. Would appreciate an honest reply. Are you adding rails support to Netbeans to make rails development better? Or to make more rails developers use Netbeans?

Posted by Dushyanth Tiwari on June 06, 2007 at 06:31 AM PDT #

Ruby 'find usages' is a killer app. Good luck!

Posted by Greg on June 06, 2007 at 10:20 AM PDT #

Hi Tor - can I ask for a quick update re how the installation support for setting up the equivalent TextMate shortcuts/bundle support is going? Last I heard there were still some work to be done to get this occurring properly through the update mechanism?
Refactoring support is looking great...looking forward to having some time to test/play. Cheers GregH

Posted by GregH on June 06, 2007 at 01:19 PM PDT #

Nice walk through. I went ahead and tagged it under ruby on tektag.com for future reference.

Posted by Tim on June 06, 2007 at 01:48 PM PDT #

Hi, Tor! Excellent (as usual) work! one question - is it possible to change the font-size of a help window (which appears during autocompletion with samples of the ruby code inside) ?

Posted by freeman on June 06, 2007 at 06:52 PM PDT #

@Dushyanth: I would believe the response is both. Why would you need an 'or' ?

Posted by Decklane on June 08, 2007 at 04:20 AM PDT #

Tor, could you please teach NetBeans DRYML for Hobo: http://hobocentral.net/ ? They've already got some TextMate support and looks like someone may be working on RadRails. It would definitely be good to have.

Posted by JohnH on June 08, 2007 at 02:02 PM PDT #

Nice work. Netbeans is shaping up to be a very good Ruby development environment

Posted by Dave on June 08, 2007 at 02:42 PM PDT #

Hi All, thanks for your comments (and sorry about the delay, I'm a bit behind on my e-mail).

Dushyanth - it's both. This is not some conspiracy to try to get Ruby developers to come to a Java IDE and maybe switch to Java. Instead, NetBeans is branching out as a tool and supporting other languages. (We've already had a C++ IDE for Solaris based on it so it's not really true that it's been just a Java IDE, but that's how a lot of people think of it). Sun is doing this because we want to have solutions people are looking for. Ruby runs very well on a big Solaris server... certainly better than ASP.NET :-)

Hi Greg (sorry about the comment spam trouble - the spam filter seems a bit overly aggressive, but it's a sitewide thing I have no control over). Somebody is looking into the posibility of including these snippets in the product by default, which would be a better solution than bundling an importer. I'll ping them and see how it's proceeding.

Freeman, I dont know of a way to change the help window font. But one thing I could do which might be nice is add a property which lets you specify your own stylesheet to be used for that HTML area. I'll post something here when I get that done.

JohnH, DRYML looks interesting. Of course there are also requests for other additional languages, like HAML. It's hard to know how to prioritize this work :) DRYML however looks very ERB/RHTML-like. Perhaps we can get pretty far by just assigning a flavor of RHTML-treatment to .dryml files?

Posted by Tor Norbye's Weblog on June 09, 2007 at 07:56 AM PDT #

Yes, that may be possible. If you look at the forums on the HoboCentral site you'll see a lot of the footwork already done. For example, you can see how they're doing it for TextMate right here: http://hobocentral.net/forum/viewtopic.php?t=2328

Perhaps downloading Niko's bundle from subversion will give you all that's needed to have it going in just a few minutes?

Posted by JohnH on June 09, 2007 at 08:57 AM PDT #

Hi JohnH, give build #1830 a shot (http://deadlock.nbextras.org/hudson/job/ruby). DRYML files now get RHTML treatment. TODO - handle the non-html tags (like <:attribute> tags) better and improve completion etc.

Posted by Tor Norbye's Weblog on June 09, 2007 at 12:54 PM PDT #

Tor, I've got a question about an older feature. I'm trying to import TextMate bundles using the nbm you built. I imported it, and when I go to Edit | Source | Show Templates | Import, I see the .tmbundle file type option, but when I try to import a .tmbundle file I get XML parsing errors: 'Content is not allowed in prolog', etc. It seems like it's trying to parse it as xml. I'm using the lastest ruby only mac os net beans zip. Steve

Posted by Stephen Molitor on June 10, 2007 at 08:32 AM PDT #

Stephen, the importer imports two kinds of abbreviations: NetBeans abbreviation files (XML) and TextMate bundle files (.tmbundle). It sounds like it's trying to import something as a NetBeans abbreviation file. Can you make sure that you've changed the filetype filter in the filechooser to the TM bundle type, and you have chosen the "whatever.tmbundle" file as the file to be imported? (If you choose any other file, like as specific snippet file, it's probably not doing the right thing).

Posted by Tor Norbye's Weblog on June 10, 2007 at 03:09 PM PDT #

Yeah, I'm selecting 'TextMate Bundles (*.tmbundle) as the File Format, and I tried importing my local Ruby.tmbundle and Rails.tmbundle, among others, with no luck. Here's a sample of the errors message I get importing Ruby.tmbundle: IMPORT SUMMARY ------------------------------ Parsing error in "#!;usr;local;bin;ruby -w.plist"; skipping Content is not allowed in prolog.Parsing error in "010 ruby for element in collection.plist"; skipping Content is not allowed in prolog.Parsing error in "020 ruby inject.plist"; skipping Content is not allowed in prolog.Parsing error in "040 ruby reject.plist"; skipping

Posted by Stephen Molitor on June 10, 2007 at 05:36 PM PDT #

Interesting. It sounds like your snippet files have a different format than the once I had and made work. Would you mind zipping up your .tmbundle directory and mailing it to me? tor.norbye@sun.com

Posted by Tor Norbye's Weblog on June 10, 2007 at 07:05 PM PDT #

Hi Tor! I cannot find "soft word wrapping possibility"- is it in NetBeans? JEdit has good one - when it wraps strings, then each wrapped string begins from the indention that "parent" string has. It would just great to has such in NetBeans

Posted by freeman on June 10, 2007 at 09:47 PM PDT #

Tor, since version 1849 code templates don't work, what can it be?

Posted by freeman on June 12, 2007 at 02:19 AM PDT #

I noticed that myself and pinged the editor developers; apparently there was a snafu and they said it's been fixed. The good news is that the abbreviations support is being rewritten so there will be some good enhancements; for example the DTD is being changed such that templates can have display names which we can use for the Surround With quick tip etc.

Try a very recent build, and please let me know if it's still not working and I'll pass the word on.

Posted by Tor Norbye's Weblog on June 12, 2007 at 06:51 AM PDT #

"This feature also knows about .rhtml and .erb files - it parses the embedded Ruby and analyzes it. " Does it know about .mab files? :)

Posted by Jerrett on June 12, 2007 at 12:21 PM PDT #

Hi Jerrett, Yes, it knows about .mab files; these are treated as Ruby files (which they are, as far as I can understand). Thus, "normal" code completion (and go to declaration, and indentation, and bracket matching etc. etc.) should just work. One area we could improve in though is to teach code completion about the context the .mab file will be executed in, such that the inherited methods ("html", "p", etc.) show up in code completion. That's not done. If you know (or know where I can find) information on what modules or classes are included in the context when a .mab file is running that would help. I recently did something similar for .builder files (teaching code completion what the "xml" object refers to) and .rjs files (for the "page" object).

Posted by Tor Norbye's Weblog on June 12, 2007 at 01:11 PM PDT #

Tor, I would like to thank you for your work - great stuff. I have been seeing netbeans+rails buzz on the web and today I finally gave it a shot. I would have to say that so far everything just works! I started out witht the full boat netbeans download (160+ MB) and then went with your ide only build - I hope this is an option when this gets released thanks again!

Posted by tom fowler on June 12, 2007 at 06:36 PM PDT #

Now code templates work again, thank. Another tip - if there is a lot of migration in the project, than all of them cannot be located on the screen, so it's impossible to select (or scroll) the latest migrations (at least on my 12800x800 screen i can see only 40 of them)

Posted by freeman on June 12, 2007 at 06:54 PM PDT #

I still can't get importing of TextMate bundles to work. I downloaded build 1951, running on Mac OS X.

Posted by Stephen Molitor on June 13, 2007 at 08:43 AM PDT #

Hi Steve, Thanks for sending the snippets; it made it obvious what the problem is. All of your snippets are in a binary plist format. Apparently, Apple switched to this recently (for performance reasons). The snippets I had played with were all in the XML format. Rather than figuring out the binary format, I've updated the importer to detect this situation, and when it does, the import log looks like follows which hopefully explains what needs to be done:
IMPORT SUMMARY
------------------------------

Binary plist files!

Your snippets are in a binary plist format.
To be imported, they must be in XML format.

First make a backup of your bundle tree (zip -r bundle.zip whatever.bundle)
and then run "plutil -convert xml1 snippet.plist" to convert each file.
To perform this for a directory tree, use this:

find . -name "*.plist" -exec plutil -convert xml1 {} \;

(To change back, replace -convert xml1 with -convert binary1

Imported 0 snippets.
Skipped 0 snippets.
By the way, we're still looking into bundling a lot of these with the product so you won't need to import if you're just using the builtins.

Posted by Tor Norbye's Weblog on June 13, 2007 at 01:57 PM PDT #

Tor, great stuff, I just picked up the latest build with rails plugin support....fantastic! Sorry if this is the wrong place/process ... but what are the chances that support for "script/console" will be implemented? thanks again..

Posted by tom fowler on June 13, 2007 at 03:57 PM PDT #

Tor, Thanks for figuring out the binary plist problem. I guess I could have figured that out. Thanks for the incredibly quick responses! This Ruby NetBeans thing is really shaping up. Keep up the fantastic work! Steve

Posted by Stephen Molitor on June 13, 2007 at 06:50 PM PDT #

Okay, the TextMate snippets are all included by default now, so no more need to import by hand. (Although this week probably isn't the best week to grab builds; it's feature freeze tomorrow so there's a LOT of integrations happening all over the code base... But as of next week it will be bugfixing and stability work only, so things should get better.

Regarding Rails console - I just took a stab at implementing it - and I integrated in disabled form because of a blocking bug in the output window (106741). Hopefully somebody will evaluate it quickly.

Posted by Tor Norbye on June 14, 2007 at 08:59 PM PDT #

This is very cool and very promising! Excellent! You are worthy of your namesake ;)

Posted by Jan Wikholm on June 15, 2007 at 12:58 AM PDT #

Hey Tor, thanks for that. DRYML is, to me, one of the most exciting things for Rails that I've seen in a long time. It looks like they've got a lot in store for Hobo as time goes forward so this is definitely a positive move!

Posted by JohnH on June 15, 2007 at 12:44 PM PDT #

"It does however still have difficulties knowing whether symbols and method names that occur in multiple places refer to say the same method, " You say that like it's a small detail, but isn't that really the hard part of the Ruby refactoring problem?

Posted by Mark Allerton on June 15, 2007 at 11:08 PM PDT #

Just picked up build 1952 with Rails Console support! Thanks. I would just like to say that I have switched to Netbeans for my dev work and have been very pleased. I would encourage anyone who hasn't yet found their Nirvana in a Rails/Ruby IDE to give Netbeans a try.

Posted by tom fowler on June 19, 2007 at 01:03 PM PDT #

Post a Comment:

Comments are closed for this entry.