Ruby Snippets, Part Two
My recent post about snippets has brought in more mail than I was expecting. While snippets may not be one of the ‘big features’ of Ruby In Steel from the development point of view, I get the strong impression that, from the user’s perspective, the little blighters may be more important than they are given credit for.
I can understand why. Snippets are not just shortcuts for lazy typists (though they are that too!) – they are also one of the ways in which you can configure the editor to work the way that you want it to. Editors are very personal things. Some people get addicted to Emacs, others love Vi(m) or Brief while increasingly large numbers of Mac users can’t imagine using anything other than Textmate.
If it’s just a favourite keyboard mapping you need, Visual Studio can sort things out easily. It comes with Emacs and Brief key mappings (among others) as standard and you can set up your own custom keymappings if you happen to have a couple of hours to spare. But if you’re a Textmate user, one of the things you are likely to miss in other editors are all those snippets. I don’t know how closely Visual Studio snippets can mimic the Textmate variety as I’m not a Textmate user (but thanks to those of you who are, and who’ve emailed me on the subject). I have a feeling that Textmate snippets are a bit more ‘programmable’ than the Visual Studio variety. At any rate, even if they aren’t identical, I’m sure it should be possible to create a useful set of auto-expansion Ruby snippets for use in Ruby In Steel.
In my last blog entry, I mentioned that I am writing a little editor utility, Sniped!, to make snippet writing easier. I forgot to post a screenshot. So now, to make up for that deficit, here is one.
As you can see, writing a snippet with Sniped! is just a case of filling out some fields. The actual code is entered into the editing panel down at the bottom. Just above this, on the left you can see a list of available identifiers – here there are just two, $name$ and $aName$ though, in principle, there may be many more. You can insert the available identifiers into the code of the snippet by selecting them from a popup menu. The same menu also lets you enter the end point where the cursor is positioned when the snippet has finished) and the selected area – the point at which a marked block of code in the Visual Studio editor will be placed inside the snippet itself (if you so wish).
The only other thing you need to do when writing a snippet is to define the identifiers. You can do this in this popup dialog…
Here you can enter the name default value, tooltip and a few other bits of data applicable to each identifier. The Function field is not (currently) used for Ruby snippets – as far as I am aware functions are only available to C# and J# snippets.
Here is the above snippet as it will appear in the code editor…
I decided to make Sniped! work with C# (as that’s the language I use most of the time these days when not coding in Ruby) and I’ve also added some extra features which, to the best of my knowledge, will also let it work with Visual Basic and XML snippets.
Anyhow, I’m hoping that, once users get the chance to play around with our Ruby snippets, some people may use Sniped! to write a whole bunch of new ones.
Oh, and did I mention that we are also writing a macro library for Ruby in Steel? Well, you can never have too much in the way of configurability…