|
| 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