Velocity bundle for TextMate

28 September 2007

Well over a year ago, I mentioned that I was working on a Velocity bundle for Textmate. Or, to be more precise: I mentioned that I’d already written one that we were using at NPG.

A year later, I’m ready to release the bundle; you can get it from its Google Code site. But before you go there, an explanation for the delay is in order – and on the way, I’ll tell you about how the bundle was written.

Continue reading this post…

As a Rubyist and Textmate user, you’ll probably be aware that def will tab-expand to stub out a method definition. You might also be aware that, for the purposes of Test::Unit, deft will tab-expand to a test method beginning def test_, allowing you to append the name of your test.

But that’s not much more help, because if we’re naming our tests properly, they’re probably going to have very_long_names, and hitting underscore all those times is a bit of a pain. So I rectified that, with this command (and it’s a command, rather than a snippet, because of all the processing it does). Pull up the commands dialog (Command-Opt-Control-C), create a new command in the Ruby bundle, and give it the following code:

#!/usr/bin/env ruby

name = STDIN.read.strip
testname = name.gsub(" ", "_").downcase
print <<OUTPUT
	def test_#{testname}
		\$0
	end
OUTPUT

The command’s Input should be “Selected Text” or “Line”; its Output should be “Insert as Snippet”. The scope should be set to source.ruby. And give it whatever key definition you want; I’ve got it on ctrl-opt-shift-t.

Usage is easy. On a new line in your test file, type the name of your test in plain English with no punctuation, eg:

get to index should list all items

and then hit your shortcut. You’ll get the following out:

def test_get_to_index_should_list_all_items

end

and your cursor will be slap bang in the middle of the test, indented, ready to write. That’s what I really want from a test definition snippet – something more than deft supplies. It’s another minute or two’s work to make it strip punctuation, so you can convert real sentences to test cases. I just decided to condition myself to save on coding on this morning’s commute.

Problem: there’s no Velocity (VTL) bundle for Textmate.

Solution: write one yourself.

I’m currently working on a basic Velocity bundle for Textmate. We use it as a templating language at work, and, let’s face it, Textmate is an awesome editor with many, many ways to make your life easier. Given that it’s listed as a bundle people might be interested in… I’d better get started on it, right?

If you’re interested, leave a comment or drop me an email. So far I’ve got some basic function and syntax highlighting, along with autocompletion of some common constructs. Once it’s more finalised – and has been built in accordance to the VTL spec, not just the way I write it, I’ll start putting out releases.

(And if none of this makes much sense: Velocity/VTL is a templating language for Java web-apps. It’s one of the least sucky Java templating languages, apparently, but it’s not as mature of fully-featured as, say, Smarty.)