Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 67f0d1c6ac43d1f0fa70b2b42a1517cbb79749ca @CraigWilliams committed Dec 27, 2011
Showing with 231 additions and 0 deletions.
  1. +3 −0 Default (OSX).sublime-keymap
  2. +23 −0 README.md
  3. +27 −0 beautify_ruby.py
  4. +21 −0 license.txt
  5. +157 −0 ruby/beautifier.rb
@@ -0,0 +1,3 @@
+[
+ { "keys": ["super+k"], "command": "beautify_ruby" }
+]
@@ -0,0 +1,23 @@
+# BeautifyRuby
+
+Beautifies Ruby code. This plugin uses the [Ruby Script Beautifier](http://www.arachnoid.com/ruby/rubyBeautifier.html) written by P.Lotus
+
+I made very little modification to get it to work with a Sublime Text 2 plugin.
+
+### Key Binding
+
+```
+ cmd + k
+```
+
+### Installation
+Install this plugin using [Package Control](http://wbond.net/sublime_packages/package_control), a
+package manager for Sublime Text 2.
+
+In Sublime Text 2, press "cmd + shift + p" and then type "install".
+
+Once you see "Package Control: Install Package", enter.
+
+When the packages load, another selection window will appear. Type
+
+BeautifyRuby and enter. All done!
@@ -0,0 +1,27 @@
+import os.path
+from os import popen
+import sublime, sublime_plugin
+
+class BeautifyRubyCommand(sublime_plugin.TextCommand):
+ def run(self, edit):
+ ext = os.path.basename(self.view.file_name())
+
+ if ext.endswith(".rb"):
+ ruby_script = self.ruby_script()
+ current_file = self.view.file_name()
+ args = "/usr/bin/ruby '" + ruby_script + "' '" + unicode(current_file) + "'"
+ beautified = os.popen(args).read()
+
+ self.update_view(beautified)
+ else:
+ sublime.error_message("This is not a Ruby file.")
+
+ def ruby_script(self):
+ return os.path.join(sublime.packages_path(), 'BeautifyRuby', 'Ruby', 'beautifier.rb')
+
+ def update_view(self, contents):
+ body = self.view.window().active_view().substr(sublime.Region(0, self.view.window().active_view().size()))
+ edit = self.view.window().active_view().begin_edit()
+ self.view.window().active_view().erase(edit, sublime.Region(0, self.view.window().active_view().size()))
+ self.view.window().active_view().insert(edit, 0, contents)
+ self.view.window().active_view().end_edit(edit)
@@ -0,0 +1,21 @@
+All of BeautifyRuby is licensed under the MIT license.
+
+Copyright (c) 2011 Craig Williams <cwilliams.allancraig@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,157 @@
+#!/usr/bin/env ruby
+
+# Ruby beautifier, version 2.1, 09/11/2006
+# Copyright (c) 2006, P. Lutus
+# Released under the GPL
+
+# Changes
+# Craig Williams
+# 12/26/2011
+# Modified (very minor) to work with a Sublime Text 2 plugin
+
+$tabSize = 2
+$tabStr = " "
+
+# indent regexp tests
+
+$indentExp = [
+ /^module\b/,
+ /^if\b/,
+ /(=\s*|^)until\b/,
+ /(=\s*|^)for\b/,
+ /^unless\b/,
+ /(=\s*|^)while\b/,
+ /(=\s*|^)begin\b/,
+ /(^| )case\b/,
+ /\bthen\b/,
+ /^class\b/,
+ /^rescue\b/,
+ /^def\b/,
+ /\bdo\b/,
+ /^else\b/,
+ /^elsif\b/,
+ /^ensure\b/,
+ /\bwhen\b/,
+ /\{[^\}]*$/,
+ /\[[^\]]*$/
+]
+
+# outdent regexp tests
+
+$outdentExp = [
+ /^rescue\b/,
+ /^ensure\b/,
+ /^elsif\b/,
+ /^end\b/,
+ /^else\b/,
+ /\bwhen\b/,
+ /^[^\{]*\}/,
+ /^[^\[]*\]/
+]
+
+def makeTab(tab)
+ return (tab < 0) ? "" : $tabStr * $tabSize * tab
+end
+
+def addLine(line,tab)
+ line.strip!
+ line = makeTab(tab)+line if line.length > 0
+ return line + "\n"
+end
+
+def beautifyRuby(contents)
+ commentBlock = false
+ programEnd = false
+ multiLineArray = Array.new
+ multiLineStr = ""
+ tab = 0
+ source = contents
+ dest = ""
+ source.split("\n").each do |line|
+ if(!programEnd)
+ # detect program end mark
+ if(line =~ /^__END__$/)
+ programEnd = true
+ else
+ # combine continuing lines
+ if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
+ multiLineArray.push line
+ multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
+ next
+ end
+
+ # add final line
+ if(multiLineStr.length > 0)
+ multiLineArray.push line
+ multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
+ end
+
+ tline = ((multiLineStr.length > 0)?multiLineStr:line).strip
+ if(tline =~ /^=begin/)
+ commentBlock = true
+ end
+ end
+ end
+ if(commentBlock || programEnd)
+ # add the line unchanged
+ dest += line + "\n"
+ else
+ commentLine = (tline =~ /^#/)
+ if(!commentLine)
+ # throw out sequences that will
+ # only sow confusion
+ while tline.gsub!(/'.*?'/,"")
+ end
+ while tline.gsub!(/".*?"/,"")
+ end
+ while tline.gsub!(/\`.*?\`/,"")
+ end
+ while tline.gsub!(/\{[^\{]*?\}/,"")
+ end
+ while tline.gsub!(/\([^\(]*?\)/,"")
+ end
+ while tline.gsub!(/\/.*?\//,"")
+ end
+ while tline.gsub!(/%r(.).*?\1/,"")
+ end
+ tline.gsub!(/\\\"/,"'")
+ $outdentExp.each do |re|
+ if(tline =~ re)
+ tab -= 1
+ break
+ end
+ end
+ end
+ if (multiLineArray.length > 0)
+ multiLineArray.each do |ml|
+ dest += addLine(ml,tab)
+ end
+ multiLineArray.clear
+ multiLineStr = ""
+ else
+ dest += addLine(line,tab)
+ end
+ if(!commentLine)
+ $indentExp.each do |re|
+ if(tline =~ re && !(tline =~ /\s+end\s*$/))
+ tab += 1
+ break
+ end
+ end
+ end
+ end
+ if(tline =~ /^=end/)
+ commentBlock = false
+ end
+ end
+ STDOUT.write(dest)
+ # uncomment this to complain about mismatched blocks
+ # if(tab != 0)
+ # STDERR.puts "#{path}: Indentation error: #{tab}"
+ # end
+end
+
+path = ARGV.first
+contents = IO.read(path)
+
+beautifyRuby(contents)

0 comments on commit 67f0d1c

Please sign in to comment.