nothing but .class after primitive or array types in basic completion (IDEA-72265)

This commit is contained in:
peter
2011-09-13 12:56:59 +02:00
parent c3372a3de5
commit 9ddcf4fde8
7 changed files with 73 additions and 11 deletions

View File

@@ -132,7 +132,8 @@ public class JavaCompletionContributor extends CompletionContributor {
if (JavaCompletionData.AFTER_TRY_BLOCK.isAcceptable(position, position) ||
JavaCompletionData.START_SWITCH.isAcceptable(position, position) ||
JavaCompletionData.INSTANCEOF_PLACE.isAcceptable(position, position)) {
JavaCompletionData.INSTANCEOF_PLACE.isAcceptable(position, position) ||
JavaCompletionData.isAfterPrimitiveOrArrayType(position)) {
return null;
}
@@ -202,7 +203,7 @@ public class JavaCompletionContributor extends CompletionContributor {
final CompletionResultSet result = JavaCompletionSorting.addJavaSorting(parameters, _result);
if (ANNOTATION_ATTRIBUTE_NAME.accepts(position)) {
if (ANNOTATION_ATTRIBUTE_NAME.accepts(position) && !JavaCompletionData.isAfterPrimitiveOrArrayType(position)) {
completeAnnotationAttributeName(result, position, parameters);
result.stopHere();
return;
@@ -370,6 +371,11 @@ public class JavaCompletionContributor extends CompletionContributor {
if (grand instanceof PsiNewExpression && ((PsiNewExpression)grand).getQualifier() != null) {
return false;
}
if (JavaCompletionData.isAfterPrimitiveOrArrayType(position)) {
return false;
}
return true;
}

View File

@@ -22,7 +22,10 @@ import com.intellij.codeInsight.completion.util.ParenthesesInsertHandler;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupItem;
import com.intellij.codeInsight.lookup.TailTypeDecorator;
import com.intellij.patterns.*;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.PsiElementPattern;
import com.intellij.patterns.PsiJavaElementPattern;
import com.intellij.patterns.PsiJavaPatterns;
import com.intellij.psi.*;
import com.intellij.psi.filters.*;
import com.intellij.psi.filters.classes.EnumOrAnnotationTypeFilter;
@@ -32,6 +35,7 @@ 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;
@@ -152,6 +156,8 @@ public class JavaCompletionData extends JavaAwareCompletionData{
new ScopeFilter(new ClassFilter(JspClassLevelDeclarationStatement.class)));
public static final ElementPattern<PsiElement> START_FOR =
psiElement().afterLeaf(psiElement().withText("(").afterLeaf("for")).withParents(PsiJavaCodeReferenceElement.class, PsiExpressionStatement.class, PsiForStatement.class);
private static final PsiJavaElementPattern.Capture<PsiElement> CLASS_REFERENCE =
psiElement().withParent(psiElement().referencing(psiClass()));
public JavaCompletionData(){
declareCompletionSpaces();
@@ -557,15 +563,32 @@ 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);
}
private static void addClassLiteral(CompletionResultSet result, PsiElement position) {
if (psiElement().afterLeaf(psiElement().withText(".").afterLeaf(
or(
psiElement().withParent(psiElement().referencing(psiClass())),
psiElement().withText(string().oneOf("]", PsiKeyword.VOID)),
psiElement().withText(string().oneOf(PRIMITIVE_TYPES))
))).accepts(position) &&
!INSIDE_PARAMETER_LIST.accepts(position) &&
!(position.getContainingFile() instanceof PsiJavaCodeReferenceCodeFragment)) {
if (INSIDE_PARAMETER_LIST.accepts(position) || position.getContainingFile() instanceof PsiJavaCodeReferenceCodeFragment) {
return;
}
if (psiElement().afterLeaf(psiElement().withText(".").afterLeaf(CLASS_REFERENCE)).accepts(position) ||
isAfterPrimitiveOrArrayType(position)) {
result.addElement(createKeyword(position, PsiKeyword.CLASS));
}
}

View File

@@ -0,0 +1,9 @@
private class Zooooooo {
@interface B {
Class<?> value();
}
@B(byte[].c<caret>)
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,9 @@
private class Zooooooo {
@interface B {
Class<?> value();
}
@B(byte[].class<caret>)
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,5 @@
private class Zooooooo {
{
Class<?> cls = byte[].Cla<caret>
}
}

View File

@@ -996,6 +996,8 @@ public class NormalCompletionTest extends LightFixtureCompletionTestCase {
public void testStaticInnerExtendingOuter() throws Exception { doTest() }
public void testPrimitiveClass() throws Exception { doTest() }
public void testPrimitiveArrayClass() throws Exception { doTest() }
public void testPrimitiveArrayOnlyClass() throws Exception { doAntiTest() }
public void testPrimitiveArrayInAnno() throws Exception { doTest() }
public void testSaxParserCommonPrefix() throws Exception {
myFixture.addClass("public class SAXParser {}")

View File

@@ -21,6 +21,7 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.search.PsiElementProcessor;
@@ -824,6 +825,13 @@ public class PsiTreeUtil {
return prevLeaf;
}
@Nullable
public static PsiElement prevVisibleLeaf(@NotNull final PsiElement element) {
PsiElement prevLeaf = prevLeaf(element, true);
while (prevLeaf != null && StringUtil.isEmptyOrSpaces(prevLeaf.getText())) prevLeaf = prevLeaf(prevLeaf, true);
return prevLeaf;
}
@Nullable
public static PsiElement nextLeaf(final PsiElement element, final boolean skipEmptyElements) {
PsiElement nextLeaf = nextLeaf(element);