Skip to content

Instantly share code, notes, and snippets.

@mislav
Last active May 20, 2020 14:52
Show Gist options
  • Save mislav/5346548 to your computer and use it in GitHub Desktop.
Save mislav/5346548 to your computer and use it in GitHub Desktop.
Vim highlight lines of Ruby that are not covered with tests. When the test suite runs, SimpleCov will generate a "coverage/raw" file. Next, the Vim function can be used to read data from that file for the current buffer and mark lines not covered with tests.
require 'simplecov'
require 'coveralls'
# writes out a "coverage/raw" file to be read by Vim
class RawMissingLinesFormatter
def format(result)
File.open('coverage/raw', 'w') { |raw|
result.source_files.each do |file|
file.lines.each do |line|
if line.missed?
raw.puts "- #{line.number} #{file.filename}"
elsif line.covered?
# uncomment to also have covered lines displayed:
# raw.puts "+ #{line.number} #{file.filename}"
end
end
end
}
end
end
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::HTMLFormatter,
RawMissingLinesFormatter
]
SimpleCov.start do
add_filter '/bundle/'
end
" This is a local, per-project `.vimrc`.
" It maps `H` to read in line coverage from the "coverage/raw" file and
" mark uncovered lines as errors using Vim :signs functionality.
highlight SignColumn NONE
sign define covpos text=• texthl=String
sign define covneg linehl=Error text=𐄂 texthl=HelpDebug
function! s:SignCoverage(file)
let output = system('grep "'.a:file.'" coverage/raw')
let lines = split(output, '\n')
exec 'sign unplace * buffer='.bufnr('%')
for line in lines
let [ type, lnum, fname ] = split(line, ' ')
let name = type == '+' ? 'covpos' : 'covneg'
exec 'sign place '.lnum.' line='.lnum.' name='.name.' buffer='.bufnr('%')
endfor
endfunction
map H :call <SID>SignCoverage(expand('%:p'))<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment