Skip to content

Instantly share code, notes, and snippets.

@txus
Created June 18, 2012 21:36
Show Gist options
  • Save txus/2950877 to your computer and use it in GitHub Desktop.
Save txus/2950877 to your computer and use it in GitHub Desktop.
Vimcov runs a Ruby script and opens Vim marking the lines that were actually run

vimcov

Vimcov runs a Ruby script and opens Vim marking the lines that were actually run.

This could be useful when debugging long methods with lots of branching -- instead of abusing puts, you can just use Vimcov and you're good to go.

It runs only in Ruby 1.9.x.

Future versions will include more tight integration with test runs or Rails or whatever.

Usage

./vimcov my_script.rb

It will open vim and mark the lines that were actually run :D

#!/usr/bin/env ruby
require "coverage.so"
Coverage.start
require_relative ARGV[0]
Coverage.result.each do |k,v|
header = []
linenumbers = []
v.each_with_index { |n,idx| linenumbers << idx+1 unless n.nil? || n.zero? }
header << %Q{+"hi covered guifg=lightgreen guibg=lightgreen"}
header << %Q{+"silent sign unplace file=#{k}"}
header << %Q{+"sign define covered text=X texthl=covered"}
linenumbers.map! { |n| %Q{+"sign place #{n} line=#{n} name=covered file=#{k}"} }
everything = [header + linenumbers].join(' ')
system %Q{vim #{k} #{everything}}
end
@josepjaume
Copy link

This is awesome, Txus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment