6

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.

Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67

2 Answers2

2

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!

xixixao
  • 481
  • 6
  • 16
  • +1, thanks, that's a very good idea, but that would mean I have to create as many code templates for these functions as many arguments I would need in these functions. But I'll think about it! – Sk8erPeter Aug 21 '12 at 13:38
1

Why don't you just use get_defined_vars() to pass them all in one shot? This way, your macro only needs to be a single static line.

function dsm($func, array $args)
{
    foreach ($args as $name => $value) {
        echo "in $func, arg '$name' is $value\n";
    }
}

function testModule_theme($existing, $type, $theme, $path) {
    dsm(__FUNCTION__, get_defined_vars());
}

testModule_theme(1, 2, 3, 4);

Output:

in testModule_theme, arg 'existing' is 1
in testModule_theme, arg 'type' is 2
in testModule_theme, arg 'theme' is 3
in testModule_theme, arg 'path' is 4
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • +1, this is a very good idea, but I don't always want to print out all the variables, in many cases, I want to comment out one or two of them, that's why I wanted to "reserve" separate lines for these debugging stuffs. (By the way, [dsm()](http://api.drupal.org/api/devel/devel.module/function/dsm/7) is a function in Drupal's Devel module.) – Sk8erPeter Aug 21 '12 at 13:35