Ruby Screenshot of the Week #18: Errors and Snippets


Tim Bray just wrote about error messages and their occasional difficulty for users. I ran into the same issue myself a couple of weeks ago when somebody filed a bug that NetBeans didn't correctly handle Ruby documentation markers. When looking at the user's source, I realized the error was right in the user's source file - but the parser message hadn't been particularly helpful: Syntax error, unexpected =.



This of course seems like an area where the IDE can help. So, as of the latest builds, there are some error rule handlers (in the experimental hints plugin) to help with this. Suppose you try to add in a documentation block like this:






The lightbulb on top of the stop sign tells you there's an associated suggestion. Press Alt-Enter to see the error message and suggested fix:






Applying the fix will obviously correct the indentation of =begin and =end. Here's another example - opening a file with ambiguous parameters:






I would really like your ideas on other common error patterns I can detect and better yet, fix. (See
RubyFeedback or leave comments here.)



While we're on the topic of quick fixes, the experimental hints also include some new suggestions for converting between brace blocks and do/end blocks. If you place the caret within the first line of a do-block or a {}-block, a little lightbulb will appear in the left margin. If you press Alt-Enter, you'll see some applicable fixes:






The exact list of choices shown obviously depends on the block. If you do this from a do/end block, the conversion goes in the opposite direction. And if you're looking at a multi-line block rather than a single line block, it will offer to collapse the block into a single line rather than expanding it as shown here. (It only offers this if it can fit the statements on a single line within the right-hand side margin, as shown by the faint red line in the editor.)



Unsurprisingly, applying the first fix gives us this source code for the block:






(This operation will sometimes also insert parentheses around the previous argument list when necessary, since {}-blocks have different precedence than do-end blocks.)



Finally, I just fixed some bugs in the code template handling code. If you invoke code completion you should now see the possible code snippets that match your input - along with their tab trigger names on the right and the full code template in the documentation popup:






That's it for now. Don't forget that NetBeans 6.0 Beta 1 was just released. The
New And Noteworthy document has the rundown on the new features included since Milestone 10. (Most of the things I've described in this blog entry were checked in after the beta cut off but beta is more stable for regular use.)



P.S. Rails creator David Heinemeier Hansson had some nice things to say about Sun's Ruby and Rails efforts. There's no evil plan, I promise!

Comments:

We can't sing enough praise for you guys!

Thanks and Thanks.

Posted by Hemant Kumar on September 20, 2007 at 07:53 AM PDT #

Tor: If there's anything we can do in JRuby to help standardize these errors, like agreeing upon an enum that lists them all, please let me know. I'd rather not have us update an error message and break some magic you've done in the NB stuff, and these error conditions coming out of the parser really should be more programmatically useful to embedders like yourself.

Posted by Charles Oliver Nutter on September 20, 2007 at 06:56 PM PDT #

Tor,

Great stuff, as usual.

Two minor things you might consider before final release:

1. There is no assert_equal snippet. There's an assert_not_equal (asne), an assert_same, etc., but no assert_equal. That's the most basic assertion (besides assert) so a snippet might be nice. (I've added my own.)

2. Tab activation of snippets doesn't seem to work when preceded by a period. For example, 'collection.ea' doesn't complete the each snippet. You have to put a space before the 'ea'. The enumeration snippets (ea and friends) don't really make much sense if you can't have a period before them.

Also, are there any plans to make code completion aware of Rails ActiveRecord objects? It would be neat if you could complete AR database attributes. I guess you could look at either the migrations or the schema.rb file to do this. You also then complete things like 'Person.find_by_first_name' and other dynamic AR finders.

3rdRails has this feature. I tried it out -- pretty cool, but I'm sticking with NetBeans. I'd stick with NB even if 3rdRails was free. 3rdRails still seems buggy: my first code completion attempt cause a NPE. Not a good start. It has lots of fancy features, but I miss the little things in NB: the squiggly lines over unused variables, bolding method calls, switching between test and source files, etc. Completion of AR db attributes is handy though.

Steve

Posted by Steve Molitor on September 21, 2007 at 02:18 AM PDT #

One more question: Is there any way to get code completion aware of things in files like environment.rb, test_helper.rb, etc. in Rails projects? I put constants in environment.rb and custom test helper methods in test_helper.rb, that I can never remember the exact names of. Completion would be handy.

Posted by Steve Molitor on September 21, 2007 at 02:28 AM PDT #

Charlie, yes, having a unique error code would be a great improvement. I have unit tests for all the error messages I'm relying on, and this is all driven off of the internal JRuby that I upgrade manually myself, so it's unlikely that this would break accidentally even if you change error messages, but it's a more solid architecture (and something javac already does). I assume this means you'd refine the IRubyWarnings interface.

Steve,
1. Whoops. I'm not sure why it went missing in the previous import - it's not listed as skipped in the old import summary. I'm upgrading the imported snippets and it's there now (but I haven't checked in yet).

2. The reason tab activation isn't working is issue http://www.netbeans.org/issues/show_bug.cgi?id=115569 which I just upgraded the priority on.

Regarding active record code completion, we've been discussing that on dev@ruby.netbeans.org and it's definitely in the plans.

Have you tried Command-K completion? If environment.rb is open, type a couple of the first chars and then Command-K (if necessary repeatedly to get the complete word inserted). It's not the same as semantic code completion but a very useful editing function. I do want to fix the code completion issues though. Can you give more details about what it is you have in your environment.rb that you want it to recognize (and from where?). E.g. are you simply defining top level constants there (to Object) that should be visible elsewhere?

Posted by Tor Norbye on September 21, 2007 at 05:13 AM PDT #

(Just to clarify command-K: when I said "environment.rb is open" I don't mean that this only works when editing environment.rb, you can match symbols in any buffer from any other open buffers.

Posted by Tor Norbye on September 21, 2007 at 05:14 AM PDT #

Tor,

I'm a Command-K addict. It helps. Mainly it's top level constants in environment.rb that I'd like to complete or jump to the declaration of to see the value. Of course I also put environment specific constants in development.rb, test.rb etc, but that would be more of a problem because you'd have to pick just one environment.

As I mentioned I also put test helper methods in test-helper.rb, extending Test::Unit.

In one project I also have custom extensions to ActiveRecord and ActionController. The sources are in /lib and required in environment.rb.

Steve

Posted by Steve Molitor on September 21, 2007 at 06:03 AM PDT #

Tor,

Has there been any talk about Rails-aware refactorings? In other words if I change the name of controller, have the tool also change the file name, the test file name, and the view folder?

Steve

Posted by Steve Molitor on September 21, 2007 at 06:06 AM PDT #

Hi Tor, I just started to use NB for my ror development and it looks really promising, unfortunately on my dual G5 it is pretty slow, coming from TextMate ;o)

One keyboard shortcut i cannot find in NB and is most used shortcut in TM for sure is Ctl-L to write "=>". This is used everywhere in ruby...

Thanks

Posted by miro on September 23, 2007 at 06:22 PM PDT #

I just found a sidenote on one netbeans.org page saying more about keyboard shortcut colon-tab which creates hash "key => "value"" pair, too bad it does not work in RHTML view.

Posted by miro on September 23, 2007 at 06:40 PM PDT #

Found it here: http://wiki.netbeans.org/wiki/view/RubyShortcuts .
Too bad docs is so scattered around many pages, anyway cool that is in there.
sorry for bothering you

Posted by miro on September 23, 2007 at 06:45 PM PDT #

Hey Tor, a few questions:

First, can control-tab be changed? I use Quicksilver for Mac OS X and that is my primary trigger for bringing it up and really would not like to change my standard workflow. Seems like if we could have a "double spacebar" or some sort of double key like Google does with their Desktop Search application (ie, command-command or control-control) that would be very helpful.

Second, where can I actually find a list of all template snippets? I've dug around the xml files and unpacked various jars but can't seem to find it. I ended up taking a screenshot of them, but would rather have a pretty printed list of them handy for instances when you update and such for while I'm learning.

Oh, and is anything going on with the dark theme for the overall NB interface as well as refining the actual text editor highlighting? There are quite a few pieces of the the current dark themes that do not look right and are hard to read while highlighting and such.

Thanks much.

Posted by ylon on September 23, 2007 at 10:58 PM PDT #

Hi Steve,
I updated all the TextMate snippets to the latest trunk version, and in particular, "ase" should be there now. And yes, there are plans to do Rails-specific refactorings!

Ylon, you can find a full list of the Ruby snippets here:
http://ruby.netbeans.org/codetemplates-ruby.html
and the RHTML snippets here:
http://ruby.netbeans.org/codetemplates-rhtml.html

(I also linked to this from the code templates page which talks about the available syntax - http://wiki.netbeans.org/wiki/view/RubyCodeTemplates ).

Also, the completion trigger -should- be configurable. I think it even binds multiple keybindings by default to make conflicts less problematic. Without making any changes, on my Mac, I can use Command-\\ (if you look in the Source menu, look for "Complete Code".)

Unfortunately, I don't have any news on the dark theme. A couple of people have stepped forwards and offered to take a look at the XML files but I haven't seen any patches yet.

Miro,
you can use "l"+tab instead of ctrl+l to get a "=>". And yeah these should probably be added to RHTML as well. (It actually -should- be working inside Ruby logical sections, but there's a bug preventing this from actually working. For 6.0 perhaps I'll just go and add the most critical ones there.

Posted by Tor Norbye on September 24, 2007 at 08:13 AM PDT #

Thanks very much for the info Tor. The good dark themes are certainly a high priority in my book and it appears others'. Great to hear that folks are working on it (or hopefully so).

Curious, any more DRYML updates coming from the changes that occurred from the .6.2 release at hobocentral.net? I've not really dug around and seen what you're actually doing with it, just getting started myself with Hobo so I can't really be a good judge of what's going on yet.

Thanks again!

Posted by ylon on September 24, 2007 at 11:25 AM PDT #

Thought that this was interesting:
http://lifeonrails.org/2007/8/30/netbeans-not-just-a-nice-personality

Posted by ylon on September 25, 2007 at 04:28 AM PDT #

Tor, with regards to picking colors, who would be responsible for implementing the color picker? It would be very beneficial to use the default Apple color picker versus the current Java based one on Mac OS X.

Also, how would one go about exporting a color theme for posting to the web for others to use?

Posted by ylon on September 25, 2007 at 05:17 AM PDT #

Sorry, one more comment. I'm sure this needs to be pointed to the proper direction: for the Fonts & Colors it would be quite useful if it would show a full source file with every imaginable (contrived of course) text scenario and then allow the user to actually click on each piece of text and have it automatically select the proper Category selector for color, font, etc. selection.

Posted by ylon on September 25, 2007 at 05:23 AM PDT #

thanks for fast reply and actually implementing it, now there is one more reason to buy macbookpro finally ! i really like netbeans but on my g5 tower on g4 pooowerbook is pretty slow...

Posted by miro on September 25, 2007 at 07:39 AM PDT #

just realised little annoyance, when I double-click a variable in rb, it selects whole text "@variable", but in rhtm it only selects "variable", without @

Posted by miro on October 01, 2007 at 11:19 PM PDT #

interesting that ":whatever" gets selected whole in both rb and rhtml

Posted by miro on October 01, 2007 at 11:31 PM PDT #

Post a Comment:
Comments are closed for this entry.
About

Tor Norbye

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today