This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 153725 - Provide ability to assign return value in code completion
Summary: Provide ability to assign return value in code completion
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Editor (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Dusan Balek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-11-23 21:30 UTC by tomwheeler
Modified: 2011-09-27 11:40 UTC (History)
1 user (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description tomwheeler 2008-11-23 21:30:54 UTC
Consider that a java developers spends a great deal of time writing code like this:

   int x = myObject.getIntValue();

The code completion feature allows a developer to type "myObject.", choose getIntValue() from the list of methods on
that object and then hit enter to generate the right side of the statement (minus the semicolon).  After doing so, the
developer must then navigate back before that statement to type left side of the statement (the assignment).  

One of the IDE's nice features is that you can now hit the back arrow key a few times after the code completion and then
hit "Alt+Enter" to choose the "Assign return value to new variable" quick fix to have it generate the assignment portion
of the statement.  

What I am requesting here is that you make it possible to combine these two features.  In other words, instead of typing
"myObject." and then hitting Enter to select the code completion, I should be able to type "myObject." and then hit
"Alt+Enter" (or some key combination) to simultaneously select the method and do the assignment.  Therefore, by typing
"myObject.", pressing the down arrow to select the method, and then hitting Alt+Enter, a line similar to the entire
statement listed above should be generated.  This would be a great convenience and save a good deal of time throughout
the day.

Ideally, the end state would be that the cursor is placed after the new method call (i.e. after the close parenthesis in
the position where you could type a semicolon to terminate the statement).
Comment 1 Dusan Balek 2011-08-03 11:52:45 UTC
We could try to do something for NB 7.1.
Comment 2 Dusan Balek 2011-09-07 09:19:36 UTC
Fixed in jet-main.

http://hg.netbeans.org/jet-main/rev/2963de079c0f
Comment 3 Quality Engineering 2011-09-08 14:29:23 UTC
Integrated into 'main-golden'
Changeset: http://hg.netbeans.org/main-golden/rev/2963de079c0f
User: Dusan Balek <dbalek@netbeans.org>
Log: Issue #153725: Provide ability to assign return value in code completion - fixed.
Comment 4 Geertjan Wielenga 2011-09-12 08:31:34 UTC
Tried it, it's great, thanks!
Comment 5 tomwheeler 2011-09-13 13:27:10 UTC
Verified using a nightly build:

Product Version: NetBeans IDE Dev (Build 201109120600)
Java: 1.6.0_27; Java HotSpot(TM) 64-Bit Server VM 20.2-b06
System: Linux version 2.6.27-17-server running on amd64; UTF-8; en_US (nb)

Thanks for implementing this!
Comment 6 ceklock 2011-09-18 05:12:20 UTC
I would like to see that as the default behaviour when you press Enter. Why not?

When you press Enter in Code Completion Box one of these should happen:
- Generate the entire statement.
or at least
- Complete the code and add ';' at the end. 

I can't imagine a good reason not to do this by default when Enter is pressed. If you do not want to create the entire statement then you press '.', ';' or space.
Comment 7 ceklock 2011-09-18 21:50:14 UTC
I am just asking to change the "ALT ENTER" shortcut just to Enter. It's more intuitive.
Comment 8 Dusan Balek 2011-09-19 09:19:02 UTC
I'm not sure if the change in the default behavior would be well accepted by majority of our users. I think it's a subject for a wider discussion. BTW - do you know if any other IDE behaves this way?
Comment 9 matthies 2011-09-19 12:20:12 UTC
I'm against changing the default behavior. There are many situations where I don't intend to code an assignment statement, and it would be a pain to always have to think of having to type a different key. Also, when you want to write multiline code like

    foo.getDerivedBarObject(ssdfsd, gyhujtyu)
        .transmogrify(target);

it get's complicated because you'd want to type Enter instead of "." to generate the line break, which conflicts with the suggested semantics of Enter.
Comment 10 ceklock 2011-09-19 15:37:46 UTC
"What about argument guessing? If it's "foo.getCar(someArg, someOtherArg)", then would pressing the dot key open the second CC popup, or would it go into argument editing mode (to possibly correct the guessed arguments)?

I don't think this would work well. I wouldn't want to have to take care which key to press to select a completion." - by Niklas Matthies in NetCat mailing list.


Ok, I understand your point. Forget the "dot automatically opens completion popup".

I would be happy if Enter always define a new variable (when the method is not void), because the general design principle says that a given construct should do precisely one thing.

1) So lets suppose this:
foo.get|         >> Opens auto completion popup at |
>> Selected method is foo.getCar(someArg, someOtherArg), and you press Enter.
>> The "rename variable" dialog box appears and you give the name "car" to the variable. So you have:
Car car = foo.getCar(someArg, someOtherArg);
>> Then the cursor is placed at someArg so you can rename it. The cursor navigates to someOtherArg when you press Tab or Enter, and to the end of the line if you press Tab or Enter again.

2) Other case:
System.out.println(foo.get|);    >> Opens auto completion popup at |
>> Selected method is foo.getCar(someArg, someOtherArg), and you press Enter.
>> The "rename variable" dialog box appears and you give the name "car" to the variable. So you have:
Car car = foo.getCar(someArg, someOtherArg);
System.out.println(car);
>> Then the cursor is placed at someArg so you can rename it. The cursor navigates to someOtherArg when you press Tab or Enter, and to the end of the line if you press Tab or Enter again.

If you dont want this behavior press '.', ';' or space when a method is selected on the auto-completion popup window.

I think this will be better for everyone.
Comment 11 matthies 2011-09-19 16:01:19 UTC
Sorry, it won't be better for me. I suspect most users wouldn't want the IDE to lock them into that "general design principle".
Comment 12 ceklock 2011-09-19 16:07:56 UTC
"Also, when you want to write
multiline code like

    foo.getDerivedBarObject(ssdfsd, gyhujtyu)
        .transmogrify(target);

it get's complicated because you'd want to type Enter instead of "." to
generate the line break, which conflicts with the suggested semantics of
Enter."



Suppose you start with this:

foo.get|   >> Displays auto-completion popup at | (when the shortcut for
auto-completion is activated).

>> The method "getDerivedBarObject(ssdfsd, gyhujtyu)" is selected from the auto-completion popup window. You press Enter.

>> Then the "rename variable" dialog appears, you give the name "obj" to the variable. So you have this:

DerivedBarObject obj = foo.getDerivedBarObject(ssdfsd, gyhujtyu);

>> The cursor is placed at "ssdfsd" so you can rename it. If you press Enter or Tab the cursor navigates to "gyhujtyu". If you press Enter or Tab again the cursor goes to the end of the line.

>> Ok, now the cursor is at the end of the line. You press Enter (you are not in any popup window now) so you go to the next line. Then you type this:

obj.|   >> and call the auto-completion popup window

>> Select the "transmogrify(target)" from the popup window and press Enter. So you will have:

DerivedBarObject obj = foo.getDerivedBarObject(ssdfsd, gyhujtyu);
obj.transmogrify(target);

>> If the "transmogrify(target)" method is void then the cursor will be placed at "target" so you can rename it. If you press Enter or Tab again the cursor goes to the end of the line.
Comment 13 Jan Lahoda 2011-09-19 16:16:33 UTC
Overall, I agree with dbalek and matthies that this change of default behavior
is unlikely to be accepted by the users, and I consider it undesirable (sorry).

(In reply to comment #10)
> "What about argument guessing? If it's "foo.getCar(someArg, someOtherArg)",
> then would pressing the dot key open the second CC popup, or would it go into
> argument editing mode (to possibly correct the guessed arguments)?
> 
> I don't think this would work well. I wouldn't want to have to take care which
> key to press to select a completion." - by Niklas Matthies in NetCat mailing
> list.
> 
> 
> Ok, I understand your point. Forget the "dot automatically opens completion
> popup".
> 
> I would be happy if Enter always define a new variable (when the method is not
> void), because the general design principle says that a given construct should
> do precisely one thing.
> 
> 1) So lets suppose this:
> foo.get|         >> Opens auto completion popup at |
> >> Selected method is foo.getCar(someArg, someOtherArg), and you press Enter.
> >> The "rename variable" dialog box appears and you give the name "car" to the variable. So you have:

I definitely disagree with code completion to show any dialog after pressing
Enter on confirming a method call. That is sure to break user's workflow.

> Car car = foo.getCar(someArg, someOtherArg);
> >> Then the cursor is placed at someArg so you can rename it. The cursor navigates to someOtherArg when you press Tab or Enter, and to the end of the line if you press Tab or Enter again.
> 
> 2) Other case:
> System.out.println(foo.get|);    >> Opens auto completion popup at |
> >> Selected method is foo.getCar(someArg, someOtherArg), and you press Enter.
> >> The "rename variable" dialog box appears and you give the name "car" to the variable. So you have:
> Car car = foo.getCar(someArg, someOtherArg);
> System.out.println(car);
> >> Then the cursor is placed at someArg so you can rename it. The cursor navigates to someOtherArg when you press Tab or Enter, and to the end of the line if you press Tab or Enter again.

Sorry, but I have to disagree completely here: such a code rewrite in this
context is simply too magical and users are not going to understand and accept
this behavior, especially if it is the default behavior.

> 
> If you dont want this behavior press '.', ';' or space when a method is
> selected on the auto-completion popup window.

Along the matthies lines: currently, when I press Enter, I always get a
sensible result. I *may* think before pressing Enter and press Alt-Enter, '.'
or ';' to get a result better suited for my current situation. But if I press
Enter, I always get something meaningful that I can extend. With your proposal
I would *have to* think before pressing anything and choose the correct
shortcut. That is a huge paradigm shift, IMO.

> 
> I think this will be better for everyone.

Sorry, but I have to disagree again: I am convinced that the proposed change of
default would produce piles of complaints and would deter users from using
NetBeans.
Comment 14 ceklock 2011-09-19 16:46:35 UTC
"Sorry, it won't be better for me. I suspect most users wouldn't want the IDE to
lock them into that 'general design principle'."


The IDE will not lock the users into that principle. If you dont want that behavior you press space or dot or semicolon for example.

Furthermore, Enter should insert new variable only in the first level method.
Comment 15 ceklock 2011-09-19 16:51:06 UTC
Many users don't even know that "Alt Enter" shortcut exists. But ok, you decide.

I think Alt Enter will be a forgotten feature.
Comment 16 ceklock 2011-09-27 11:40:01 UTC
Sorry for my last comment above.

I am sure Alt Enter to assign return value in code completion is really a great feature, no matter if is "Alt Enter" or just "Enter". At the end you are right.

Thanks for that :)