Bug 195346 - [assist] Array type should be filtered while completing in case of a switch
Summary: [assist] Array type should be filtered while completing in case of a switch
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P5 normal (vote)
Target Milestone: 3.7 M1   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-07-04 04:11 EDT by David Audel CLA
Modified: 2010-08-03 06:12 EDT (History)
2 users (show)

See Also:
srikanth_sankaran: review+


Attachments
proposed fix v1.0 + regression tests (13.05 KB, patch)
2010-05-31 06:08 EDT, Ayushman Jain CLA
no flags Details | Diff
improved fix + regression tests (17.61 KB, patch)
2010-06-01 14:41 EDT, Ayushman Jain CLA
no flags Details | Diff
updated patch (17.83 KB, patch)
2010-06-21 08:05 EDT, Ayushman Jain CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description David Audel CLA 2007-07-04 04:11:53 EDT
from bug 111882 comment 0

Array type should be filtered while completing in case of a switch

class X {
  static char[] AN_ARRAY = new char[10];
  static int AN_INT_VALUE = 0;
  static int ANOTHER_VALUE = 1;

  void foo(int i) {
    switch (i) {
      case AN // <-- complete after the 'AN'
    }
  }
}

AN_ARRAY shouldn't be proposed.
Comment 1 David Audel CLA 2007-07-04 04:16:00 EDT
Currently proposals which are not compatible with the expected type are proposed because an indirect access to a valid proposal is still possible.
But when the type is an array there is no possible indirect proposal, so we could filtered these proposals.

We could add the same behavior for final classes and types with no compatible members.
Comment 2 Ayushman Jain CLA 2010-05-31 06:08:47 EDT
Created attachment 170505 [details]
proposed fix v1.0 + regression tests

This patch does the following two things:
1) It inhibits array types from showing up in proposals inside case expressions
2) Since case expressions must be constant expressions, it bumps up the priority of final variables and fields by 1 so that they always show up at the top of the list.

(1) is achieved by introducing a new boolean CompletionEngine$assistNodeIsInsideCase. If the proposal being considered is an array type and this flag is set, it is filtered off.

(2) is achieved by a new relevance constant RelevanceConstants$R_FINAL. This gets added to the relevance computation of final variables and fields when completing inside a case expression.
Comment 3 Ayushman Jain CLA 2010-05-31 06:16:35 EDT
(In reply to comment #1)

> We could add the same behavior for final classes and types with no compatible
> members.

(1) Final classes shouldnt be filtered off. Consider the following scenario:

----------8<------------------------------
final class TryFinal {
	public  static final int abc= 1;
}
public class TryMe {
  static final int TANOTHER_VALUE = 1;
  final class TryFinalInner {
		public static final   int abc2= 1;
	}

  void foo(int i) {
    switch (i) {
      case T: System.out.println();// <-- complete after the 'AN'
    }
  }
}

The proposals TANOTHER_VALUE, TryFinal.abc and TryFinalInner.abc2 are all correct. Hence final classes shouldnt be left out.

(2) Types with no compatible member types can be filtered off. But this will involve going through all the types defined and then going through each member, which will be very type consuming if either, or both of these are large in number. Adding such time complexity to content assist could result in a lot of chaos. So IMHO, filtration should not to be done so deeply.
Comment 4 Srikanth Sankaran CLA 2010-05-31 07:28:02 EDT
Comments on the patch : 

(1) In the following example,

class X {
  static char[] AN_ARRAY = new char[10];
  static int AN_INT_VALUE = 0;
  static int ANOTHER_VALUE = 1;

  void foo(int i, int [] AN_ARGUMENT_ARRAY) {
    switch (i) {
      case AN:break;
    }
  }
}

AN_ARGUMENT_ARRAY is proposed, I would have expected it to be suppressed
pretty much like AN_ARRAY was suppressed. Likewise for locals.

(2) I would suggest the setting of this.assistNodeIsInsideCase happen
only in the relevant situations by invoking a new method pretty much
along the lines org.eclipse.jdt.internal.codeassist.CompletionEngine.assistNodeIsExtendedType(ASTNode, ASTNode) is used.

(3) Should R_FINAL be something other than just a boost by 1 ? It sounds
like a very small boost given that any non final things we propose there
will cause a compile error. We don't want this to be very that this comes
to dominate either.

(4) dont ==> should be don't in comments. Otherwise eclipse draws a line
line indicating a spelling error and it is distracting needlessly. Likewise
arent ==> aren't
proriority ==> priority
Comment 5 Ayushman Jain CLA 2010-06-01 14:41:58 EDT
Created attachment 170675 [details]
improved fix + regression tests

incorporated the review comments into this new patch.

Points 1 and 4 from comment 4 have been taken care of as is. 

point 2 - setting of the new flag CompletionEngine$assistNodeIsInsideCase has been moved to individual completion functions for single name ref, qualified name ref, single type ref and qualified type ref. This hasnt been done for parameterized type ref because inside a case expression they wont be used.

point 3 - bumped up the relevance of the new constant R_FINAL to 3 from 1. This should suffice to bring the constants on top of the list in this case. If we come across a case in the future when a non-final variable still appears with a greater relevance for some reason, we can increase the relevance number further.
Comment 6 Srikanth Sankaran CLA 2010-06-21 02:54:57 EDT
(In reply to comment #5)
> Created an attachment (id=170675) [details]
> improved fix + regression tests
> 
> incorporated the review comments into this new patch.

I gave this patch a quick once over and it looks fine.
Just a couple of comments:

for (int i = 0; i < ((SwitchStatement) astNodeParent).caseCount; i++) {

is better coded as

for (int i = 0, caseCount = ((SwitchStatement) astNodeParent).caseCount;  i < caseCount; i++) {

This should have been pointed out with the earlier patch itself.
Sorry, I missed it.
Comment 7 Ayushman Jain CLA 2010-06-21 08:05:54 EDT
Created attachment 172318 [details]
updated patch

updated patch to incorporate comment 6 and updated for HEAD.
Comment 8 Ayushman Jain CLA 2010-06-22 14:42:39 EDT
Released in HEAD for 3.7M1.
Added tests:
CompletionTests.testBug195346a()
CompletionTests.testBug195346b()
Comment 9 Srikanth Sankaran CLA 2010-08-03 06:12:58 EDT
Verified for 3.7 M1 using build I20100802-180