Picture of stu

Rake + Builder + TextMate = Sweet

  • Posted By Stuart Halloway on August 30, 2006

Jim Weirich gives a cool example of using Rake to automate searching your source code. The example even includes Emacs integration. But I live in TextMate these days, so here’s the TextMate integration for Jim’s fic.rb:

#!/usr/bin/env ruby
#fl2tm.rb converts 'file:lineno context' into HTML for TextMate
require 'rubygems'
require_gem 'builder'

base = Dir.pwd
title = ENV["TITLE"] || "Stdout 4 TextMate" 
lines = []
xml = Builder::XmlMarkup.new(:target=>STDOUT, :indent=>2)
xml.html do
  xml.head do
    xml.title(title)
  end
  xml.body do
    xml.h1(title)
    while line = gets
      lines.push line
      # doesn't handle whitespace in path names 
      if line =~ /\s*([^:]+):(\d+):(.*)/
        (file, line, context) = [$1,$2,$3].map {|i| i.chomp}
        xml.li do
          if file =~ %r{^/}
            href = "txmt://open?url=file://#{file}&line=#{line}" 
          else
            href = "txmt://open?url=file://#{base}/#{file}&line=#{line}" 
          end
          xml.a "href" => href do
            xml.text! "#{file}:#{line}" 
          end
          xml.text! context
        end
      end
    end
    xml.hr
    xml.pre(lines.join())
  end
end
This little script converts the output of fic into HTML with TextMate links, so that you can click on the links to navigate TextMate. To add to TextMate, create a command that looks like this:
#!/usr/bin/env ruby
$: << ENV['TM_SUPPORT_PATH'] + '/lib'
require "dialog" 

Dialog.request_string(:title => "Find in Code",
                     :prompt => "Extensions Then Res",
                    :button1 => "Find") do |line|
  Dir.chdir(ENV['TM_PROJECT_DIRECTORY'])
  path = ENV['TM_RUBY_SAMPLES'] 
  system "#{path}/rake/fic.rb #{line} | #{path}/rake/fl2tm.rb" 
end
In the TextMate Bundle Editor, set Input to ‘None’ and Output to ‘Show as HTML.’ You’ll have to replace the TM_RUBY_SAMPLES with your path to fic.rb and fl2tm.rb-I haven’t had time to package this (and other goodies) into a TextMate bundle yet. You will need to have RubyGems and Builder. I hope Jim has those installed. :)

Comments
  1. topfunkyAugust 30, 2006 @ 02:19 PM
    Great idea! I've been learning more about customizing TextMate and am using a similar task that just uses grep. It seems that you could use grep and still pipe it through this script to format the output, but it would hopefully be faster than the pure Ruby version. And since TextMate only runs on Mac (which have grep), this wouldn't be a problem. Or is there another practical benefit that I'm missing? I'll try to bundle my grep version as well.
  2. topfunkySeptember 01, 2006 @ 11:38 AM
    This is awesome! I'm piping the results of my Rails deprecated code rake task through a modified version of your script, which allows me to jump straight to the line found by grep. So cool!
  3. Nathaniel TalbottSeptember 01, 2006 @ 05:18 PM
    I feel like I'm missing something... how does this differ from Edit->Find->Find in Project?
  4. topfunkySeptember 02, 2006 @ 11:48 AM
    For me, this isn't any different from Find in Project, but the ability to pipe a customized grep through a script and turn it into clickable output is useful.