Actionscript Getter/Setter Command for Textmate

I love TextMate.
Yay, today I've purchased the extraordinary TextMate and wrote my first command for it. A pain for a Ruby/Bash/AppleScript beginner like me, so it may be cumbersome and/or ugly, but it works. So let me show you what it does ...
1. Variable naming

Basically I give my private properties the suffix "__", so I can identify them easier inside the class body. Therefore I laid the regexp out this way, but feel free to change it to fit your needs.
2. Triggering the command

If you named your properties the right way (or edited the regexp) this popup window comes up, after triggering the command. You can now easily choose which variable you want to add getter/setters for. Just select and press OK and vóila ...
3. The pasted snippet

the getter/setters have been automatically been generated.
4. The setup
Save: nothing
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 %s( Void ) : %s
-
{
-
return __%s;
-
}
-
EOF
-
getter = getter % [ item[ "name" ], item[ "type" ], item[ "name" ] ]
-
-
setter = <<-EOF
-
public function set%s( val : %s ) : Void
-
{
-
__%s = val;
-
}
-
EOF
-
setter = setter % [ item[ "name" ].sub(/./){$&.upcase}, item[ "type" ], item[ "name" ] ]
-
-
-
snippet = getter + "\n" + setter
-
print snippet
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
6 Comments
Jump to comment form | comments rss [?] | trackback uri [?]