Bug 198508 - [content assist] Code completion enhancement - semicolon after calls to void methods
Summary: [content assist] Code completion enhancement - semicolon after calls to void ...
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 3.3   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 4.3 M7   Edit
Assignee: Martin Mathew CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 24613 (view as bug list)
Depends on: 198748
Blocks:
  Show dependency tree
 
Reported: 2007-08-01 05:48 EDT by Mark Sederqvist CLA
Modified: 2013-04-15 11:50 EDT (History)
5 users (show)

See Also:
daniel_megert: review+


Attachments
Fix. (5.06 KB, patch)
2013-03-25 11:09 EDT, Martin Mathew CLA
daniel_megert: review-
Details | Diff
Fix. (6.28 KB, patch)
2013-03-27 06:36 EDT, Martin Mathew CLA
daniel_megert: review-
Details | Diff
Fixed failing tests. (12.44 KB, patch)
2013-03-27 06:39 EDT, Martin Mathew CLA
daniel_megert: review+
Details | Diff
Patch. (4.33 KB, patch)
2013-04-12 06:21 EDT, Martin Mathew CLA
daniel_megert: review+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Mark Sederqvist CLA 2007-08-01 05:48:02 EDT
Why doesn't the code editor code completion automatically put a semicolon upon inserting a call to a void method?

That would really save some time, since the cursor is always placed in the parameter list (between the method call brackets), and for void methods you always have to move to the end of the line and put a semicolon.

I can't off the top of my head think of a situation where you do not need to manually enter a semicolon upon a call to a void method.
Comment 1 Dani Megert CLA 2007-08-01 09:11:22 EDT
Note that you can press ; instead of Enter to insert the proposal and add a ';'
Comment 2 Mark Sederqvist CLA 2007-08-01 10:36:45 EDT
I was unaware of that, but I would still expect semicolon to be inserted by default after a void method call, if there are no cases where it is not needed.
Comment 3 Alex Blewitt CLA 2007-08-01 11:01:39 EDT
Using ; for me didn't work to select a proposal. It just gave me a ';' in the way. I did get it to accidentally work once or twice, but more often than not it just failed.
Comment 4 Dani Megert CLA 2007-08-02 04:17:00 EDT
>Using ; for me didn't work to select a proposal.
It works for me:
	HashMap m;
	m.e<code assist>
        press ';'
        ==> m.entrySet();

Please file a bug report for the case(s) that don't work
Comment 5 Alex Blewitt CLA 2007-08-02 18:45:14 EDT
Raised bug 198748 as per comment 4
Comment 6 Martin Mathew CLA 2013-03-15 02:41:22 EDT
*** Bug 24613 has been marked as a duplicate of this bug. ***
Comment 7 Martin Mathew CLA 2013-03-15 02:46:33 EDT
Providing a semicolon after calls to void methods as part of the content assist looks like a genuine request because:
1. Could not think of any scenario where we do further processing from a void method.
2. A void method never appears as part of the method parameter.
3. We will not add semicolon through content assist for a constructor invocation though as per the signature the return type is "void".
4. For the bug 333428 half of the problems will be solved if a void method completes with a semicolon :-)

If there is any valid case where adding a semicolon as part of content assist for a void method would cause inconvenience let us discuss.
Comment 8 Martin Mathew CLA 2013-03-25 11:09:47 EDT
Created attachment 228996 [details]
Fix.

With this patch, the Java editor code completion automatically put a semicolon upon inserting a call to a void method. Note that a constructor invocation will not add a semicolon.

Dani, kindly let me know if the approach is right.
Comment 9 Dani Megert CLA 2013-03-25 12:01:02 EDT
Comment on attachment 228996 [details]
Fix.

(In reply to comment #8)
> Created attachment 228996 [details] [diff]
> Fix.
> 
> With this patch, the Java editor code completion automatically put a
> semicolon upon inserting a call to a void method. Note that a constructor
> invocation will not add a semicolon.
> 
> Dani, kindly let me know if the approach is right.

The approach is fine, but several tests now fail.


Additional comments:
- Why is protected static final String SEMICOLON= ";"; //$NON-NLS-1$
  in AbstractJavaCompletionProposal and not in LazyJavaCompletionProposal where
  the other char constants are?

- hasReturnType(...) can be written as one-liner:
  !Signature.SIG_VOID.equals(method.getReturnType())

- I would replace hasReturnType(...) with canAppendSemicolon(...), so that we 
  have less code duplication.
Comment 10 Dani Megert CLA 2013-03-26 06:25:35 EDT
Some reminder: we need to make sure that when the user also types the ';', we never end up with two semicolons. It should behave the same as when we type '(' and then later ')', e.g.
type "foo(" ==> inserts foo()
type "some stuff" ==> foo(some stuff)
press ')' ==> foo(some stuff)<caret>
Comment 11 Martin Mathew CLA 2013-03-27 06:36:44 EDT
Created attachment 229083 [details]
Fix.

Thanks for the comments Dani.

The last char in the replacement string is considered as the exit character. So the exit character will be ')'for non-void methods and ';' for void methods.
Typing the exit character when inside the link mode of the method completion will result in exiting from the link mode.

In the current implementation below is the behavior

void foo(String s, int i)
1. foo|<ctrl+space> => inserts foo(|s|, i);
2. type "string" => foo(|"string"|, i);
3. type , => foo("string", |i|);
4. type 10 => foo("string", |10|);
5. type ) or ; at this point => foo("string", 10);|
type ; in any of the previous steps will result in exit from the link mode.

Dani, at step 5, is the behavior correct when user type ). Should the cursor be positioned before ; ?
Comment 12 Martin Mathew CLA 2013-03-27 06:39:17 EDT
Created attachment 229084 [details]
Fixed failing tests.

Some of the tests in org.eclipse.jdt.text.tests were failing due to this fix. I have fixed them.
Comment 13 Martin Mathew CLA 2013-03-27 09:46:43 EDT
When Eclipse Preferences -> Java -> Editor -> Typing : option "Automatically insert at correct position" for Semicolons is selected, and ';' is used to trigger method selection from the content assist pop-up, it results in 2 ';' to be added at the end of the method. This will be taken care while fixing bug 198748.
Comment 14 Dani Megert CLA 2013-03-28 08:38:13 EDT
(In reply to comment #11)
> Created attachment 229083 [details] [diff]
> Fix.
> 

The fix does not take comment 10 into account:

Test Case 1:
1. paste this into Package Explorer:
	public void bug() {
		b
	}
2. after "b", press Ctrl+Space
3. press Enter
4. press ';'
==> two semicolons

It should behave like if you type "foo(" and then ')', which also does not add a second ')'.



Test Case 2:
1. enable to automatically insert semicolon at correct position
2. paste this into Package Explorer:
	public void bug(int i) {
		b
	}
3. after "b", press Ctrl+Space
4. press ';'
==> two semicolons



> In the current implementation below is the behavior
> 
> void foo(String s, int i)
> 1. foo|<ctrl+space> => inserts foo(|s|, i);
> 2. type "string" => foo(|"string"|, i);
> 3. type , => foo("string", |i|);
> 4. type 10 => foo("string", |10|);
> 5. type ) or ; at this point => foo("string", 10);|
> type ; in any of the previous steps will result in exit from the link mode.
> 
> Dani, at step 5, is the behavior correct when user type ). Should the cursor
> be positioned before ; ?

The caret should be after the semicolon.
Comment 15 Martin Mathew CLA 2013-04-01 02:25:06 EDT
(In reply to comment #14)
> The fix does not take comment 10 into account:
> 
> Test Case 1:
> 1. paste this into Package Explorer:
> 	public void bug() {
> 		b
> 	}
> 2. after "b", press Ctrl+Space
> 3. press Enter
> 4. press ';'
> ==> two semicolons
> 
> It should behave like if you type "foo(" and then ')', which also does not
> add a second ')'.
Link mode is enabled only for methods with parameter. In the above case, since the option to insert matching closing braces is enabled, the closing braces is inserted when user type the opening braces. Note the cursor position after user type '(' <bug(|)>. When dealing with a void method with parameter <void bug(int i)>, on content assist when the user selects the method and then type Enter, the link mode is active <bug(|i|);> and on typing semicolon or ')' no redundant character will be added and it result in exit from link mode.
However when user selects the void method with no parameters by typing the Enter key, semicolon is inserted at the end of the method and is not in link mode anymore <bug();|>. If we notice the cursor is positioned after the semicolon. Should this behavior be changed? If so after user type the Enter key, where should the cursor be positioned?
> 
> Test Case 2:
> 1. enable to automatically insert semicolon at correct position
> 2. paste this into Package Explorer:
> 	public void bug(int i) {
> 		b
> 	}
> 3. after "b", press Ctrl+Space
> 4. press ';'
> ==> two semicolons
As mentioned in comment #13 this will be taken care while fixing bug 198748 as selecting the method on ; trigger is handled there.

> > In the current implementation below is the behavior
> > 
> > void foo(String s, int i)
> > 1. foo|<ctrl+space> => inserts foo(|s|, i);
> > 2. type "string" => foo(|"string"|, i);
> > 3. type , => foo("string", |i|);
> > 4. type 10 => foo("string", |10|);
> > 5. type ) or ; at this point => foo("string", 10);|
> > type ; in any of the previous steps will result in exit from the link mode.
> > 
> > Dani, at step 5, is the behavior correct when user type ). Should the cursor
> > be positioned before ; ?
> 
> The caret should be after the semicolon.
The current behavior is right then. At step 5, when user type ')' the caret is positioned after the semicolon.
Comment 16 Dani Megert CLA 2013-04-03 04:38:25 EDT
(In reply to comment #15)
> (In reply to comment #14)
> > The fix does not take comment 10 into account:
> > 
> > Test Case 1:
> > 1. paste this into Package Explorer:
> > 	public void bug() {
> > 		b
> > 	}
> > 2. after "b", press Ctrl+Space
> > 3. press Enter
> > 4. press ';'
> > ==> two semicolons
> > 
> > It should behave like if you type "foo(" and then ')', which also does not
> > add a second ')'.
> Link mode is enabled only for methods with parameter. In the above case,
> since the option to insert matching closing braces is enabled, the closing
> braces is inserted when user type the opening braces. Note the cursor
> position after user type '(' <bug(|)>. When dealing with a void method with
> parameter <void bug(int i)>, on content assist when the user selects the
> method and then type Enter, the link mode is active <bug(|i|);> and on
> typing semicolon or ')' no redundant character will be added and it result
> in exit from link mode.
> However when user selects the void method with no parameters by typing the
> Enter key, semicolon is inserted at the end of the method and is not in link
> mode anymore <bug();|>. If we notice the cursor is positioned after the
> semicolon. Should this behavior be changed? If so after user type the Enter
> key, where should the cursor be positioned?

After the single ';'


> As mentioned in comment #13 this will be taken care while fixing bug 198748 as > selecting the method on ; trigger is handled there.

OK, so, we'll first have to close that bug. We can't commit code that adds duplicate semicolons.
Comment 17 Dani Megert CLA 2013-04-03 05:43:50 EDT
(In reply to comment #16)
> > As mentioned in comment #13 this will be taken care while fixing bug 198748 
> > as selecting the method on ; trigger is handled there.
> 
> OK, so, we'll first have to close that bug. We can't commit code that adds
> duplicate semicolons.

I cannot verify this, since, after applying the patch from bug 198748, this patch here won't apply.
Comment 18 Martin Mathew CLA 2013-04-03 05:47:52 EDT
(In reply to comment #17)
> I cannot verify this, since, after applying the patch from bug 198748, this
> patch here won't apply.
Same files were modified for these 2 bugs, hence there could be some mix up. Let's first finalize and close bug 198748. Then i will upload a fresh patch for this bug.
Comment 19 Dani Megert CLA 2013-04-03 05:48:41 EDT
(In reply to comment #18)
> (In reply to comment #17)
> > I cannot verify this, since, after applying the patch from bug 198748, this
> > patch here won't apply.
> Same files were modified for these 2 bugs, hence there could be some mix up.
> Let's first finalize and close bug 198748. Then i will upload a fresh patch
> for this bug.

Yes, that's fine.
Comment 20 Martin Mathew CLA 2013-04-12 06:21:04 EDT
Created attachment 229668 [details]
Patch.

Appends semicolon when a void method is inserted via content assist.
As per comment #14, took care not to add duplicate semicolon when the user types semicolon after inserting the method.
Dani, kindly review.
Comment 21 Dani Megert CLA 2013-04-15 11:49:47 EDT
Thanks Manju, the patch is fine. I only made some minor tweaks to the Javadoc, the new method name and the code in apply(...). Please take a look.

Fixed with http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=9e44ebc0447ede8a22ec472659a5fa68ca2c3424