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

Is it possible to write a NetBeans code template for using all the arguments declared in the function header (for example, to call another function with these variables) ? . The number of arguments may be different, so this does not seem easy.

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


Here's a usage example (calling the dsm() function several times depending on the number of 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, in the first case there are 4 arguments, and only 1 in the second. The name of the arguments also changes depending on the given function.

Here is the code template that I already wrote to use the dsm() function:

ddsm code template

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

so I just type ddsm , ddsm Tab , and then I have to enter the exact name of the variable. Therefore, it will print the following:

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

After that, I can change the part of variables and enter another name, and the same will be used in the line. Example:

Using ddsm code template

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

+6
source share
2 answers

This is really hard. If you knew that you would use a macro when declaring a function, you can use the following templates:

 // 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} } 

A couple of templates give you a very quick declaration, and you must enter parameter names only once.

If you add these macros later, you may need to look at this document and implement the desired behavior (although this can be quite complicated).

Hope this helps!

+2
source

Why don't you just use get_defined_vars () to pass them in just one shot? Thus, your macro should be only one 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); 

Conclusion:

 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 
+1
source

Source: https://habr.com/ru/post/921810/


All Articles