From 5c1901e2022d73289d288aa347c1c0f56e174d04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rene=CC=81=20Schwaiger?= Date: Sun, 29 Mar 2015 13:45:05 +0200 Subject: [PATCH] Add support for Perl-Critic Perl-Critic is a Perl source code analyzer. The command contained in this commit checks the current file with this tool. It opens a new HTML output window containing clickable references to the problems found by `perlcritic`. --- Commands/Check With Perl-Critic.tmCommand | 51 ++++++++++++++++++++++ Support/perlcritic.rb | 53 +++++++++++++++++++++++ info.plist | 1 + 3 files changed, 105 insertions(+) create mode 100644 Commands/Check With Perl-Critic.tmCommand create mode 100755 Support/perlcritic.rb diff --git a/Commands/Check With Perl-Critic.tmCommand b/Commands/Check With Perl-Critic.tmCommand new file mode 100644 index 0000000..e7f01df --- /dev/null +++ b/Commands/Check With Perl-Critic.tmCommand @@ -0,0 +1,51 @@ + + + + + beforeRunningCommand + saveActiveFile + command + #!/usr/bin/ruby + +# -- Imports ------------------------------------------------------------------- + +require ENV['TM_BUNDLE_SUPPORT'] + '/perlcritic.rb' + +# -- Main ---------------------------------------------------------------------- + +perl_critic + input + none + inputFormat + text + keyEquivalent + ~@c + name + Check With Perl-Critic + outputCaret + afterOutput + outputFormat + html + outputLocation + newWindow + requiredCommands + + + command + perlcritic + locations + + /usr/local/bin/perlcritic + + variable + TM_PERLCRITIC + + + scope + source.perl + uuid + 3F862332-074A-447B-AF5B-00D54FAA50F7 + version + 2 + + diff --git a/Support/perlcritic.rb b/Support/perlcritic.rb new file mode 100755 index 0000000..0bb5843 --- /dev/null +++ b/Support/perlcritic.rb @@ -0,0 +1,53 @@ +# rubocop:disable Style/AsciiComments, Style/HashSyntax + +# Analyze the current file with Perl-Critic. +# +# Authors:: Guillaume Carbonneau +# Matt Foster +# René Schwaiger (sanssecours@f-m.fm) + +# -- Imports ------------------------------------------------------------------- + +require ENV['TM_SUPPORT_PATH'] + '/lib/escape' +require ENV['TM_SUPPORT_PATH'] + '/lib/tm/executor' + +# -- Functions ----------------------------------------------------------------- + +# Format a single line of output from +perlcritic+. +# +# = Arguments +# +# [line] A single line of output produced by +perlcritic+ +# [show_reference] A boolean value that specifies if the output should include +# references to the matching section of the book “Perl Best +# Practices” (PBP). +# +# = Output +# +# The function returns a string containing HTML code. +def format_critic_output(line, show_reference) + return 'No problems found — Yay!' if line.chomp.end_with?('source OK') + styles = { 5 => 'Red', 4 => 'GoldenRod', 3 => 'DodgerBlue' } + styles.default = '' + file, line_number, column, message, reference, severity = line.split(':') + link = "txmt://open?url=file://#{e_url file}&line=#{line_number}" \ + "&column=#{column}" + " + (Severity: #{severity.to_i}) #{message} + at line #{line_number}, column #{column}. + #{reference + '.' if show_reference}
" +end + +# Output an HMTL formatted analysis of the current Perl file by +perlcritic+. +def perl_critic + show_reference = ENV['TM_PERLCRITIC_REFERENCE'] ? true : false + TextMate::Executor.run( + ENV['TM_PERLCRITIC'] || 'perlcritic', '--verbose', '%f:%l:%c:%m:%e:%s\n', + ENV['TM_PERLCRITIC_LEVEL'] || '--stern', '--nocolor', ENV['TM_FILEPATH'], + :use_hashbang => false, :verb => 'Criticizing') do |line, type| + case type + when :err then "#{htmlize(line)}" + when :out then format_critic_output(line, show_reference) + end + end +end diff --git a/info.plist b/info.plist index ddbda4a..f40e82e 100644 --- a/info.plist +++ b/info.plist @@ -19,6 +19,7 @@ ------------------------------------ DA0A5FEC-5F16-11D9-B9C3-000D93589AF6 DA0A5F4B-5F16-11D9-B9C3-000D93589AF6 + 3F862332-074A-447B-AF5B-00D54FAA50F7 CF2CA4F9-82A7-11D9-8753-000D9332809C DA0A5E1E-5F16-11D9-B9C3-000D93589AF6 ------------------------------------