Wednesday, June 20, 2007

Ruby Screenshot of the Week #14: RHTML

WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.


Phew - it's been a very busy past couple of weeks. We had feature freeze for NetBeans 6 just a couple of days ago (Friday night at midnight to be exact) so I was focusing all my energy on getting as many things done as possible by the deadline. From here on out, focus will be on stability. I'm starting to work my way through the bug list, and it's looking promising.



The feature I'd like to show this time is a recent addition: Improved RHTML support. In particular, RHTML completion, RHTML goto declaration, and displaying Ruby errors in RHTML files.



Here's RHTML completion (click for full size):








Things to notice here is that first of all, the various methods you might want completion (and more importantly, documentation) for that are coming from ActionView are showing up as expected - link_to, form_tag, and so on. In addition, methods from your Helper class for this controller also show up. There's also an inherited attribute from ActionView here.



Second, note the enhanced display of the rdoc. It turns out lots of Rails documentation have code snippets, and the colorizer (which I
discussed recently) now properly handles language embedding too - RHTML fragments containing Ruby and HTML colors.) Speaking of colors, NetBeans 6 recently got a makeover in the default color scheme. In addition, there are some new color themes available in the options dialog - including the old colors. You can see the new colors in these screenshots.



Go To Declaration is pretty self explanatory - it works the same way that it does in Ruby files; hold the Ctrl-key and click on identifiers and it will warp to the definition for that method, class or variable assignment. Another new feature is displaying source errors in the file. Behind the scenes, NetBeans now models what ERB will do to the file in a Rails server, and maps these errors back to the RHTML source. Thus, if you have an error like the below, you get an error annotation with a tooltip displaying the error:






One thing which is still missing is indentation of the Ruby code in RHTML files. Marek Fukala is working on this for all the embedded languages, and I believe this feature is still going in after the freeze, so look for it in upcoming builds.



Finally, thanks to those of you who have been using the regular builds and providing feedback and bug reports. I apologize for the last couple of weeks - because of the heavy feature focus before the deadline I haven't been very responsive, and the builds have gone through a huge amount of flux. One of the things which is going on is that all the keyboard shortcuts have been changing - the details are outlined here - so if you thought something was broken, perhaps it's just using a different shortcut now. (We're still tweaking this.) Things will get more and more stable from day to day now. Milestone 10 should be out in a couple of weeks.


Tuesday, June 5, 2007

Ruby Screenshot of the Week #13: Refactoring

WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.


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!