From 59724dc1b68f019abadf8a5cfcbddfbb27ad20c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schwaiger?= Date: Fri, 3 Jun 2016 15:54:32 +0200 Subject: [PATCH] =?UTF-8?q?Add=20command:=20Reformat=20Document=20(?= =?UTF-8?q?=E2=8C=83=E2=87=A7H)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new command “Reformat Document” formats the current document using the `--auto-correct` option of [RuboCop][]. It also shows information about the formatting status in a floating tooltip. The command displays the information about the formatting status either as black text only, or as colorful text if [aha][] is accessible via `$PATH`. [aha]: https://github.com/theZiz/aha [RuboCop]: https://github.com/bbatsov/rubocop --- Commands/Reformat Document.tmCommand | 52 ++++++++++++++++++ Support/lib/rubocop.rb | 82 ++++++++++++++++++++++++++++ info.plist | 1 + 3 files changed, 135 insertions(+) create mode 100644 Commands/Reformat Document.tmCommand create mode 100644 Support/lib/rubocop.rb diff --git a/Commands/Reformat Document.tmCommand b/Commands/Reformat Document.tmCommand new file mode 100644 index 0000000..7a823c7 --- /dev/null +++ b/Commands/Reformat Document.tmCommand @@ -0,0 +1,52 @@ + + + + + beforeRunningCommand + saveModifiedFiles + command + #!usr/bin/env ruby18 + +# -- Imports ------------------------------------------------------------------- + +require ENV['TM_BUNDLE_SUPPORT'] + '/lib/rubocop' + +# -- Main ---------------------------------------------------------------------- + +RuboCop.reformat + + input + document + inputFormat + text + keyEquivalent + ^H + name + Reformat Document + outputCaret + heuristic + outputFormat + text + outputLocation + discard + requiredCommands + + + command + rubocop + locations + + /usr/local/bin/rubocop + $HOME/.rbenv/shims/rubocop + $HOME/.rvm/scripts/rvm + + + + scope + source.ruby + uuid + 64BB4EE2-2803-410C-B140-EA545A13F250 + version + 2 + + \ No newline at end of file diff --git a/Support/lib/rubocop.rb b/Support/lib/rubocop.rb new file mode 100644 index 0000000..2929d66 --- /dev/null +++ b/Support/lib/rubocop.rb @@ -0,0 +1,82 @@ +# rubocop: disable AsciiComments +# rubocop: disable Style/HashSyntax + +# -- Imports ------------------------------------------------------------------- + +require ENV['TM_BUNDLE_SUPPORT'] + '/lib/executable' + +require ENV['TM_SUPPORT_PATH'] + '/lib/exit_codes' +require ENV['TM_SUPPORT_PATH'] + '/lib/progress' +require ENV['TM_SUPPORT_PATH'] + '/lib/tm/detach' +require ENV['TM_SUPPORT_PATH'] + '/lib/tm/save_current_document' + +# -- Module -------------------------------------------------------------------- + +# This module allows you to reformat files via RuboCop. +module RuboCop + class << self + # This function reformats the current TextMate document using RuboCop. + # + # It works both on saved and unsaved files: + # + # 1. In the case of an unsaved files this method will stall until RuboCop + # fixed the file. While this process takes place the method displays a + # progress bar. + # + # 2. If the current document is a file saved somewhere on your disk, then + # the method will not wait until RuboCop is finished. Instead it will run + # RuboCop in the background. This has the advantage, that you can still + # work inside TextMate, while RuboCop works on the document. + # + # After RuboCop finished reformatting the file, the method will inform you + # – via a tooltip – about the problems RuboCop found in the *previous* + # version of the document. + def reformat + unsaved_file = true unless ENV['TM_FILEPATH'] + TextMate.save_if_untitled('rb') + format_file(locate_rubocop, unsaved_file) + end + + private + + def locate_rubocop + Dir.chdir(ENV['TM_PROJECT_DIRECTORY'] || + File.dirname(ENV['TM_FILEPATH'].to_s)) + begin + Executable.find('rubocop').join ' ' + rescue Executable::NotFound => error + return 'rubocop' if File.executable?(`which rubocop`.rstrip) + TextMate.exit_show_tool_tip(error.message) + end + end + + def format_file(rubocop, unsaved_file) + aha = `which aha`.rstrip.match(/.+/) + output_format = aha ? :html : :text + command = "#{rubocop} -a \"$TM_FILEPATH\" #{'--color | aha' if aha} 2>&1" + if unsaved_file + format_unsaved(command, output_format) + else + format_saved(command, output_format) + end + end + + def format_unsaved(rubocop_command, output_format) + output, success = TextMate.call_with_progress( + :title => '🤖 RuboCop', :summary => 'Reformatting File' + ) do + [`#{rubocop_command}`, $CHILD_STATUS.success?] + end + TextMate.exit_show_tool_tip(output) unless success + TextMate::UI.tool_tip(output, :format => output_format) + TextMate.exit_replace_document(File.read(ENV['TM_FILEPATH'])) + end + + def format_saved(rubocop_command, output_format) + TextMate.detach do + output = `#{rubocop_command}` + TextMate::UI.tool_tip(output, :format => output_format) + end + end + end +end \ No newline at end of file diff --git a/info.plist b/info.plist index 4961e89..d61f440 100644 --- a/info.plist +++ b/info.plist @@ -31,6 +31,7 @@ ------------------------------------ 8646378E-91F5-4771-AC7C-43FC49A93576 EE5F19BA-6C02-11D9-92BA-0011242E4184 + 64BB4EE2-2803-410C-B140-EA545A13F250 ------------------------------------ EE5F1FB2-6C02-11D9-92BA-0011242E4184 FBFC214F-B019-4967-95D2-028F374A3221