[TxMt] PHP getter/setter generator

Steve Lianoglou lists at arachnedesign.net
Mon Aug 6 02:02:36 UTC 2007


Hey all,

I've been doing some php code lately and got a bit tired of writing  
getters/setters. I didn't find a command to generate them for me, so  
I whipped one up.

I'm putting it here in case someone finds it useful.

The idea is to highlight the lines in your class file where you  
define the class's instance vars. You then just have to fire this  
command and it'll create generic getters/setters for the vars found  
in the selection (can do multiple per line). If no lines are select,  
it will try to create getters/setters for the vars in the current line.

It might be handier if it dumped the code into the clipboard, or  
something, but it will just drop the code right below the current  
selection.

If that sounds handy, you can create a command like so:

==================
Save: NOTHING
Input: SELECTED TEXT or LINE
Output: INSERT AS TEXT
Scope Selector: source.php


Command(s):
-----------------
#!/usr/bin/env python
import os,re,sys

template = """
public function get:CALL:() {
\treturn $this->:VAR:;
}

public function set:CALL:($:VAR:) {
\t$this->:VAR: = $:VAR:;
}
"""

regex = re.compile(r'\$(\w+)')

try:
     text = os.environ['TM_SELECTED_TEXT']
except KeyError:
     text = os.environ['TM_CURRENT_LINE']

for line in text.split('\n'):
     for var in regex.findall(line):
         code = template.replace(':VAR:', var)
         code = code.replace(':CALL:', var[0].upper() + var[1:])
         sys.stdout.write(code)
=====================

Bind to your favorite key combo and voila.

-steve




More information about the textmate mailing list