Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added 'Beautify all changed' command

  • Loading branch information...
commit d8fd1de6a4b779dfd55dd33d78fbcf32bdd0361a 1 parent e9ed06c
@mocoso authored
View
67 Commands/Beautify all changed.tmCommand
@@ -0,0 +1,67 @@
+<?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>nop</string>
+ <key>command</key>
+ <string>#!/usr/bin/env ruby
+# see Help / Environment Variables for the full list
+begin
+ require File.join(ENV['TM_SUPPORT_PATH'], 'lib', 'exit_codes')
+ require File.join(ENV['TM_BUNDLE_SUPPORT'], 'lib', 'rbeautify')
+
+ require 'rubygems'
+ require 'grit'
+ include Grit
+
+ if ENV['TM_PROJECT_DIRECTORY']
+ repo = Repo.new(ENV['TM_PROJECT_DIRECTORY'])
+
+ files = (repo.status.added + repo.status.changed).map{ |file| file.first }.select{ |file_name| file_name.match /\.rb$/ }
+
+ puts "&lt;h1&gt;Beautifying modified ruby files&lt;/h1&gt;"
+
+ if files.empty?
+ puts "&lt;p&gt;No changed or added ruby files.&lt;/p&gt;"
+
+ else
+ puts "&lt;p&gt;Updated:&lt;/p&gt;"
+ puts "&lt;ol&gt;"
+ files.each do |file_name|
+ message = file_name.dup
+ begin
+ if RBeautify.beautify_file(File.join(ENV['TM_PROJECT_DIRECTORY'], file_name))
+ message &lt;&lt; ' - beautifed'
+ else
+ message &lt;&lt; ' - no updates required'
+ end
+ rescue Exception =&gt; e
+ message &lt;&lt; " - threw exception #{e}"
+ end
+ puts "&lt;li&gt;#{message}&lt;/li&gt;"
+ end
+ puts "&lt;/o&gt;"
+ end
+
+ else
+ TextMate.exit_show_tool_tip "No project directory."
+ end
+
+rescue Exception =&gt; e
+ TextMate.exit_show_tool_tip "There is a problem beautifying this source: #{e}"
+end</string>
+ <key>input</key>
+ <string>none</string>
+ <key>keyEquivalent</key>
+ <string>^~@b</string>
+ <key>name</key>
+ <string>Beautify all changed</string>
+ <key>output</key>
+ <string>showAsHTML</string>
+ <key>scope</key>
+ <string>source.ruby</string>
+ <key>uuid</key>
+ <string>B6DE0B79-ECEC-4E9C-AA1F-4619F91A081A</string>
+</dict>
+</plist>
View
7 Readme.markdown
@@ -13,6 +13,13 @@ Run this:
Then select 'Bundles > Bundle Editor > Reload Bundles' from Textmate's menus
+## Dependencies
+
+The 'Beautify all changed' command relies on
+
+ - Your project using Git for source control
+ - You installing the Grit gem (sudo gem install mojombo-grit)
+
## Credits
This was based on the [ruby beautifier script][rbs] by Paul Lutus and [Beautiful Ruby in Textmate][brit] by Tim Burks
View
13 Support/lib/rbeautify.rb
@@ -22,7 +22,7 @@
module RBeautify
- def RBeautify.beautify_string(source, path = "")
+ def RBeautify.beautify_string(source)
dest = ""
previous_line = nil
line = nil
@@ -40,19 +40,22 @@ def RBeautify.beautify_string(source, path = "")
return dest
end
- def RBeautify.beautify_file(path)
+ def RBeautify.beautify_file(path, backup = false)
if(path == '-') # stdin source
source = STDIN.read
print beautify_string(source,"stdin")
else # named file source
source = File.read(path)
- dest = beautify_string(source,path)
+ dest = beautify_string(source)
if(source != dest)
- # make a backup copy
- File.open(path + "~","w") { |f| f.write(source) }
+ if backup
+ # make a backup copy
+ File.open(path + "~","w") { |f| f.write(source) }
+ end
# overwrite the original
File.open(path,"w") { |f| f.write(dest) }
end
+ return source != dest
end
end # beautify_file
View
5 info.plist
@@ -4,6 +4,11 @@
<dict>
<key>name</key>
<string>Code Beautifier</string>
+ <key>ordering</key>
+ <array>
+ <string>374B6862-84D9-4169-8B42-E090EEF63DC2</string>
+ <string>B6DE0B79-ECEC-4E9C-AA1F-4619F91A081A</string>
+ </array>
<key>uuid</key>
<string>00A79803-4522-481E-BE42-B2F96AD2E0D2</string>
</dict>

1 comment on commit d8fd1de

@pervel

Using the shortcut &#x2303;&#x2325;&#x2318;B is not such a good idea as it defaults to Show Bundle Editor.

Please sign in to comment.
Something went wrong with that request. Please try again.