mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-138061 Prefer "return" completion suggestion before the last expression in a code block
This commit is contained in:
@@ -21,6 +21,7 @@ import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.LookupElementWeigher;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.openapi.util.Conditions;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.patterns.ElementPattern;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.filters.getters.MembersGetter;
|
||||
@@ -256,6 +257,18 @@ public class PreferByKindWeigher extends LookupElementWeigher {
|
||||
return true;
|
||||
}
|
||||
PsiStatement[] siblings = ((PsiCodeBlock)statement.getParent()).getStatements();
|
||||
return statement == siblings[siblings.length - 1];
|
||||
PsiStatement lastOne = siblings[siblings.length - 1];
|
||||
if (statement == lastOne) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// we might complete 'return' before an expression, then it's still last statement
|
||||
if (siblings.length >= 2 && statement == siblings[siblings.length - 2] && lastOne instanceof PsiExpressionStatement) {
|
||||
int start = statement.getTextRange().getStartOffset();
|
||||
int end = lastOne.getTextRange().getStartOffset();
|
||||
return !StringUtil.contains(statement.getContainingFile().getViewProvider().getContents(), start, end, '\n');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
public class Test {
|
||||
|
||||
int rMethod() {}
|
||||
|
||||
int foo(int rParam) {
|
||||
Object rLocal;
|
||||
r<caret> rMethod();
|
||||
}
|
||||
|
||||
}
|
||||
+4
@@ -288,6 +288,10 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase {
|
||||
checkPreferredItems(0, "return", "rLocal", "rParam", "rMethod");
|
||||
}
|
||||
|
||||
public void testPreferReturnBeforeExpression() {
|
||||
checkPreferredItems(0, "return", "rLocal", "rParam", "rMethod");
|
||||
}
|
||||
|
||||
public void testPreferModifiers() {
|
||||
checkPreferredItems(0, "private", "protected", "public", "paaa", "paab");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user