Fun with TextMate: align equals in a block
[UPDATE 2008-03-21] (original article follows this update)
So, yesterday, I received a very nice email from Guillaume Cerquant about this script. He wanted it to be able to work with Objective-C variable declarations, as well as assignment statements. I thought it a good idea, so I’ve updated the script to handle both situations. Given either of the following messy blocks of code:
x = 10
String x = 10;
LongClassName *xyzabc = 13;
Class *xyzabc[] = { abc, def, ghi };
Class* xyzabc = &x;
int i;
boolean isItTrue;
boolean meh = false;
start_on = ENV["TM_LINE_NUMBER"].to_i
block_top = lines.length + 1
block_bottom = 0
search_top = 1
the script will reformat them to look like this:
x = 10
String x = 10;
LongClassName *xyzabc = 13;
Class *xyzabc[] = { abc, def, ghi };
Class* xyzabc = &x;
int i;
boolean isItTrue;
boolean meh = false;
start_on = ENV["TM_LINE_NUMBER"].to_i
block_top = lines.length + 1
block_bottom = 0
search_top = 1
In simple assignment blocks, the new script should continue to behave the way it always has, with maybe one exception in Ruby:
puts x
This code will be seen as a variable declaration, not a Ruby function call, which means it will get realigned by the new script, while it would have been ignored by the old. Hopefully, that won’t break things for anyone. If it does, I’ve left the link to the original version of the script, below.
Thanks to Guillaume for the idea. Also, I should point out that he’s done his own version of the script that specifically handles Objective-C variable declaration blocks, with outdenting of the * marker on pointers. My version doesn’t attempt to do that, so if you would like that feature, be sure to check out his version.
Okay, without further ado: Assignment Block Tidier, v0.6. The same instructions apply: add it in the Bundle Editor as a command, set it to take the selected text or document as input, and to replace the selected text with the output.
I’ve sent the new version of the script to Allan, so it may soon replace the original version in the default Source bundle. In any event, if you have any problems with either version of the script, please let me know.
Original Article
[UPDATE]: The script described in this article now ships with TextMate. Check the “Source” bundle.
I like keeping my code neat and tidy, especially when it comes to blocks of assignment statements. I find it a lot easier to read and to search a list of assignment statements if the equals signs are all lined up.
Consider:
my $var = alkjb;
my $longvar = alkjdsf;
my $superlongvar = lkjsdlfkjss;
my $other = sldkfjsdf;
my $x = slfksdlkfjslkfjsldkfj;
versus
my $var = alkjb;
my $longvar = alkjdsf;
my $superlongvar = lkjsdlfkjss;
my $other = sldkfjsdf;
my $x = slfksdlkfjslkfjsldkfj;
Problem is, it takes a lot of work to maintain that alignment, as I use descriptive variable names, which can vary in length a great deal. I used to do the work by hand, and it was a PITA. Well, no more! Here’s a TextMate command script I wrote to realign the statements at or around the current line, or within the current selection. If called without a selection, the script considers only contiguous assignment statements around the current line to be within the block. Add it as a command that takes the selected text or document as input and replaces the selected text with the output.
Note that the script is fresh off the presses, and hasn’t been heavily tested. It’s probably got a few bugs left to squash. It’s licensed under the Academic Free License version 3.0, which basically means you can do anything you want with it, but use is ENTIRELY at your own risk. See the actual license for the specifics, which override anything I’ve said here.
About this entry
- Published:
- 2006-05-16 / 3:14
- Category:
- Projects
Navigation and links
- Prev:
- Great Mac Software
No comments
Jump to comment form | comments rss [?] | trackback uri [?]