Skip to content

Reformat Document & Minor Improvements #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Commands/Check Python Syntax.plist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<key>command</key>
<string>#!/bin/bash

TPY=${TM_PYTHON:-python}
TPY=${TM_PYTHON:-"/usr/bin/python"}

"$TPY" "$TM_BUNDLE_SUPPORT/bin/pycheckmate.py" "$TM_FILEPATH"</string>
<key>fileCaptureRegister</key>
Expand Down
2 changes: 1 addition & 1 deletion Commands/Documentation for Current Word.tmCommand
Original file line number Diff line number Diff line change
Expand Up @@ -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 - &lt;&lt;PYTHON
/usr/bin/python -S - &lt;&lt;PYTHON
# coding: UTF-8
import sys
from sys import exit
Expand Down
4 changes: 2 additions & 2 deletions Commands/Documentation in Browser.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>command</key>
<string>#!/bin/bash

TPY=${TM_PYTHON:-python}
TPY=${TM_PYTHON:-"/usr/bin/python"}

echo '&lt;html&gt;&lt;body&gt;'
"$TPY" "${TM_BUNDLE_SUPPORT}/browse_pydocs.py"
Expand All @@ -17,7 +17,7 @@ echo '&lt;/body&gt;&lt;/html&gt;'</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>^H</string>
<string>~@h</string>
<key>name</key>
<string>Documentation in Browser</string>
<key>outputCaret</key>
Expand Down
51 changes: 51 additions & 0 deletions Commands/Reformat Document.tmCommand
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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/yapf'

# -- Main ----------------------------------------------------------------------

YAPF.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>yapf</string>
<key>locations</key>
<array>
<string>$HOME/.pyenv/shims/yapf</string>
<string>/usr/local/bin/yapf</string>
</array>
</dict>
</array>
<key>scope</key>
<string>source.python</string>
<key>uuid</key>
<string>CA6F6269-AD9D-46DB-A9A1-7A9987C4E6EA</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
82 changes: 82 additions & 0 deletions Support/lib/yapf.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<string>504278F6-89F4-11D9-9326-000D93B6E43C</string>
<string>09E7930D-E706-4C90-B37E-5B95E1D97949</string>
<string>44C9C59C-89F9-11D9-9326-000D93B6E43C</string>
<string>CA6F6269-AD9D-46DB-A9A1-7A9987C4E6EA</string>
<string>95FFEECE-73E4-4B33-9CAE-1641C62FFBC0</string>
<string>------------------------------------</string>
<string>095E8342-FAED-4B95-A229-E245B0B601A7</string>
Expand Down