smart completion of primitive and array type class object access (IDEA-72265)

This commit is contained in:
peter
2011-09-13 17:06:51 +02:00
parent aea5cea3d5
commit a3a880da44
8 changed files with 45 additions and 30 deletions
@@ -21,7 +21,6 @@ import com.intellij.codeInsight.template.SmartCompletionContextType;
import com.intellij.codeInsight.template.impl.TemplateImpl;
import com.intellij.codeInsight.template.impl.TemplateSettings;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.PsiJavaPatterns;
import com.intellij.psi.*;
import com.intellij.psi.filters.getters.ClassLiteralGetter;
import com.intellij.psi.filters.getters.JavaMembersGetter;
@@ -36,7 +35,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.Map;
import static com.intellij.patterns.PsiJavaPatterns.psiClass;
import static com.intellij.patterns.PsiJavaPatterns.psiElement;
/**
@@ -63,10 +61,7 @@ public class BasicExpressionCompletionContributor {
final Consumer<LookupElement> result,
PrefixMatcher matcher) {
final PsiElement element = parameters.getPosition();
if (PsiJavaPatterns.psiElement().afterLeaf(
PsiJavaPatterns.psiElement().withText(".").afterLeaf(
PsiJavaPatterns.psiElement().withParent(
PsiJavaPatterns.psiElement().referencing(psiClass())))).accepts(element)) {
if (JavaCompletionData.isAfterTypeDot(element)) {
addKeyword(result, element, PsiKeyword.CLASS);
addKeyword(result, element, PsiKeyword.THIS);
@@ -35,7 +35,6 @@ import com.intellij.psi.filters.getters.JavaMembersGetter;
import com.intellij.psi.filters.position.*;
import com.intellij.psi.filters.types.TypeCodeFragmentIsVoidEnabledFilter;
import com.intellij.psi.impl.source.jsp.jspJava.JspClassLevelDeclarationStatement;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.jsp.JspElementType;
import com.intellij.psi.templateLanguages.OuterLanguageElement;
import com.intellij.psi.util.PsiTreeUtil;
@@ -530,7 +529,9 @@ public class JavaCompletionData extends JavaAwareCompletionData{
addPrimitiveTypes(result, position);
addClassLiteral(result, position);
if (isAfterTypeDot(position)) {
result.addElement(createKeyword(position, PsiKeyword.CLASS));
}
final ProcessingContext context = new ProcessingContext();
if (psiElement().afterLeaf(
@@ -564,33 +565,19 @@ public class JavaCompletionData extends JavaAwareCompletionData{
}
static boolean isAfterPrimitiveOrArrayType(PsiElement element) {
element = PsiTreeUtil.prevVisibleLeaf(element);
if (element == null || !element.textMatches(".")) return false;
boolean array = false;
while (true) {
element = PsiTreeUtil.prevVisibleLeaf(element);
if (element == null) return false;
if (element.textMatches("]")) {
array = true;
element = PsiTreeUtil.prevVisibleLeaf(element);
if (element == null || !element.textMatches("[")) return false;
} else {
break;
}
}
return psiElement().withElementType(ElementType.PRIMITIVE_TYPE_BIT_SET).accepts(element) || array && CLASS_REFERENCE.accepts(element);
return psiElement().withParent(
psiReferenceExpression().withFirstChild(
psiElement(PsiClassObjectAccessExpression.class).withLastChild(
not(psiElement().withText(PsiKeyword.CLASS))))).accepts(element);
}
private static void addClassLiteral(CompletionResultSet result, PsiElement position) {
static boolean isAfterTypeDot(PsiElement position) {
if (INSIDE_PARAMETER_LIST.accepts(position) || position.getContainingFile() instanceof PsiJavaCodeReferenceCodeFragment) {
return;
return false;
}
if (psiElement().afterLeaf(psiElement().withText(".").afterLeaf(CLASS_REFERENCE)).accepts(position) ||
isAfterPrimitiveOrArrayType(position)) {
result.addElement(createKeyword(position, PsiKeyword.CLASS));
}
return psiElement().afterLeaf(psiElement().withText(".").afterLeaf(CLASS_REFERENCE)).accepts(position) ||
isAfterPrimitiveOrArrayType(position);
}
private static void addPrimitiveTypes(CompletionResultSet result, PsiElement position) {
@@ -109,6 +109,7 @@ public class ReferenceExpressionCompletionContributor {
public void run() {
final PsiElement element = parameters.getPosition();
if (JavaSmartCompletionContributor.INSIDE_TYPECAST_EXPRESSION.accepts(element)) return;
if (JavaCompletionData.isAfterPrimitiveOrArrayType(element)) return;
final int offset = parameters.getParameters().getOffset();
final PsiReference reference = element.getContainingFile().findReferenceAt(offset);
@@ -0,0 +1,5 @@
class Bar {
public static void main(String[] args) {
Class<?> cls = byte[].class;<caret>
}
}
@@ -0,0 +1,5 @@
class Bar {
public static void main(String[] args) {
Class<?> cls = byte[].<caret>
}
}
@@ -0,0 +1,10 @@
class Bar {
@interface B {
Class<?> value();
}
@B(byte[].class<caret>)
public static void main(String[] args) {
}
}
@@ -0,0 +1,10 @@
class Bar {
@interface B {
Class<?> value();
}
@B(byte[].<caret>)
public static void main(String[] args) {
}
}
@@ -951,6 +951,8 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase {
public void testStaticallyImportedField() throws Throwable { doTest(); }
public void testSiblingOfAStaticallyImportedField() throws Throwable { doTest(); }
public void testPrimitiveArrayClassInMethod() throws Throwable { doTest(); }
public void testPrimitiveClassInAnno() throws Throwable { doTest(); }
public void testInferFromCall() throws Throwable {
doTest();