forked from textmate/ruby.tmbundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command: Reformat Document (⌃⇧H)
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
1 parent
11ad1b9
commit 59724dc
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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> |
This file contains 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
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 |
This file contains 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