Created
May 18, 2022 12:53
-
-
Save andynu/b19d28aee427e0ea26707cd40500c279 to your computer and use it in GitHub Desktop.
rfluff a way to list TODOS|XXX|HACK|etc /w optional vim quickfix formatting.
This file contains hidden or 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
#!/usr/bin/ruby | |
# | |
# gather a few statistics and find all flagged comments | |
# | |
# Usage: | |
# rfluff [-qf] [tag] [tag] ... | |
# | |
# -qf outputs a vim compatible quickfix format, otherwise | |
# the output is human readable | |
# | |
# [tag] - Default tags are listed below in `flags`, but you can add any additional tags you want on the commandline. | |
# | |
# | |
# Vim Users: | |
# | |
# Command to call :Rfluff and have it show up in your quickfix list. | |
# | |
# command! Rfluff cexpr system('rfluff -qf ' . shellescape(expand('%:r')))|:cw | |
# | |
file_types = %w[rb erb haml rake cgi coffee yaml yml] | |
particular_files = %w[README* readme* rakefile Rakefile capfile Capfile Gemfile Guardfile] | |
find = [ | |
particular_files, | |
particular_files.map{|f| '**/' + f}, | |
file_types.map{|ext| '**/*.' + ext} | |
] | |
flags = { | |
'TODO' => /\bTODO\b/, | |
'FIXME' => /\bFIXME\b/i, | |
'@deprecated' => /\b@deprecated\b/i, | |
'@todo' => /\b@todo\b/i, | |
'XXX' => /\bXXX\b/i, | |
'REVIEW' => /\bREVIEW\b/, | |
'FEATURE' => /\bFEATURE\b/, | |
'PROBLEM' => /\bPROBLEM\b/, | |
'RAILS_TEMPLATE' => /\bRAILS_TEMPLATE\b/, | |
'hack' => /\bhack\b/i | |
# "relative_paths" => /\.\.\//, | |
} | |
IGNORE_PATHS = %w[ | |
tmp | |
node_modules | |
] | |
ARGV.each do |arg| | |
next if arg == '' | |
next if arg == '-qf' | |
flags[arg] = %r{\b#{arg}\b/i} | |
end | |
# initialize totals | |
totals = {} | |
comments = {} | |
# | |
flags.keys.each do |flag| | |
totals[flag] = 0 | |
end | |
# iterate over the pertinent files | |
Dir[*find.flatten].each do |file_name| | |
next if IGNORE_PATHS.any?{|ignore_path| file_name.start_with?(ignore_path) } | |
comments[file_name] = [] | |
# examine for comments | |
File.open(file_name, 'r') do |file| | |
file.each_with_index do |line, line_number| | |
flags.each_pair do |flag, regex| | |
unless line.index(regex).nil? | |
totals[flag] += 1 | |
comments[file_name] << [line_number + 1, line.strip.chomp] | |
#comments[file_name] << [line_number+1, line.strip.chomp + "["+flag+", #{regex.to_s}"+"]"] | |
end | |
end | |
end | |
end | |
end | |
# clean up a display of a hash of totals | |
# ala Data::Hash::Totals | |
def as_table(hash) | |
max = 0 | |
hash.keys.each do |key| | |
if key.size > max | |
max += key.size | |
end | |
end | |
table = '' | |
unused_keys = [] | |
hash.keys.each do |key| | |
if hash[key].zero? | |
unused_keys << key | |
else | |
table += key.rjust(max) + ': ' + hash[key].to_s + "\n" | |
end | |
end | |
return table, unused_keys | |
end | |
def print_human(comments, totals) | |
# show what we've collected | |
found = false | |
comments.keys.each do |key| | |
unless comments[key].empty? | |
found = true | |
puts key | |
comments[key].each do |line_number, line| | |
puts "\t%s: %s" % [line_number, line] | |
end | |
puts | |
end | |
end | |
# show the totals | |
table, unused_keys = as_table(totals) | |
if found | |
puts | |
puts table | |
puts | |
puts "# Unused keys: #{unused_keys}" | |
end | |
end | |
def print_vim_quickfix(comments, totals) | |
# show what we've collected | |
comments.keys.each do |filename| | |
next if comments[filename].empty? | |
comments[filename].each do |line_number, line| | |
puts '%s|%s| %s' % [filename, line_number, line] | |
end | |
end | |
## show the totals | |
#puts as_table(totals) | |
end | |
if ARGV[0] == '-qf' | |
print_vim_quickfix(comments, totals) | |
else | |
print_human(comments, totals) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment