suggest primitive types in expression contexts in case one wants to write int.class (IDEA-90013)

This commit is contained in:
peter
2012-08-13 11:27:39 +02:00
parent 1f9aa00c72
commit 7791e6848c
7 changed files with 30 additions and 5 deletions
@@ -155,7 +155,7 @@ public class JavaCompletionData extends JavaAwareCompletionData {
private static final PsiJavaElementPattern.Capture<PsiElement> CLASS_REFERENCE =
psiElement().withParent(psiReferenceExpression().referencing(psiClass()));
public static final ElementPattern<PsiElement> EXPR_KEYWORDS = and(
private static final ElementPattern<PsiElement> EXPR_KEYWORDS = and(
psiElement().withParent(psiElement(PsiReferenceExpression.class).withParent(
not(
or(psiElement(PsiTypeCastExpression.class),
@@ -455,7 +455,7 @@ public class JavaCompletionData extends JavaAwareCompletionData {
}
}
if (EXPR_KEYWORDS.accepts(position)) {
if (isExpressionPosition(position)) {
if (PsiTreeUtil.getParentOfType(position, PsiAnnotation.class) == null) {
result.addElement(TailTypeDecorator.withTail(createKeyword(position, PsiKeyword.NEW), TailType.INSERT_SPACE));
result.addElement(createKeyword(position, PsiKeyword.NULL));
@@ -516,6 +516,11 @@ public class JavaCompletionData extends JavaAwareCompletionData {
}
}
private static boolean isExpressionPosition(PsiElement position) {
return EXPR_KEYWORDS.accepts(position) ||
psiElement().insideStarting(psiElement(PsiClassObjectAccessExpression.class)).accepts(position);
}
public static boolean isInstanceofPlace(PsiElement position) {
return INSTANCEOF_PLACE.isAcceptable(position, position);
}
@@ -590,16 +595,18 @@ public class JavaCompletionData extends JavaAwareCompletionData {
boolean typeFragment = position.getContainingFile() instanceof PsiTypeCodeFragment && PsiTreeUtil.prevVisibleLeaf(position) == null;
boolean declaration = DECLARATION_START.accepts(position);
boolean expressionPosition = isExpressionPosition(position);
if (START_FOR.accepts(position) ||
isInsideParameterList(position) && !AFTER_DOT.accepts(position) ||
VARIABLE_AFTER_FINAL.accepts(position) ||
inCast ||
declaration ||
typeFragment ||
expressionPosition ||
isStatementPosition(position)) {
for (String primitiveType : PRIMITIVE_TYPES) {
LookupElement keyword = createKeyword(position, primitiveType);
result.addElement(inCast || typeFragment ? keyword : new OverrideableSpace(keyword, TailType.HUMBLE_SPACE_BEFORE_WORD));
result.addElement(inCast || typeFragment || expressionPosition ? keyword : new OverrideableSpace(keyword, TailType.HUMBLE_SPACE_BEFORE_WORD));
}
}
if (declaration) {
@@ -0,0 +1,5 @@
public class Util {
int goo() {
Class[] c = {AbstractMethodError.class, <caret>}
}
}
@@ -0,0 +1,5 @@
public class Util {
int goo() {
new Class[]{AbstractMethodError.class, <caret>.class}
}
}
@@ -0,0 +1,5 @@
public class Util {
int goo() {
new Class[]{<caret>}
}
}
@@ -104,6 +104,9 @@ public class KeywordCompletionTest extends LightCompletionTestCase {
public void testReturnInTernary() throws Exception { doTest(1, "return"); }
public void testFinalAfterParameterAnno() throws Exception { doTest(2, "final", "float", "class"); }
public void testClassInMethod() throws Exception { doTest(2, "class", "char"); }
public void testIntInClassArray() throws Throwable { doTest(2, "int", "char", "final"); }
public void testIntInClassArray2() throws Throwable { doTest(2, "int", "char", "final"); }
public void testIntInClassArray3() throws Throwable { doTest(2, "int", "char", "final"); }
public void testTryInExpression() throws Exception {
configureByFile(BASE_PATH + "/" + getTestName(true) + ".java");
@@ -48,7 +48,7 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase {
}
public void testReturnF() throws Throwable {
checkPreferredItems(0, "false", "finalize");
checkPreferredItems(0, "false", "float", "finalize");
}
public void testPreferDefaultTypeToExpected() throws Throwable {
@@ -773,7 +773,7 @@ public class ListUtils {
public void testDoubleFalse() throws Throwable {
configureByFile(getTestName(false) + ".java");
assertFirstStringItems("false", "fefefef", "finalize");
assertFirstStringItems("false", "fefefef", "float", "finalize");
}
public void testSameNamedVariableInNestedClasses() throws Throwable {