Skip to content

Instantly share code, notes, and snippets.

@amw
Last active July 9, 2017 11:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amw/2235895ef6600b3d3ab1 to your computer and use it in GitHub Desktop.
Save amw/2235895ef6600b3d3ab1 to your computer and use it in GitHub Desktop.
Open Vim/MacVim or other $EDITOR on each offense in RuboCop
require "rubocop"
class OpenEditor < RuboCop::Formatter::ProgressFormatter
VimFamily = %w{vim gvim mvim}
def report_file file, offenses
super
if editor_supports_lines?
offenses.each do |offense|
run_editor file, offense unless offense.corrected?
end
else
run_editor file
end
end
def editor
ENV["EDITOR"]
end
def editor_supports_lines?
VimFamily.include? editor
end
def run_editor file, offense = nil
if offense
args = args_with_offense file, offense
else
args = args_without_offense file
end
original_modification_time = File.mtime file
system editor, *args
return if original_modification_time != File.mtime(file)
output.puts "No changes made to the file. Aborting."
exit 1
end
def args_with_offense file, offense
case editor
when *VimFamily
# Text vim will hide our console output, so we need to echo the message again
# inside vim.
message = offense.message.to_s
.gsub('"', "double quote")
.gsub("'", "quote")
[
"+#{offense.line}", # go to line
"-f", # gvim/mvim will not detach from shell
"-c", "echomsg '#{message}'",
file
]
else
[file]
end
end
def args_without_offense file
[file]
end
end
rubocop -r ./editor.rb -f OpenEditor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment