Bug 261534 - content assist after instanceof should also work after &&
Summary: content assist after instanceof should also work after &&
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.6 M7   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-01-19 12:16 EST by Markus Keller CLA
Modified: 2010-04-26 06:56 EDT (History)
2 users (show)

See Also:
srikanth_sankaran: review+


Attachments
proposed fix v1.0 + regression tests (6.83 KB, patch)
2010-04-20 13:50 EDT, Ayushman Jain CLA
no flags Details | Diff
updated patch for HEAD (6.82 KB, patch)
2010-04-22 02:58 EDT, Ayushman Jain CLA
no flags Details | Diff
proposed fix v2.0 + regression tests (8.47 KB, patch)
2010-04-22 08:01 EDT, Ayushman Jain CLA
no flags Details | Diff
Revised patch (8.47 KB, patch)
2010-04-22 08:50 EDT, Srikanth Sankaran CLA
srikanth_sankaran: iplog+
srikanth_sankaran: review+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Keller CLA 2009-01-19 12:16:25 EST
I20090114-1322

Content assist after instanceof should also propose members of the cast variable after an &&, e.g.:

	Object prevStatement= statements.get(idx - 1);
	if (prevStatement instanceof IfStatement && prevStatement.getE|) {
		
	}

Content assist at | should also propose getElseStatement(), like when the && is split into a nested if:

	if (prevStatement instanceof IfStatement) {
		if (prevStatement.getE|) { // proposes getElseStatement()
			
		}
	}
Comment 1 Srikanth Sankaran CLA 2010-03-31 05:37:50 EDT
Ayush, Please take a look.
Comment 2 Ayushman Jain CLA 2010-04-20 13:50:59 EDT
Created attachment 165470 [details]
proposed fix v1.0 + regression tests

The patch fixes this case as follows:
1) It recognises an instanceof expression when the completion node is in a binary expression through the change in CompletionParser#attachOrphanCompletionNode().

2) Once the instanceof expression is recognised, we go into CompletionParser#buildMoreCompletionEnclosingContext() where we build the ifstatement. Here, presently we use two counters - controlIndex, and blockIndex to look into the elementObjectInfoStack. Normally, for an instance of occuring inside an if statetent viz. 
if(o instanceof MyClass) {

a block delimiter, and a control delimiter is pushed on encountering the closing parantheses of the if statement. 

But in case of
if(o instanceof MyClass && o.|) {

we have found the assistNode even before we could find enclosing parentheses for the if statement. So the control and block indices are incorrect to be used in this case. For this purpose i've added another delimiter K_BETWEEN_INSTANCEOF_AND_RPAREN which will be pushed on consuming the instanceof Expression. So, in buildMoreCompletionEnclosingContext(), if we find no control delimiter, we will try finding the instance of delimiter through K_BETWEEN_INSTANCEOF_AND_RPAREN. If this is found then we'll use its index to look into the elementObjectInfoStack for the correct instanceof exp.

3) For the case
if(o instanceof MyClass && o.|) {

we try to build an AST which looks like this :
if(o instanceof MyClass) 
   <CompleteOnSingleNameReference:o>

This is to make sure that the completion engine can find the enclosing node for the assist node, and ascertain the need for finding proposals from the casted receiver. This is achieved through the change at CompletionParser.java:1101.

Note that this fix will not handle cases such as 

if ( i == 1 && o instanceof MyClass && o.|)

This and any related case will be handled with bug 304006.
Comment 3 Srikanth Sankaran CLA 2010-04-22 00:50:24 EDT
(In reply to comment #2)
> Created an attachment (id=165470) [details]
> proposed fix v1.0 + regression tests

Hi Ayush,

Can you regenerate the patch after synching with HEAD ?
This patch fails to apply as it is -- Thanks.
Comment 4 Ayushman Jain CLA 2010-04-22 02:58:32 EDT
Created attachment 165696 [details]
updated patch for HEAD

Same fix as above, updated for HEAD
Comment 5 Srikanth Sankaran CLA 2010-04-22 06:02:30 EDT
With the posted patch, when I complete at the first spot marked
with | I get blatantly invalid proposals.

package test;
public class CompletionAfterInstanceOf {
	public static void main(String [] args) {
	Object chars= args;
       if (chars instanceof String || chars.to|) {  // incorrect proposals show up here
	   }
       if (chars instanceof String && chars.to|) {  // this appears fixed.
	   }

	}
}

So the current patch needs to be reworked to avoid this problem.
Comment 6 Srikanth Sankaran CLA 2010-04-22 06:42:44 EDT
This case seems not to work too, though also without the patch.
Not sure if this was one of the limitations recognized by the
original enhancement.

package test;
public class CompletionAfterInstanceOf {
	public static void main(String [] args) {
	Object chars= args;
	Object chars2 = args;
       if (chars instanceof String && chars2 instanceof String) {
    	   chars.to|  // casted receiver proposals don't show up here,
    	   
	   }
	}
}
Comment 7 Ayushman Jain CLA 2010-04-22 08:01:57 EDT
Created attachment 165739 [details]
proposed fix v2.0 + regression tests

(In reply to comment #6)
> This case seems not to work too, though also without the patch.
> Not sure if this was one of the limitations recognized by the
> original enhancement.

This case comes under the umbrella of 'instanceof expressions inside binary expressions', which are being handled in bug 304006. So we should not cover this here.

Updated the fix for the case in comment # 5. Added it as a negative test in CompletionTests.testBug261534b()
Comment 8 Srikanth Sankaran CLA 2010-04-22 08:50:15 EDT
Created attachment 165750 [details]
Revised patch

This is the same Ayush's patch with some formatting
cleanups. Also replaced BinaryExpression with 
AND_AND_expression in one place, therebey tightening the
constraints.
Comment 9 Srikanth Sankaran CLA 2010-04-22 08:53:03 EDT
Released in HEAD for 3.6M7
Comment 10 Srikanth Sankaran CLA 2010-04-26 06:56:07 EDT
Verified for 3.6M7 using build I20100424-2000