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

Bug 539617

Summary: Content assist not working for constructors in lamda expressions
Product: [Eclipse Project] JDT Reporter: Kilian Brachtendorf <Kilian.Brachtendorf>
Component: CoreAssignee: Stephan Herrmann <stephan.herrmann>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: Kilian.Brachtendorf, manoj.palat, mauromol, stephan.herrmann
Version: 4.9   
Target Milestone: 4.20 M3   
Hardware: All   
OS: All   
See Also: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/130512
https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=f19d20e6a917016c9405829df3732ebb1d810478
https://bugs.eclipse.org/bugs/show_bug.cgi?id=539685
Whiteboard:
Attachments:
Description Flags
Content assist actual output none

Description Kilian Brachtendorf CLA 2018-09-28 08:45:08 EDT
Created attachment 276045 [details]
Content assist actual output

The content assist does not suggest any constructors for objects created in lambda expressions. 

Minimal example:
public class CodeCompletion {
	public static void main(String[] args) {
		new Thread( () -> {
			Double d = new Double(
		});
	}
}

Expected:
 Double(double value) 
 Double(String s)
suggested in the content assist window.

Actual: 
 see screenshot


Tested on 4.9.0 Windows 10 and 4.8.0 macOS 10.13.6
Comment 1 Eclipse Genie CLA 2018-10-05 18:02:03 EDT
New Gerrit change created: https://git.eclipse.org/r/130512
Comment 2 Stephan Herrmann CLA 2018-10-05 18:46:15 EDT
(In reply to Eclipse Genie from comment #1)
> New Gerrit change created: https://git.eclipse.org/r/130512

New test that codifies the existing unexpected behavior.

Interestingly, this is the recovered AST as returned by CompletionParser:

public class CodeCompletion {
  public CodeCompletion() {
  }
  public static void main(String[] args) {
    new Thread(() -> {
  Double d = new Double(<CompleteOnName:>);
});
  }
}

This means, syntax recovery has invented the closing ");".

When, however, the same statement occurs outside lambdas, it is parsed as

    Double d = <CompleteOnAllocationExpression:new Double()>;


This difference in parse trees explains the unexpected result.

The undesired <CompleteOnName:> is created from 
CompletionOnSingleNameReference.<init>(char[], long, char[][], boolean, boolean) line: 52	
CompletionParser.createSingleAssistNameReference(char[], long) line: 4924	
CompletionParser(AssistParser).getUnspecifiedReferenceOptimized() line: 1613	
CompletionParser.getUnspecifiedReferenceOptimized() line: 5099	
CompletionParser(Parser).consumePostfixExpression() line: 6106	
CompletionParser(Parser).consumeRule(int) line: 7588	

The empty identifier consumed here is invented by the scanner when if finds a newline at the faked EOF position.


All this implies the bug has nothing to do with bug 535743 as I had hoped.
Comment 3 Stephan Herrmann CLA 2021-04-24 18:06:23 EDT
I had a fresh look at this after bug 539685. In that bug one part of the change was to let CompletionParser override newMessageSend(). Coming back here I realized that Parser didn't even have newAllocationExpression() that I could override. After fixing that omission things were moving in a good direction. See patch sets #2 & #3 in gerrit.

Strangly now we sometimes get structures like <CompletionOnQualifiedAllocation:new Foo(<CompletionOnName:>)>, i.e., nested completion nodes. This happens because we hit the cursor location before any AST is created for the allocation, so we create that <CompletionOnName:>, which later becomes an argument of the later created allocation.

For now I simply ensured that the outer node is the one firing CompletionNodeFound, although the inner node would throw it before the outer completes. We might want to come back later to check if we can avoid the inner completion node to begin with, but for now it seems we can well cope with it.

It's nice to see that the fix here also resolves what I discussed in bug 539685 comment 32. The affected tests that I changed via the bug could be reverted almost to the previous state in this bug.
Comment 5 Manoj N Palat CLA 2021-05-18 21:35:20 EDT
Verified for 4.20 M3 Version: 2021-06 (4.20)Build id: I20210518-0850