Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 466252

Summary: [templates] new 'finally' template does not appear when there's no catch block
Product: [Eclipse Project] JDT Reporter: Dani Megert <daniel_megert>
Component: CoreAssignee: Jay Arthanareeswaran <jarthana>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: daniel_megert, jarthana, noopur_gupta, robert.roth.off
Version: 4.5   
Target Milestone: 4.6 M7   
Hardware: All   
OS: All   
See Also: https://git.eclipse.org/r/71128
https://git.eclipse.org/r/71141
https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=3f07cee03bf21eb29e60cb096a0f462d3c1d5779
https://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=a4a8119e97de6e0227fed420006f636c311530e2
Whiteboard:
Bug Depends on: 184222    
Bug Blocks:    

Description Dani Megert CLA 2015-05-04 02:44:59 EDT
I20150503-2000.

The new 'finally' template introduced via bug 184222 does not work when there's no catch block:

	void foo() {
		try {
		} f<Ctrl+Space>
	}
Comment 1 Dani Megert CLA 2015-05-12 04:54:38 EDT
Robert, will you have time to fix this today or tomorrow?
Comment 2 Robert Roth CLA 2015-05-12 05:22:23 EDT
(In reply to Dani Megert from comment #1)
> Robert, will you have time to fix this today or tomorrow?

Yes, I will take a look today (tonight), and comment with the results, be it a gerrit request or an "I'm stuck with ..." type answer.
Comment 3 Robert Roth CLA 2015-05-12 05:34:53 EDT
(In reply to Dani Megert from comment #0)
> I20150503-2000.
> 
> The new 'finally' template introduced via bug 184222 does not work when
> there's no catch block:
> 
> 	void foo() {
> 		try {
> 		} f<Ctrl+Space>
> 	}

By the way, the same stands for the catch block too for me, the catch block template is not shown in the proposals, autocompletion inserts the catch keyword only:

    void foo() {
        try {
        } c<Ctrl+Space>
    }

Based on this, I feel that this is not strictly related to the new templates, but rather to the part selecting the templates to show in the given context, but will take a more in-depth look.
Comment 4 Noopur Gupta CLA 2015-05-12 06:09:25 EDT
It probably depends on the compile error at the place of invocation. I do not get other templates too at this location and some other locations with compile error.
Comment 5 Noopur Gupta CLA 2015-05-12 06:21:09 EDT
Please check if it is related to bug 432539.
Comment 6 Robert Roth CLA 2015-05-13 01:49:08 EDT
Dani, I took a look, but I'm not that familiar with JDT, so I couldn't really find the root cause, therefore I won't be able to fix it today. 

I wanted to check if it is indeed related to bug 432539, but I couldn't find another Java construct which would require two blocks (try...catch/finally), to see if a similar thing happens in case of a compile error (no templates in autocompletion). If it is indeed related to that bug, I guess this can't be fixed in time for 4.5, as the other bug is targeted for 4.6.
Comment 7 Dani Megert CLA 2015-05-13 03:56:12 EDT
Noopur, please take a look.
Comment 8 Noopur Gupta CLA 2015-05-13 05:56:10 EDT
When there is no catch block, CompletionContext#getTokenLocation() returns TOKEN_KIND_UNKNOWN at TemplateCompletionProposalComputer.computeCompletionEngine(JavaContentAssistInvocationContext context): line 71. 

When there is a catch block, it returns TL_STATEMENT_START, which is correct.

Hence, in absence of catch block, the context is set as "java" instead of "java-statements" (the context of the template). As a result, we don't get the template proposal.

We should not change the context of the template to "java" as it will result in template appearing at non-relevant locations too.

Moving to JDT/Core for comments on CompletionContext#getTokenLocation().
Comment 9 Jay Arthanareeswaran CLA 2015-05-13 11:45:13 EDT
Quick look shows that we have this code in CompletionEngine:

if (astNodeParent == null && astNode instanceof CompletionOnSingleNameReference &&						!((CompletionOnSingleNameReference)astNode).isPrecededByModifiers) {
  context.setTokenLocation(CompletionContext.TL_STATEMENT_START);
}

In this particular case (where we don't have the catch block, the completion node is of type CompleteOnKeyword and we don't set token location to TL_STATEMENT_START.

By altering the check to allow CompleteOnKeyword, I can see the finally template being offered. But things still don't work inside a lambda body, which could be lot more tricky.

At this point, I don't know if there are side effects. I am not an expert in this area, so will have to rely on tests.
Comment 10 Dani Megert CLA 2015-05-13 11:46:52 EDT
(In reply to Jay Arthanareeswaran from comment #9)
> At this point, I don't know if there are side effects. I am not an expert in
> this area, so will have to rely on tests.

This sounds like a candidate to move to 4.6 then.
Comment 11 Jay Arthanareeswaran CLA 2015-05-13 12:03:05 EDT
(In reply to Dani Megert from comment #10)
> This sounds like a candidate to move to 4.6 then.

Yes, if it can wait. We can consider in 4.5.1 too if you like.
Comment 12 Eclipse Genie CLA 2016-04-21 06:43:50 EDT
New Gerrit change created: https://git.eclipse.org/r/71128
Comment 13 Jay Arthanareeswaran CLA 2016-04-21 06:49:04 EDT
(In reply to Eclipse Genie from comment #12)
> New Gerrit change created: https://git.eclipse.org/r/71128

I consider this to be a safe fix that works inside lambda as well. The problem is I can't write a meaningful testcase for this in core.

Noopur, will you be able to review/test the fix and also write a test case on the UI front? Thanks!
Comment 14 Eclipse Genie CLA 2016-04-21 09:22:35 EDT
New Gerrit change created: https://git.eclipse.org/r/71141
Comment 15 Noopur Gupta CLA 2016-04-21 09:25:13 EDT
(In reply to Jay Arthanareeswaran from comment #13)
> (In reply to Eclipse Genie from comment #12)
> > New Gerrit change created: https://git.eclipse.org/r/71128
> 
> I consider this to be a safe fix that works inside lambda as well. The
> problem is I can't write a meaningful testcase for this in core.
> 
> Noopur, will you be able to review/test the fix and also write a test case
> on the UI front? Thanks!

I tested the fix and it works fine. We don't have tests in UI for templates other than SurroundWithTemplates. I managed to create a test case for this issue:

(In reply to Eclipse Genie from comment #14)
> New Gerrit change created: https://git.eclipse.org/r/71141

It can be committed after the jdt.core change.
Comment 17 Jay Arthanareeswaran CLA 2016-04-21 09:37:56 EDT
Thanks Noopur.

For the records, the fix also works for the issue Robert reported in comment #3.
Comment 19 Sasikanth Bharadwaj CLA 2016-04-26 07:44:56 EDT
Verified for Neon M7 using I20160425-1300 build