Last active
July 9, 2017 11:03
-
-
Save amw/2235895ef6600b3d3ab1 to your computer and use it in GitHub Desktop.
Open Vim/MacVim or other $EDITOR on each offense in RuboCop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rubocop -r ./editor.rb -f OpenEditor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment