Reply to comment

Beautiful Ruby in TextMate

เนื่องจาก TextMate ไม่ได้มีการจัด code ของ ruby ให้อ่านได้ง่ายๆ ดังนั้น Paul Lutus จึงได้ทำ Ruby code beautifier in Ruby โดยเพิ่มมันเข้าไปใน TextMate

วิธีการเพิ่มเข้าไปใน TextMate

1. ไปที่เมนู Bundles –> Bundle Editor –> Show Bundle Editor

TextMate01

2. เลื่อน scroll bar มาจนเห็น Ruby แล้วเปิดมันออก

TextMate02

3. กดเครื่องหมาย + ด้านล่าง แล้วเลือก New Command ซึ่งคุณสามารถตั้งชื่ออะไรก็ได้ ในที่นี้ใช้ BeautifulRuby เมื่อตั้งชื่อเสร็จแล้วมาดูส่วนของ Activation เลือก Key Equivalent จากนั้นเลือกช่องว่างด้านข้างเพื่อตั้งค่าทางลัด โดยในที่นี้ใช้ Command+Shift+B เมื่อกำหนดเสร็จแล้ว มาดูส่วนของ Scope Selector ให้กำหนดค่าเป็น source.ruby

TextMate03

4. นำ code ด้านล่างนี้ไปใส่ในช่อง Command(s)

#!/usr/bin/env ruby
 
# Ruby beautifier, version 1.3, 04/03/2006
# Copyright (c) 2006, P. Lutus
# TextMate modifications by T. Burks
# Released under the GPL
 
$tabSize = 3
$tabStr = " "
 
# indent regexp tests
 
$indentExp = [
   /^module\b/,
   /(=\s*|^)if\b/,
   /(=\s*|^)until\b/,
   /(=\s*|^)for\b/,
   /(=\s*|^)unless\b/,
   /(=\s*|^)while\b/,
   /(=\s*|^)begin\b/,
   /(=\s*|^)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
   return line + "\n"
end
 
def beautifyRuby
   commentBlock = false
   multiLineArray = Array.new
   multiLineStr = ""
   tab = 0
   source = STDIN.read
   dest = ""
   source.split("\n").each do |line|
      # 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
      if(commentBlock)
         # add the line unchanged
         dest += line + "\n"
      else
         commentLine = (tline =~ /^#/)
         if(!commentLine)
            # throw out sequences that will
               # only sow confusion
               tline.gsub!(/\/.*?\//,"")
               tline.gsub!(/%r\{.*?\}/,"")
               tline.gsub!(/%r(.).*?\1/,"")
               tline.gsub!(/\\\"/,"'")
               tline.gsub!(/".*?"/,"\"\"")
               tline.gsub!(/'.*?'/,"''")
               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 "Indentation error: #{tab}"
   #end
end
 
beautifyRuby

ที่มา: neontology

No votes yet

Reply

The content of this field is kept private and will not be shown publicly.