Blog

Align comments in Textmate

Unless I’m using Objective-C, which is fantastically self-documenting, I often like to add comments to parameters to remind me of what they do. For example:

imagecopy($image, $frame, 
          ($x=max($width-$this->topRightCap[kCapWidth], $this->topLeftCap[kCapWidth])), // Destination x
          0,  // Destination y
          $x,  // Source x
          0,  // Source y
          min($this->topRightCap[kCapWidth], $width-$this->topLeftCap[kCapWidth]),  // Source width
          $this->topRightCap[kCapHeight]); // Source height

The only thing is, it looks terrible and is very hard to read. I usually carefully insert spaces before the comments so they line up, but that’s really hard to maintain.

I use Textmate, and found an ‘Align Assignments’ script by Chris Poirier which applies very nice formatting to a block of assignments.

Some trivial modifications resulted in a similar script to align comments. Hit Ctrl-Option-Command-/, and:

imagecopy($image, $frame, 
          ($x=max($width-$this->topRightCap[kCapWidth], $this->topLeftCap[kCapWidth])), // Destination x
          0,                                                                            // Destination y
          $x,                                                                           // Source x
          0,                                                                            // Source y
          min($this->topRightCap[kCapWidth], $width-$this->topLeftCap[kCapWidth]),      // Source width
          $this->topRightCap[kCapHeight]);                                              // Source height

Here it is:

Align Comments.tmCommand.zip

, , . Bookmark the permalink. Both comments and trackbacks are currently closed.

3 Comments

  1. Posted December 20, 2009 at 9:54 pm | Permalink

    bak hacito sana neler anlatacagim ama once onayla?

  2. Jay Kickliter
    Posted October 5, 2010 at 8:58 pm | Permalink

    I’ve been trying my best to modify your code, but I can’t get it to do what I want.

    I want it behave as is, but not to touch lines where the first character is a comment.

    So:

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Setup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    tMin = -1/10; tMax = 1/10;

    fsa = 200; % sampling rate ts = 1/fs; % sampling period t = tMin:ts:tMax-ts; % sampling duration N = length(t); % number of samples

    Becomes: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Setup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    tMin = -1/10; tMax = 1/10;

    fsa = 200; % sampling rate ts = 1/fs; % sampling period t = tMin:ts:tMax-ts; % sampling duration N = length(t); % number of samples

    Any tips?

    • Jay Kickliter
      Posted October 5, 2010 at 8:59 pm | Permalink

      Oops. It didn’t format as planned. But I hope you get the idea.