Skip to content

Commit

Permalink
“Reformat Document”: Also reformat unsaved files
Browse files Browse the repository at this point in the history
This new version of “Reformat Document” now also reformats “unsaved”
files. This means you can now also use the command on Ruby files in the
bundle editor.
  • Loading branch information
sanssecours committed Apr 20, 2017
1 parent 6eb65b4 commit bbb2226
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 37 deletions.
38 changes: 9 additions & 29 deletions Commands/Reformat Document.tmCommand
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,28 @@
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>saveActiveFile</string>
<string>saveModifiedFiles</string>
<key>command</key>
<string>#!/bin/bash
<string>#!usr/bin/env ruby18
cd "${TM_PROJECT_DIRECTORY:-$(dirname "$TM_FILEPATH")}"
[[ -f "$HOME/.rvm/scripts/rvm" ]] &amp;&amp; . "$HOME/.rvm/scripts/rvm"
# -- Imports -------------------------------------------------------------------
(
if [[ -f "Gemfile" ]]; then
bundle list rubocop &amp;&amp; bundler_rubocop='bundle exec rubocop'
fi
require ENV['TM_BUNDLE_SUPPORT'] + '/lib/rubocop'
if [[ ! `which rubocop` &amp;&amp; -z "$bundler_rubocop" ]]; then
"$DIALOG" tooltip --text "This command requires RuboCop.
Please install RuboCop via “gem install rubocop”."
exit
fi
# -- Main ----------------------------------------------------------------------
aha_found=`which aha`
if [[ $aha_found ]]; then
color='n';
format='--html'
else
format='--text'
fi
rubocop="${bundler_rubocop:-"${TM_RUBOCOP:-rubocop}"}"
output=`$rubocop -a$color "$TM_FILEPATH"`
if [[ $aha_found ]]; then output=`echo "$output" | aha`; fi
"$DIALOG" tooltip "$format" "$output"
) &amp;&gt;/dev/null &amp;</string>
RuboCop.reformat
</string>
<key>input</key>
<string>none</string>
<string>document</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>^H</string>
<key>name</key>
<string>Reformat Document</string>
<key>outputCaret</key>
<string>interpolateByChar</string>
<string>heuristic</string>
<key>outputFormat</key>
<string>text</string>
<key>outputLocation</key>
Expand Down
18 changes: 10 additions & 8 deletions Support/help/help.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ The command “Reformat Document” – accessible via <kbd>^</kbd> + <kbd>⇧</

## RuboCop Version

Which version of [RuboCop][] “Reformat Document” uses depends on your environment. The command will try the following options in the given order:
Which version of [RuboCop][] “Reformat Document” uses depends on your environment. The command will try the options below in the given order:

1. The version of `rubocop` installed via [Bundler][].
2. The value of the path specified in `TM_RUBOCOP`.
3. The version of `rubocop` installed via [RVM][]. (The command assumes that you installed RVM in your home directory.)
4. Any other version of `rubocop` accessible via
1. `/usr/local/bin/rubocop`,
2. `$HOME/.rbenv/shims/rubocop` or
3. the locations in your `PATH`.
1. The value of the command specified via `TM_RUBOCOP`.
2. A executable version of RuboCop accessible via `bin/rubocop`
3. The version of `rubocop` installed via [Bundler][].
4. Any other version of `rubocop` accessible via,
- `/usr/local/bin/rubocop`,
- `$HOME/.rbenv/shims/rubocop`
- or the locations in your `PATH`.

“Reformat Document” prefers [RVM][] install location of `rubocop` for all of the applicable options above.

[Bundler]: https://bundler.io
[RVM]: https://rvm.io
83 changes: 83 additions & 0 deletions Support/lib/rubocop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# 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
output_format = aha ? :html : :text
command = "#{rubocop} -a#{'n' if aha} \"$TM_FILEPATH\"" \
"#{' | 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

0 comments on commit bbb2226

Please sign in to comment.