Smart Method Parameters Pre-Filling

Hi all, today we would like to introduce you a new feature which will be, we hope, very helpful for you! It makes pre-filling of method/function parameters really smart.

Let's start with an old behaviour. If you had a method with parameters described in PHPDoc, code completion of this function didn't care about that. It suggested you just the names of it's parameters, even though you had your own variables of a valid type declared right before code completion invocation.

It was really annoying. But now, code completion suggests you your valid variables (of desired type), so you don't have to rewrite method calls everytime you use code completion. Isn't that great? I think it is! So now that suggestion looks like this:

And the last thing. If you have your variables named exactly as parameters of suggested function and they are of desired types, you don't have to care about positions of these variables in invocation scope. They are used always, even though some other variables of desired types exist closer your completion invocation.

And, of course, it works for PHP built-in functions as well.

So that's all for today and as usual, please test it and if you find something strange, don't hesitate to file a new issue (product php, component Editor). Thanks.

Comments:

Great enhancement :)

Posted by guest on August 31, 2011 at 11:32 AM CEST #

Wow that looks really cool! Can't wait to try it out and see if it really saves you work!

Posted by Koen on August 31, 2011 at 03:15 PM CEST #

Yet another thing that vim can't do! Thanks!

Posted by Michael Butler on September 01, 2011 at 02:37 PM CEST #

I'm not sure that displaying the type in place of the variable name is an improvement.

I normally give my variables meaningful names so will likely find it frustrating when I see "function($int, $int, $int)". I'll have to look up what each parameter is before I can use it. Which makes auto-complete less useful.

Posted by guest on September 04, 2011 at 07:20 PM CEST #

I'm not sure that displaying the type in place of the variable name is an improvement.

I normally give my variables meaningful names so will likely find it frustrating when I see "function($int, $int, $int)". I'll have to look up what each parameter is before I can use it. Which makes auto-complete less useful.

Posted by guest on September 04, 2011 at 08:05 PM CEST #

Sounds like a great timesaver. Good work, thanks!

Posted by Pieter on September 06, 2011 at 06:14 PM CEST #

RE: Posted by guest on September 05, 2011 at 01:05 AM CEST

Don't worry, it can't happen, because every defined variable is used only once. So if you have a function with 3 int parameters and you have an $int variable defined before method invocation, you will just see "function($int, <original>, <original>).

Posted by Ondrej Brejla on September 07, 2011 at 12:34 PM CEST #

Very Good

Posted by guest on September 11, 2011 at 09:39 PM CEST #

Something that concerns me very greatly about this feature is how much information it will be removing from context. For example, let's say I have this scenario:

/**
* @param string $status
* @param integer $reportId
* @param integer $userId
*/
protected function _setReportUser($status, $reportId, $userId) {}

/* Then this code somewhere else */
$unrelatedString = 'blah blah blah';
$activeReportId = 1;
$activeUserId = 2;
$this->_setReportUser(/* suggestion will be ... */);
$this->_someOtherAction($unrelatedString, $activeReportId); /* This is what I would be doing with that string */

Will it suggest ($unrelatedString, $activeReportId, $activeUserId) and not show the original variable names? That original variable name of $status is very critical, as an agreement between the function writer and the function caller, of what internal type of data can be expected.

Or, maybe it will show ($unrelatedString, $activeUserId, $userId)? Again, that's confusing and very misleading.

This feature sounds like Netbeans trying to be too smart for its own good, and getting in the way of the programmer and the written definitions. At the very least I hope we'll have the option to completely turn off this behavior.

Posted by Zimzat on September 17, 2011 at 01:32 PM CEST #

Hi Zimzat,

firstly, thanks for your feedback.
And answers for your questions:

1) line with $this->_setReportUser will suggest "$unrelatedString, $activeUserId, $activeReportId"
2) line with $this->_someOtherAction will suggest "$unrelatedString, $activeUserId"

And if it's not comfortable for you and you would like to have a "classic" behaviour, you can switch this feature off in "Tools->Options->Editor->Code Completion->PHP->Use Smart Parameters Pre-Filling" (this option is new, will be in PHP daily build in one hour I hope)

Posted by Ondrej Brejla on September 19, 2011 at 09:08 AM CEST #

This sounds interesting, but as Zimzat pointed out, it could be misleading in some cases. I guess it will have to survive some live testing, and to determine hit-to-miss ratio, to see whether is it actually time-saving or error-inducing.

Posted by rosswald on September 23, 2011 at 01:32 PM CEST #

As I wrote in the last comment, now there is a possibility to disable that feature, so if you will not be satisfied with that, you can switch it off ;)

Posted by Ondrej Brejla on September 23, 2011 at 01:34 PM CEST #

Yup, making everything configurable is one of the reasons I use Netbeans :)

Posted by rosswald on September 23, 2011 at 01:42 PM CEST #

Please disable this feature by defaut. IMHO, it is a dangerous feature. Sometimes, it can help a little bit as it was planned. But some other times, it will induce errors! See this code:

$userId = 0;
$teacherId = 0;
$course = getCourse(

With this feature, NB will display "$userId" as first parameter. So you won't know what was really declared, and it can lead to errors you would never have made with the old behaviour. And it's even more dangerous for first-time users that don't know about this "smart renaming".

Please don't mangle with the function parameters' names, they are meaningful. Never wondered in a PHP function about the order of $needle and $haystack?

Posted by guest on September 25, 2011 at 10:20 AM CEST #

Seems like a very random and not well-thought-out feature. Very hack-ish. Should most definitely NOT be turned on as a default. The code completion should show the parameter type and name -- inferring which variables I want to use from the current scope is just silly and in some cases inviting you to make mistakes that you wouldn't otherwise make.

Posted by mjh_ca on October 24, 2011 at 11:58 PM CEST #

Yes, I rewrote it and now you see original params and their types in CC...and variables are prefilled "after" you apply that CC. So now it's the same like in Java editor.

Posted by Ondrej Brejla on October 25, 2011 at 04:53 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

This blogs is written by NetBeans developers who contribute to the PHP support mainly.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today