My First Serious TextMate Automation

Posted by Rick DeNatale Tue, 09 Oct 2007 12:15:00 GMT
I recently got assimilated by the Mac/TextMate borg. I'm slowly teaching my fingers to dance the TextMate tango and unlearning old vim habits.

One resource has been James Edward Gray II's book on TextMate published by the Pragmatic Programmers. I finally sat down and got serious about writing some automations of my own.

I've got to say that I'm pretty impressed by TextMate. At the last Raleigh.rb hack night I was talking to another vim user. I'd mentioned to him that you can extend TextMate easily in Ruby, without really having experienced it. Today, I wrote a neat little TextMate command to help in building Rails database test fixtures.

It acts like a tab triggered snippet, but it's a smart little critter. If I'm editing a fixture file, say 'test/fixtures/users.yml', I can type item then tab and it will produce the skeleton yaml for a new record, with:

  1. A dummy name selected for overtyping.
  2. The id set to the next available primary key
  3. Each column name as a yaml key ...
  4. ... A tabstop on a value which shows the column type

For example:

Suppose I've got a fixture file for a model called Item:

	# == Schema Information
	# Schema version: 14
	#
	# Table name: items
	#
	#  id          :integer(11)   not null, primary key
	#  description :string(255)   
	#  name        :string(255)   
	#  price       :integer(11)   
	#

	# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
	lotr1:
	  id: 1
	  name: LOTR1
	  description: The Road Goes Ever On
	lotr2:
	  id: 2
	  name: LOTR2
	  description: Introduction to Elvish

If I type a new line at the end of this file with the snippet command item and hit tab I see this:


lotr2:
  id: 2
  name: LOTR2
   description: Introduction to Elvish

  NameMe:
   id: 3
   description: string
   name: string
   price: integer

With NameMe selected so that I can easily replace it by typing. Note that the id was set to 3 since rows with ids 1 and 2 already exist.

Hitting tab again selects the first instance of string, and subsequent tabs step through the columns

Another feature which I won't show is that it looks at the table and if it doesn't have a primary key (e.g. for an association table in a has_and_belongs_to_many association), it won't generate the id: or a key.

You might be impressed, but I'm pretty happy with the results of a couple of hours of playing with TextMata and Ruby.

How it works.

The whole thing is done with a TextMate command implemented in Ruby. A few notes. First, it's just a coincidence that the snippet trigger is item and my example table is called items. Second, I'm not using the comments at the top of the fixture file which were generated when the model was created. The command works off of db/schema.db.

Here's the code:

  #!/usr/bin/env ruby -w  
  #
  #  Created by Rick DeNatale on 2007-10-09.
  #  Copyright (c) 2007. You may use this under the ruby license.                    

  $LOAD_PATH << "#{ENV['TM_SUPPORT_PATH']}/lib"
  require 'exit_codes.rb'


  class FixtureSnipGen
    attr_reader :rails_root, :table_name
    def initialize(fixture_file)
      match  = %r{(.*)test/fixtures/(.*).yml}.match(ENV["TM_FILEPATH"])
      @rails_root = match[1]
      @table_name = match[2]
      @no_id = false  
    end

    def next_id
      max_id = 0     
      while line = gets
        claimed = %r{\s+id:\s+(\d+)}.match(line) 
        max_id = [max_id, claimed[1].to_i].max if claimed
      end
      max_id + 1
    end

    def gen_snippet
      cols = gen_columns
      if cols
        puts "${1:NameMe}:"
        puts "  id: #{next_id}" unless @no_id
        puts cols.join("\n")
      else 
        puts "Couldn't find #{table_name} in schema.rb"
        TextMate.exit_show_tool_tip
      end
      puts "$0"
    end

    def schema_file_name  
      "#{rails_root}db/schema.rb"
    end 

    def value_tab(text)
      @tab_stop += 1
      (@tab_stop < 10) ? "${#{@tab_stop}:#{text}}" : " # #{text}"
    end


    def gen_columns
      #    raise Exception.new("No schema file") unless File.exist?(schema_file_name)
      started = false
      @tab_stop = @no_id ? 0 : 1
      result = []
      File.new(schema_file_name).each do | line |
        if started
          break if %r{^(\s*)create_table\s+}.match(line)
          col_match = %r{\.column\s+\"(.*)\"\s*,\s*:(\w+)}.match(line)
          result << "  #{col_match[1]}: #{value_tab(col_match[2])}" if col_match
        else
          started = %r{^(\s*)create_table\s+["']#{table_name}['"]}.match(line)
          @no_id = %r{:id\s+=>\s+false}.match(line) if started
        end
      end
      return result.empty? ? nil : result    
    end
  end

  begin
    FixtureSnipGen.new(ENV["TM_FILEPATH"]).gen_snippet
  rescue Exception => ex
    puts ex
    TextMate.exit_show_tool_tip
  end

In the TextMate bundle editor, I just added a new command with this code, and set the following options:

Save:
Nothing
Input:
Entire Document - which pipes the entire textmate buffer for the file into stdin.
Output:
Insert as Snippet - which triggers TextMate to interpret the output as a snippet once the script has finished.
Activation:
Tab Trigger = item
Scope Selector
source.yaml - this only works within yml files.

Finally note that although my example uses a table called items, it's just a coincidence that the tab trigger is item. It would be item in any fixture file.


Trackbacks

Use the following link to trackback from your own site:
http://talklikeaduck.denhaven2.com/trackbacks?article_id=469

Comments

  1. John Joyce 2 days later:

    Welcome to the joy of TextMate! Just when you start feeling limited in TM is the same time you start exploring and discover how easy it is to extend it yourself. It is missing some things, like split views, but overall it is a very nice app with a good workflow. The sad thing is working in XCode after being seriously spoiled for TM’s key-bindings and triggers…