[TxMt] PHP getter/setter generator

Jacques Chester jacques at chester.id.au
Tue Aug 7 11:24:25 UTC 2007


I did the same thing, though I think I used ruby. It should be
in the list archive somewhere.

Cheers,

JC.

On 06/08/2007, at 10:02 AM, Steve Lianoglou wrote:

> 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
>
>
> ______________________________________________________________________
> For new threads USE THIS: textmate at lists.macromates.com
> (threading gets destroyed and the universe will collapse if you don't)
> http://lists.macromates.com/mailman/listinfo/textmate

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2425 bytes
Desc: not available
URL: <http://lists.macromates.com/textmate/attachments/20070807/4781bec7/attachment.p7s>


More information about the textmate mailing list