Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetBeans code template for using all the arguments declared in the function's header

Tags:

php

netbeans

Is it possible to write a NetBeans code template for using all the arguments declared in a function's header (e.g. for calling another function with these variables)? The number of the arguments can be different, so it doesn't seem to be easy.

For example, sometimes I want to print out all the arguments in a function for debugging purposes.


Here's an example usage (calling dsm() function multiple times depending on the number of the arguments):

function testModule_theme($existing, $type, $theme, $path) {
  dsm($existing, '$existing in ' . __FUNCTION__ . '()');
  dsm($type, '$type in ' . __FUNCTION__ . '()');
  dsm($theme, '$theme in ' . __FUNCTION__ . '()');
  dsm($path, '$path in ' . __FUNCTION__ . '()');

  return array(
    // ......
  );
}

Here's another one:

function testModule_block_view($delta = '') {
  dsm($delta, '$delta in ' . __FUNCTION__ . '()');
  $block = array();
  // .....
  return $block;
}

As you can see, there are 4 arguments in the first case, and only 1 in the second. The name of the arguments is also changing depending on the given function.

There's a code template I already wrote for using dsm() function:

ddsm code template

dsm($$${VARIABLE newVarName default="variables"}, '$$${VARIABLE} in '.__FUNCTION__.'()');

this way I just type ddsm, hit Tab, and then I have to type the exact name of the variable. So it would print out the following:

dsm($variables, '$variables in ' . __FUNCTION__ . '()');

After that, I can change the variables part, and type another name, and the same would be used in the string. An example:

Using ddsm code template

But I'm still too laggard to type that stuff :D, and I'm curious if there is a way to use all the arguments of a given function when using a code template in NetBeans.

like image 531
Sk8erPeter Avatar asked Jul 31 '12 23:07

Sk8erPeter


1 Answers

This really seems difficult. If you knew you will be using the macro when you declare the function, you could use templates like this:

// shortcut dsmfun1
function ${FUNCTION_NAME}($$${PAR1}) {
  dsm($$${PAR1}, '$$${PAR1} in ' . __FUNCTION__ . '()');

  ${selection}${cursor}
}

...

// shortcut dsmfun4
function ${FUNCTION_NAME}($$${PAR1}, $$${PAR2}, $$${PAR3}, $$${PAR4}) {
  dsm($$${PAR1}, '$$${PAR1} in ' . __FUNCTION__ . '()');
  dsm($$${PAR2}, '$$${PAR2} in ' . __FUNCTION__ . '()');
  dsm($$${PAR3}, '$$${PAR3} in ' . __FUNCTION__ . '()');
  dsm($$${PAR4}, '$$${PAR4} in ' . __FUNCTION__ . '()');

  ${selection}${cursor}
}

Couple templates give you really quick declaration and you have to type the parameters' names only once.

If you are adding these macros later, you might want to have a look at this doc and implement your desired behavior (even though that might be quite tricky).

Hope this helps!

like image 69
xixixao Avatar answered Oct 28 '22 19:10

xixixao