Skip to content

Commit

Permalink
Add command: Reformat Document (⌃⇧H)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sanssecours authored and infininight committed May 30, 2017
1 parent 11ad1b9 commit 59724dc
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Commands/Reformat Document.tmCommand
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>saveModifiedFiles</string>
<key>command</key>
<string>#!usr/bin/env ruby18
# -- Imports -------------------------------------------------------------------
require ENV['TM_BUNDLE_SUPPORT'] + '/lib/rubocop'
# -- Main ----------------------------------------------------------------------
RuboCop.reformat
</string>
<key>input</key>
<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>heuristic</string>
<key>outputFormat</key>
<string>text</string>
<key>outputLocation</key>
<string>discard</string>
<key>requiredCommands</key>
<array>
<dict>
<key>command</key>
<string>rubocop</string>
<key>locations</key>
<array>
<string>/usr/local/bin/rubocop</string>
<string>$HOME/.rbenv/shims/rubocop</string>
<string>$HOME/.rvm/scripts/rvm</string>
</array>
</dict>
</array>
<key>scope</key>
<string>source.ruby</string>
<key>uuid</key>
<string>64BB4EE2-2803-410C-B140-EA545A13F250</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
82 changes: 82 additions & 0 deletions Support/lib/rubocop.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<string>------------------------------------</string>
<string>8646378E-91F5-4771-AC7C-43FC49A93576</string>
<string>EE5F19BA-6C02-11D9-92BA-0011242E4184</string>
<string>64BB4EE2-2803-410C-B140-EA545A13F250</string>
<string>------------------------------------</string>
<string>EE5F1FB2-6C02-11D9-92BA-0011242E4184</string>
<string>FBFC214F-B019-4967-95D2-028F374A3221</string>
Expand Down

0 comments on commit 59724dc

Please sign in to comment.