From 3e390292d521775cd69e6086e5f56233d25dc425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rene=CC=81=20Schwaiger?= Date: Wed, 20 Apr 2016 14:35:58 +0200 Subject: [PATCH 1/4] Hardcode path to Python executable Before this change some commands would not work if `python` linked to a recent version of Python (3.x) instead of the legacy version of Python provided by Apple (2.x). --- Commands/Check Python Syntax.plist | 2 +- Commands/Documentation for Current Word.tmCommand | 2 +- Commands/Documentation in Browser.plist | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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..521d581 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" From 0f7f92fdb88957adebbb7be61dc79eddf8c23efa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rene=CC=81=20Schwaiger?= Date: Thu, 21 Apr 2016 20:21:59 +0200 Subject: [PATCH 2/4] Add command to reformat the current file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To format the current file via [yapf](https://github.com/google/yapf) use the keyboard shortcut `^` + `⇧` + `H`. --- Commands/Reformat Document.tmCommand | 53 ++++++++++++++++++++++++++++ info.plist | 1 + 2 files changed, 54 insertions(+) create mode 100644 Commands/Reformat Document.tmCommand diff --git a/Commands/Reformat Document.tmCommand b/Commands/Reformat Document.tmCommand new file mode 100644 index 0000000..a696178 --- /dev/null +++ b/Commands/Reformat Document.tmCommand @@ -0,0 +1,53 @@ + + + + + beforeRunningCommand + nop + command + #!/bin/bash + +. "$TM_SUPPORT_PATH/lib/bash_init.sh" + +yapf_output=`"${TM_YAPF:-yapf}" --style="${TM_YAPF_STYLE:-pep8}" < /dev/stdin` +exit_status=$? + +if [ $exit_status -ne 0 ]; then + exit_show_tool_tip "$yapf_output" +fi + +echo "$yapf_output" + input + selection + inputFormat + text + keyEquivalent + ^H + name + Reformat Document + outputCaret + interpolateByChar + outputFormat + text + outputLocation + replaceInput + 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/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 From 6b3df8ca4079530e2d17459452b527074ce20c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rene=CC=81=20Schwaiger?= Date: Thu, 21 Apr 2016 20:30:04 +0200 Subject: [PATCH 3/4] =?UTF-8?q?=E2=80=9CDocumentation=20in=20Browser?= =?UTF-8?q?=E2=80=9D:=20Change=20shortcut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new shortcut is `⌥` + `⌘` + `H`. We changed the shortcut since now “Reformat Document” uses the old shortcut `^` + `⇧` + `H`. This change makes sense, since many other bundles use the latter shortcut to tidy (LaTeX, Perl, HTML) or reformat (C, JavaScript, JSON) the current document. --- Commands/Documentation in Browser.plist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Commands/Documentation in Browser.plist b/Commands/Documentation in Browser.plist index 521d581..19c384a 100644 --- a/Commands/Documentation in Browser.plist +++ b/Commands/Documentation in Browser.plist @@ -17,7 +17,7 @@ echo '</body></html>' inputFormat text keyEquivalent - ^H + ~@h name Documentation in Browser outputCaret From 89be444ba7da0a729034c1590ec8ba48a0c9bd6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rene=CC=81=20Schwaiger?= Date: Mon, 28 Nov 2016 10:46:28 +0100 Subject: [PATCH 4/4] Reformat Document: Also reformat unsaved files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Commands/Reformat Document.tmCommand | 22 ++++---- Support/lib/yapf.rb | 82 ++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 Support/lib/yapf.rb diff --git a/Commands/Reformat Document.tmCommand b/Commands/Reformat Document.tmCommand index a696178..f94b051 100644 --- a/Commands/Reformat Document.tmCommand +++ b/Commands/Reformat Document.tmCommand @@ -3,22 +3,20 @@ beforeRunningCommand - nop + saveModifiedFiles command - #!/bin/bash + #!/usr/bin/env ruby18 -. "$TM_SUPPORT_PATH/lib/bash_init.sh" +# -- Imports ------------------------------------------------------------------- -yapf_output=`"${TM_YAPF:-yapf}" --style="${TM_YAPF_STYLE:-pep8}" < /dev/stdin` -exit_status=$? +require ENV['TM_BUNDLE_SUPPORT'] + '/lib/yapf' -if [ $exit_status -ne 0 ]; then - exit_show_tool_tip "$yapf_output" -fi +# -- Main ---------------------------------------------------------------------- -echo "$yapf_output" +YAPF.reformat + input - selection + document inputFormat text keyEquivalent @@ -26,11 +24,11 @@ echo "$yapf_output" name Reformat Document outputCaret - interpolateByChar + heuristic outputFormat text outputLocation - replaceInput + discard requiredCommands 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