diff --git a/Commands/Check Python Syntax.plist b/Commands/Check Python Syntax.plist index 5272b2a..7adeb30 100644 --- a/Commands/Check Python Syntax.plist +++ b/Commands/Check Python Syntax.plist @@ -11,7 +11,7 @@ command #!/bin/bash -TPY=${TM_PYTHON:-python} +TPY=${TM_PYTHON:-"/usr/bin/python"} "$TPY" "$TM_BUNDLE_SUPPORT/bin/pycheckmate.py" "$TM_FILEPATH" fileCaptureRegister diff --git a/Commands/Documentation for Current Word.tmCommand b/Commands/Documentation for Current Word.tmCommand index 3f37782..94ccf22 100644 --- a/Commands/Documentation for Current Word.tmCommand +++ b/Commands/Documentation for Current Word.tmCommand @@ -16,7 +16,7 @@ export TM_FIRST_LINE="$first_line" export PYTHONPATH="$TM_BUNDLE_SUPPORT/DocMate" export PYTHONPATH="$TM_SUPPORT_PATH/lib:$PYTHONPATH" -/usr/bin/env python -S - <<PYTHON +/usr/bin/python -S - <<PYTHON # coding: UTF-8 import sys from sys import exit diff --git a/Commands/Documentation in Browser.plist b/Commands/Documentation in Browser.plist index 8e9efe6..19c384a 100644 --- a/Commands/Documentation in Browser.plist +++ b/Commands/Documentation in Browser.plist @@ -7,7 +7,7 @@ command #!/bin/bash -TPY=${TM_PYTHON:-python} +TPY=${TM_PYTHON:-"/usr/bin/python"} echo '<html><body>' "$TPY" "${TM_BUNDLE_SUPPORT}/browse_pydocs.py" @@ -17,7 +17,7 @@ echo '</body></html>' inputFormat text keyEquivalent - ^H + ~@h name Documentation in Browser outputCaret diff --git a/Commands/Reformat Document.tmCommand b/Commands/Reformat Document.tmCommand new file mode 100644 index 0000000..f94b051 --- /dev/null +++ b/Commands/Reformat Document.tmCommand @@ -0,0 +1,51 @@ + + + + + beforeRunningCommand + saveModifiedFiles + command + #!/usr/bin/env ruby18 + +# -- Imports ------------------------------------------------------------------- + +require ENV['TM_BUNDLE_SUPPORT'] + '/lib/yapf' + +# -- Main ---------------------------------------------------------------------- + +YAPF.reformat + + input + document + inputFormat + text + keyEquivalent + ^H + name + Reformat Document + outputCaret + heuristic + outputFormat + text + outputLocation + discard + requiredCommands + + + command + yapf + locations + + $HOME/.pyenv/shims/yapf + /usr/local/bin/yapf + + + + scope + source.python + uuid + CA6F6269-AD9D-46DB-A9A1-7A9987C4E6EA + version + 2 + + diff --git a/Support/lib/yapf.rb b/Support/lib/yapf.rb new file mode 100644 index 0000000..447efbd --- /dev/null +++ b/Support/lib/yapf.rb @@ -0,0 +1,82 @@ +# rubocop: disable Style/HashSyntax + +# -- Imports ------------------------------------------------------------------- + +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 us to reformat a file via YAPF. +module YAPF + class << self + # This function reformats the current TextMate document using YAPF. + # + # It works both on saved and unsaved files: + # + # 1. In the case of an unsaved files this method will stall until YAPF + # 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 YAPF is finished. Instead it will run + # YAPF in the background. This has the advantage, that you can still + # work inside TextMate, while YAPF works on the document. + def reformat + unsaved_file = true unless ENV['TM_FILEPATH'] + TextMate.save_if_untitled('py') + format_file(locate_yapf, unsaved_file) + end + + private + + def locate_yapf + Dir.chdir(ENV['TM_PROJECT_DIRECTORY'] || + File.dirname(ENV['TM_FILEPATH'].to_s)) + yapf = ENV['TM_YAPF'] || 'yapf' + return yapf if File.executable?(`which #{yapf}`.rstrip) + TextMate.exit_show_tool_tip( + 'Could not locate YAPF. Please make sure that you set TM_YAPF ' \ + "correctly.\nTM_YAPF: “#{ENV['TM_YAPF']}”" + ) + end + + def format_file(yapf, unsaved_file) + style = ENV['TM_YAPF_STYLE'] || 'pep8' + filepath = ENV['TM_FILEPATH'] + command = "#{yapf} -i --style=#{style} \"$TM_FILEPATH\" 2>&1" + error_message = "YAPF was not able to reformat the file: \n\n" + if unsaved_file + format_unsaved(command, error_message, filepath) + else + format_saved(command, error_message, filepath) + end + end + + def format_unsaved(yapf_command, error_message, filepath) + output, success = TextMate.call_with_progress( + :title => '🐍 YAPF', :summary => 'Reformatting File' + ) do + [`#{yapf_command}`, $CHILD_STATUS.success?] + end + TextMate.exit_show_tool_tip(error_message + output) unless success + TextMate::UI.tool_tip(output) unless output.empty? + TextMate.exit_replace_document(File.read(filepath)) + end + + def format_saved(yapf_command, error_message, filepath) + TextMate.detach do + output = `#{yapf_command}` + if $CHILD_STATUS.success? + output = (":\n\n" + output) unless output.empty? + message = "✨ Reformatted “#{File.basename(filepath)}”#{output}" + TextMate::UI.tool_tip(message) + else + TextMate::UI.tool_tip(error_message + output) + end + end + end + end +end diff --git a/info.plist b/info.plist index b8001c5..88f7e6a 100644 --- a/info.plist +++ b/info.plist @@ -32,6 +32,7 @@ 504278F6-89F4-11D9-9326-000D93B6E43C 09E7930D-E706-4C90-B37E-5B95E1D97949 44C9C59C-89F9-11D9-9326-000D93B6E43C + CA6F6269-AD9D-46DB-A9A1-7A9987C4E6EA 95FFEECE-73E4-4B33-9CAE-1641C62FFBC0 ------------------------------------ 095E8342-FAED-4B95-A229-E245B0B601A7