« Previous day (Jul 10, 2007) | Main | Next day (Jul 12, 2007) »

20070711 Wednesday July 11, 2007

Ruby Screenshot of the Week #15: Hints and Quick Fixes!

I've justed checked in support for Ruby hints and quickfixes. Most Java IDE users should be familiar with this, but I'll illustrate with some screenshots:

I've opened a Ruby file - notice the little lightbulb thingy and the underline on one of the lines:

The tooltip for the lightbulb will tell you that you have a block variable which is reusing a local variable. This means that the local variable will be modified by the block - which is sometimes not intentional. Let's put the caret there and press Alt-Enter (or invoke the "Fix Code..." item from the Source menu)

You can now use the arrow keys to choose among the available fixes, and press Return to apply one. This will initiate in-place editing of only the local variable references (or only the block variable references, depending on which fix you selected). When you're done you have a unique block variable that is no longer aliasing or side-effecting the local variable:

(The in-place editing is still a bit buggy because of issue 108889. Hopefully we can get it fixed soon.)

Here's another hint: Rails Deprecations. This looks for usages of deprecated Rails idioms, and adds little warning signs with a description. Here's an example of how this looks:

In the above code, you're using the @request field, instead of the request or request= attribute methods. And by the way, if any Rails experts are reading this: The Rails Deprecation document describes these fields are deprecated, yet the Rails code generator itself spits out code which uses them (specifically, in the controller's functional test). Is there a good reason for this? For now, the rails deprecation hint skips Rails test files when scanning for this issue.

Here's another Rails deprecation example - using one of the various deprecated methods. The tip also states the suggested replacement:

And finally, here's a line-specific quickfix. It only kicks in to analyze the current line and is suitable for things you wouldn't want splattered all over your file, such as offers to create classes and methods etc. This tip looks for class or method definitions on a single line, and offers to split and reformat them across multiple lines (I think Charlie requested this one). Here's a typical same-line-class put into a rails controller test:

Applying the fix yields the expected result - semicolons removed, newlines placed in the right places and indentation fixed as expected:

You can configure the available hints under options. (The Ruby options are going to be expanded and reorganized shortly). Here's the hints panel which lets you see the available hints, enable/disable them, and configure whether each hint is shown as a warning, an error, or a warning on the cursor-line only.

Notice how the list of hints is kinda short today... That will be the topic of my next blog entry!

P.S. We're after feature freeze so whether this appears on the update center or in the default product remains to be seen, but I'm always an optimist. In any case, it will be easy to access it. For now however it's in the daily builds

(2007-07-11 15:45:09.0) Permalink Comments [6]

Progress

I apologize for not blogging in several weeks. A number of factors have contributed; in addition to coming down with a cold, we've had a short vacation, and perhaps most importantly, I've been working on some things that aren't very glamorous to blog about.

In particular, stabilization. I rewrote the way indentation is computed, so it should be working pretty well now. One of my unit tests takes all the Ruby files in the Ruby lib distribution as well as all the Rails files, formats them and verifies that the results look okay. Various bugs have in the past caused the formatting to skew off to the right or left if the IDE didn't properly interpret the code correctly. But it's now handling nearly all 1500 files correctly. (I say nearly because I've had to exclude a couple of files that trigger a lexing bug. Unfortunately, it's a bug outside my area and the responsible engineer is on vacation - that's July for you - but it should hopefully get addressed soon. There are some other bugs in the same situation. Thank you for your patience if you're one of the daily-build-downloaders... Give us a couple of more weeks).

I've also manually inspected the diffs for some of the largest source files - date.rb, resolv.rb etc. - and not only does the new NetBeans formatting avoid mangling the source file - it produces nearly identical results to the manually formatted sources! In both files only 10-15 lines differed, and these mostly because there's no clear agreed upon convention on how far a "hanging indent" should go.

In any case, please give it a try and let me know how it works. Smart-indent (indenting the caret when you press Return) is also using the new algorithm, so writing code like

   foo =
and pressing return should now do what you expect - it will indent further in on the next line - and if you type a statement there which completes the assignment and press return, indentation is back out to the top level.

Another area I've been tweaking is handling heredocs properly. This was really challenging. Ruby allows you to write "references" to a String (which you will start defining on the next line) and then continue writing your expression. The expression then continues AFTER the string has been completed. Here's a screenshot which shows this in action:

As you can see, the method call I'm writing starts on the first line, and finishes on the last line; there are three literal strings passed to the function, and two of them are defined as heredocs (<<).

This is mostly working now (except for some incremental lexing bugs I'll get to later). I also fixed it such that matching tag highlighting (and navigation) works for heredoc markers.

Meanwhile, I haven't only been bug fixing. More on that in my next blog entry, hopefully within the next day or two....

Oh, and one more quick thing: With the latest builds, in your RHTML files, you can now control click on render :partial's, link_to etc. and it will warp to the corresponding files - RHTML partials, controller files, actions, etc.

Here's clicking on a render partial:

Here's clicking on a controller. (Even though the hyperlink only shows the controller part as the link, the Go To Declaration handler is actually looking at the parse tree and finds the :action definition too such that it can jump to the actual action in that controller.

(2007-07-11 00:14:36.0) Permalink Comments [7]