IDEA-152289 Code completion suggests "return" as the first result in the empty constructor

This commit is contained in:
peter
2016-03-15 19:55:14 +01:00
parent 28b7c2501a
commit d9cbc98a49
5 changed files with 35 additions and 2 deletions
@@ -168,7 +168,9 @@ public class PreferByKindWeigher extends LookupElementWeigher {
if (object instanceof PsiKeyword) {
String keyword = ((PsiKeyword)object).getText();
if (PsiKeyword.RETURN.equals(keyword) && isLastStatement(PsiTreeUtil.getParentOfType(myPosition, PsiStatement.class))) {
if (PsiKeyword.RETURN.equals(keyword) &&
isLastStatement(PsiTreeUtil.getParentOfType(myPosition, PsiStatement.class)) &&
!isOnTopLevelInVoidMethod(myPosition)) {
return MyResult.probableKeyword;
}
if (PsiKeyword.ELSE.equals(keyword) || PsiKeyword.FINALLY.equals(keyword)) {
@@ -262,6 +264,17 @@ public class PreferByKindWeigher extends LookupElementWeigher {
return MyResult.normal;
}
private static boolean isOnTopLevelInVoidMethod(PsiElement position) {
PsiCodeBlock block = PsiTreeUtil.getParentOfType(position, PsiCodeBlock.class);
if (block != null) {
PsiElement parent = block.getParent();
if (parent instanceof PsiMethod) {
return ((PsiMethod)parent).isConstructor() || PsiType.VOID.equals(((PsiMethod)parent).getReturnType());
}
}
return false;
}
private static boolean isGetter(Object object) {
if (!(object instanceof PsiMethod)) return false;
@@ -1,5 +1,5 @@
class Util {
void foo(int reaction, boolean rezet) {
int foo(int reaction, boolean rezet) {
re<caret>
System.out.println();
}
@@ -0,0 +1,6 @@
class Util {
Util(int reaction, boolean rezet) {
re<caret>
}
}
@@ -0,0 +1,6 @@
class Util {
void foo(int reaction, boolean rezet) {
re<caret>
}
}
@@ -435,6 +435,14 @@ interface TxANotAnno {}
checkPreferredItems 0, 'reaction', 'rezet', 'return'
}
public void testDispreferReturnInConstructor() {
checkPreferredItems 0, 'reaction', 'rezet', 'return'
}
public void testDispreferReturnInVoidMethodTopLevel() {
checkPreferredItems 0, 'reaction', 'rezet', 'return'
}
public void testDoNotPreferGetClass() {
checkPreferredItems 0, 'get', 'getClass'
incUseCount(lookup, 1)