Bug 430818 - [1.8][quick fix] Quick fix for "for loop" is not shown for bare local variable/argument/field
Summary: [1.8][quick fix] Quick fix for "for loop" is not shown for bare local variabl...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.4   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.4 RC1   Edit
Assignee: Lukas Hanke CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on: 437927 430336
Blocks: 434188
  Show dependency tree
 
Reported: 2014-03-20 13:54 EDT by Robin Stocker CLA
Modified: 2014-06-23 08:34 EDT (History)
6 users (show)

See Also:
markus.kell.r: review+
manju656: review+


Attachments
This patch fixes the bug (25.86 KB, patch)
2014-04-29 12:41 EDT, Lukas Hanke CLA
no flags Details | Diff
fix for hovering (5.38 KB, patch)
2014-05-15 12:29 EDT, Lukas Hanke CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Robin Stocker CLA 2014-03-20 13:54:00 EDT
Using I20140318-0830, the quick fixes from bug 241696 for for loops are not shown in the following situations:

    List<String> list = ...;
    list<-- Cursor position here

It also doesn't work for arguments or fields. Also not for other collection types or arrays.

What works is this:

    Map<String, String> map = ...;
    map.entrySet()<-- Cursor position here

The first examples shows the following errors in the ruler:

- Syntax error, insert ";" to complete LocalVariableDeclarationStatement
- list cannot be resolved to a type
- Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration

The second example shows only the following error (which may be the cause for the difference):

- Syntax error, insert ";" to complete Statement
Comment 1 Markus Keller CLA 2014-03-20 14:53:57 EDT
Yes, and we have 12 test failures in master for this.

The problem is the bad AST recovery, see bug 405780. We should first try to fix that problem in JDT Core. If that doesn't work out for 4.4, then we can work around it in the quick fix implementation.
Comment 2 Markus Keller CLA 2014-03-20 14:56:46 EDT
Oops, cited wrong bug. Bug 430336 is the blocker for this one.
Comment 3 Markus Keller CLA 2014-04-10 16:35:43 EDT
Disabled 12 failing tests for now:

org.eclipse.jdt.ui.tests.quickfix.LocalCorrectionsQuickFixTest.testGenerateForeachNotAddedForLowVersion
org.eclipse.jdt.ui.tests.quickfix.LocalCorrectionsQuickFixTest.testLoopOverAddedToFixesForVariablehNotAddedForLowVersion
and 10 in org.eclipse.jdt.ui.tests.quickfix.AssistQuickFixTest#testGenerateFor*

http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=67e6de8681d025aa60eef7e3f6dbe2211cc0fb18
Comment 4 Markus Keller CLA 2014-04-23 13:42:11 EDT
Lukas, your nice new "Create 'for' loop" quick fixes don't work any more, and bug 430336 seems to be too hard to fix for 4.4.

Would you have time to adapt them to the current structure of the recovered AST?
Comment 5 Lukas Hanke CLA 2014-04-24 03:18:07 EDT
Ok, I'll take a closer look at it.
Comment 6 Lukas Hanke CLA 2014-04-29 12:41:28 EDT
Created attachment 242505 [details]
This patch fixes the bug

I adapted the implementation to the available information of Luna's AST recovery.
Please note, I had to change the tests as well, because some assertions depend on the results of other quick assists/fixes (e.g. assign to local variables?) that are broken in Luna :/.
Comment 7 Markus Keller CLA 2014-05-13 19:26:31 EDT
Manju, could you please review this patch? Don't release it yet, since I need to review it as well, and since there's still a faint hope that we might find a solution for bug 430336.
Comment 8 Martin Mathew CLA 2014-05-14 02:30:43 EDT
All the tests pass with this patch.

1. Update Copyright year for GenerateForLoopAssistProposal.

2. Consider the below code snippet:
package snippet;

import java.util.Collection;

public class E {
	void foo(Collection<String> collection) {
		collection
	}
}

Hovering over 'collection' shows all quick fixes available except create for loop, but when user press Ctrl+1 then the additional 2 quick assists are added to the list. This looks inconsistent.

Also this doesn't solve the issue reported in Bug 434188.
Comment 9 Markus Keller CLA 2014-05-14 14:56:39 EDT
Fixed with http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=ff852ca64a584ee5ec86f0dd71d0cccc3d80b4d5

I've added a relevance boost in QuickAssistProcessor#getGenerateForLoopProposals(..) to get the for-loop quick assists to show up in front of the "Create class ..." quick fixes (which only show up due to the bad recovery).

We will disable the quick fixes in bug 434188 because we don't know how to compute their availability accurately enough. The quick assists will still be available via Ctrl+1 (but not in the Quick Fix hover). That's not perfect, but the best we can do for Luna.
Comment 10 Lukas Hanke CLA 2014-05-15 12:29:53 EDT
Created attachment 243135 [details]
fix for hovering

Hi, I'm not sure if it's still needed, but the above patch adds the generate for loop quick assist on hovering again. The proposals to generate for loops will be added correctly now, but there are still inconsistencies for the 'assign to local variable'-quickfix. If the hovering feature shall stay disabled for consistency purposes, just ignore this patch. It can be applied to the current master branch.
Comment 11 Markus Keller CLA 2014-05-15 13:52:00 EDT
(In reply to Lukas Hanke from comment #10)
> Created attachment 243135 [details] [diff]
> fix for hovering

Nice try :-) For the case in comment 8, you rely on the bad AST recovery that already makes "Create class"-like quick fixes appear and then put the good ones on top. Let me think a bit more about this -- maybe I'll even accept that hack for now.

But the "case IProblem.ParsingErrorInsertToComplete:" in QuickFixProcessor#hasCorrections(ICompilationUnit, int) is still a no-go
I've added some more explanation for this in bug 434188 comment 7.
Comment 12 Noopur Gupta CLA 2014-05-19 05:01:40 EDT
Verified as working (quick assists) in build id: I20140515-1230.