TextMate Command

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

variable namingscheme

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

properties popup

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

getter/setter snippet

the getter/setters have been automatically been generated.

4. The setup

Save: nothing
Command:

  1. #!/usr/bin/ruby
  2. txt = STDIN.read
  3. vars = txt.scan(/^\W*(private|public){0,1}\W*var\W*\_\_(\w*)\W*\:\W*(\w*)/)
  4.  
  5. ascript = <<-EOF
  6. tell app "TextMate"
  7.     activate
  8.     choose from list { %s } with title "Variable auswaehlen" with prompt "Welche Variable als Basis benutzen?"
  9. end tell
  10. EOF
  11.  
  12. displayList = []
  13. varList = []
  14. vars.each_with_index do |x,i|
  15.     scope = x[0]
  16.     name = x[1]
  17.     type = x[2]
  18.     urStr = ""
  19.     if ( scope != nil )
  20.         urStr += "(" + scope.capitalize() + ") "
  21.     end
  22.     urStr += "__" + name
  23.     if ( type != nil )
  24.         urStr += " : " + type
  25.     end
  26.     displayList.push( urStr )
  27.     varList.push( { "name" => name, "type" => type } )
  28. end
  29.  
  30.  
  31. list = '"' + displayList.join( '", "' ) + '"'
  32. ascript = ascript % list
  33. params = "<<'AS'\n #{ascript}\nAS"
  34.  
  35. cmd = open("|osascript" + params)
  36. result = cmd.gets.chomp()
  37. cmd.close
  38.  
  39. if ( result == "false" )
  40.     exit
  41. end
  42.  
  43. index = displayList.index( result )
  44. item = varList[ index ]
  45.  
  46. getter = <<-EOF
  47. public function %s( Void ) : %s
  48. {
  49.     return __%s;
  50. }
  51. EOF
  52. getter = getter % [ item[ "name" ], item[ "type" ], item[ "name" ] ]
  53.  
  54. setter = <<-EOF
  55. public function set%s( val : %s ) : Void
  56. {
  57.     __%s = val;
  58. }
  59. EOF
  60. setter = setter % [ item[ "name" ].sub(/./){$&.upcase}, item[ "type" ], item[ "name" ] ]
  61.  
  62.  
  63. snippet = getter + "\n" + setter
  64. 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