13 Oct 2009

TextMate command for generating ActionScript getter/setters

Based on this

I just changed the format of the code to match the existing code at work.

Command configuration:

Save: nothing

Input: Entire Document

Output: Insert as Snippet

Activation: Choose whatever you want, personally I prefer using “get” as a Tab Trigger

Scope Selector: source.actionscript

Command:

#!/usr/bin/ruby
txt = STDIN.read
vars = txt.scan(/^\W*(private|public){0,1}\W*var\W*\_(\w*)\W*\:\W*(\w*)/)

ascript = <<-EOF
tell app "TextMate" 
    activate
    choose from list { %s } with title "Variable auswaehlen" with prompt "Welche Variable als Basis benutzen?" 
end tell
EOF

displayList = []
varList = []
vars.each_with_index do |x,i|
    scope = x[0]
    name = x[1]
    type = x[2]
    urStr = ""
    if ( scope != nil )
        urStr += "(" + scope.capitalize() + ") "
    end
    urStr += "_" + name
    if ( type != nil )
        urStr += " : " + type
    end
    displayList.push( urStr )
    varList.push( { "name" => name, "type" => type } )
end

list = '"' + displayList.join( '", "' ) + '"'
ascript = ascript % list
params = "<< 'AS'\n #{ascript}\nAS"

cmd = open("|osascript" + params)
result = cmd.gets.chomp()
cmd.close

if ( result == "false" )
    exit
end

index = displayList.index( result )
item = varList[ index ]

getter = <<-EOF
public function get %s():%s{
    return this._%s;
}
EOF
getter = getter % [ item[ "name" ], item[ "type" ], item[ "name" ] ]

setter = <<-EOF
public function set %s(value:%s):void{
    this._%s = value;
}
EOF
setter = setter % [ item[ "name" ], item[ "type" ], item[ "name" ] ]

snippet = getter + "\n" + setter
print snippet