Skip to content

Commit c39ae9e

Browse files
committed
Reformat Document: Also reformat unsaved files
This new version of “Reformat Document” now also reformats “unsaved” files. This means you can now also use the command on Python code in the bundle editor.
1 parent 6b3df8c commit c39ae9e

File tree

2 files changed

+92
-12
lines changed

2 files changed

+92
-12
lines changed

Commands/Reformat Document.tmCommand

+10-12
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,32 @@
33
<plist version="1.0">
44
<dict>
55
<key>beforeRunningCommand</key>
6-
<string>nop</string>
6+
<string>saveModifiedFiles</string>
77
<key>command</key>
8-
<string>#!/bin/bash
8+
<string>#!/usr/bin/env ruby18
99
10-
. "$TM_SUPPORT_PATH/lib/bash_init.sh"
10+
# -- Imports -------------------------------------------------------------------
1111
12-
yapf_output=`"${TM_YAPF:-yapf}" --style="${TM_YAPF_STYLE:-pep8}" &lt; /dev/stdin`
13-
exit_status=$?
12+
require ENV['TM_BUNDLE_SUPPORT'] + '/lib/yapf'
1413
15-
if [ $exit_status -ne 0 ]; then
16-
exit_show_tool_tip "$yapf_output"
17-
fi
14+
# -- Main ----------------------------------------------------------------------
1815
19-
echo "$yapf_output"</string>
16+
YAPF.reformat
17+
</string>
2018
<key>input</key>
21-
<string>selection</string>
19+
<string>document</string>
2220
<key>inputFormat</key>
2321
<string>text</string>
2422
<key>keyEquivalent</key>
2523
<string>^H</string>
2624
<key>name</key>
2725
<string>Reformat Document</string>
2826
<key>outputCaret</key>
29-
<string>interpolateByChar</string>
27+
<string>heuristic</string>
3028
<key>outputFormat</key>
3129
<string>text</string>
3230
<key>outputLocation</key>
33-
<string>replaceInput</string>
31+
<string>discard</string>
3432
<key>requiredCommands</key>
3533
<array>
3634
<dict>

Support/lib/yapf.rb

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# rubocop: disable Style/HashSyntax
2+
3+
# -- Imports -------------------------------------------------------------------
4+
5+
require ENV['TM_SUPPORT_PATH'] + '/lib/exit_codes'
6+
require ENV['TM_SUPPORT_PATH'] + '/lib/progress'
7+
require ENV['TM_SUPPORT_PATH'] + '/lib/tm/detach'
8+
require ENV['TM_SUPPORT_PATH'] + '/lib/tm/save_current_document'
9+
10+
# -- Module --------------------------------------------------------------------
11+
12+
# This module allows us to reformat a file via YAPF.
13+
module YAPF
14+
class << self
15+
# This function reformats the current TextMate document using YAPF.
16+
#
17+
# It works both on saved and unsaved files:
18+
#
19+
# 1. In the case of an unsaved files this method will stall until YAPF
20+
# fixed the file. While this process takes place the method displays a
21+
# progress bar.
22+
#
23+
# 2. If the current document is a file saved somewhere on your disk, then
24+
# the method will not wait until YAPF is finished. Instead it will run
25+
# YAPF in the background. This has the advantage, that you can still
26+
# work inside TextMate, while YAPF works on the document.
27+
def reformat
28+
unsaved_file = true unless ENV['TM_FILEPATH']
29+
TextMate.save_if_untitled('py')
30+
format_file(locate_yapf, unsaved_file)
31+
end
32+
33+
private
34+
35+
def locate_yapf
36+
Dir.chdir(ENV['TM_PROJECT_DIRECTORY'] ||
37+
File.dirname(ENV['TM_FILEPATH'].to_s))
38+
yapf = ENV['TM_YAPF'] || 'yapf'
39+
return yapf if File.executable?(`which #{yapf}`.rstrip)
40+
TextMate.exit_show_tool_tip(
41+
'Could not locate YAPF. Please make sure that you set TM_YAPF ' \
42+
"correctly.\nTM_YAPF: “#{ENV['TM_YAPF']}”"
43+
)
44+
end
45+
46+
def format_file(yapf, unsaved_file)
47+
style = ENV['TM_YAPF_STYLE'] || 'pep8'
48+
filepath = ENV['TM_FILEPATH']
49+
command = "#{yapf} -i --style=#{style} \"$TM_FILEPATH\" 2>&1"
50+
error_message = "YAPF was not able to reformat the file: \n\n"
51+
if unsaved_file
52+
format_unsaved(command, error_message, filepath)
53+
else
54+
format_saved(command, error_message, filepath)
55+
end
56+
end
57+
58+
def format_unsaved(yapf_command, error_message, filepath)
59+
output, success = TextMate.call_with_progress(
60+
:title => '🐍 YAPF', :summary => 'Reformatting File'
61+
) do
62+
[`#{yapf_command}`, $CHILD_STATUS.success?]
63+
end
64+
TextMate.exit_show_tool_tip(error_message + output) unless success
65+
TextMate::UI.tool_tip(output) unless output.empty?
66+
TextMate.exit_replace_document(File.read(filepath))
67+
end
68+
69+
def format_saved(yapf_command, error_message, filepath)
70+
TextMate.detach do
71+
output = `#{yapf_command}`
72+
if $CHILD_STATUS.success?
73+
output = (":\n\n" + output) unless output.empty?
74+
message = "Reformatted “#{File.basename(filepath)}#{output}"
75+
TextMate::UI.tool_tip(message)
76+
else
77+
TextMate::UI.tool_tip(error_message + output)
78+
end
79+
end
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)