class is a valid suggestion in method body

This commit is contained in:
peter
2012-08-08 12:18:54 +02:00
parent 8a46fd89ba
commit 0d3bc73083
5 changed files with 32 additions and 33 deletions
@@ -50,15 +50,6 @@ public class Java15CompletionData extends JavaCompletionData {
registerVariant(variant);
}
{
final CompletionVariant variant = new CompletionVariant(PsiJavaFile.class, CLASS_START);
variant.includeScopeClass(PsiClass.class);
variant.addCompletion(PsiKeyword.ENUM, TailType.HUMBLE_SPACE_BEFORE_WORD);
registerVariant(variant);
}
}
protected void initVariantsInClassScope() {
@@ -138,17 +138,6 @@ public class JavaCompletionData extends JavaAwareCompletionData {
not(psiElement().inside(PsiAnnotation.class)),
not(START_SWITCH));
public static final AndFilter CLASS_START = new AndFilter(
new OrFilter(
END_OF_BLOCK,
new PatternFilter(psiElement().afterLeaf(
or(
psiElement().withoutText(".").inside(psiElement(PsiModifierList.class).withParent(not(psiElement(PsiParameter.class)))).andNot(
psiElement().inside(PsiAnnotationParameterList.class)),
psiElement().isNull())))
),
new PatternFilter(not(psiElement().afterLeaf("@"))));
private static final String[] PRIMITIVE_TYPES = new String[]{
PsiKeyword.SHORT, PsiKeyword.BOOLEAN,
PsiKeyword.DOUBLE, PsiKeyword.LONG,
@@ -233,16 +222,6 @@ public class JavaCompletionData extends JavaAwareCompletionData {
}
protected void initVariantsInFileScope(){
{
final CompletionVariant variant = new CompletionVariant(PsiJavaFile.class, CLASS_START);
variant.includeScopeClass(PsiClass.class);
variant.addCompletion(PsiKeyword.CLASS);
variant.addCompletion(PsiKeyword.INTERFACE);
registerVariant(variant);
}
}
/**
@@ -513,11 +492,17 @@ public class JavaCompletionData extends JavaAwareCompletionData {
result.addElement(TailTypeDecorator.withTail(createKeyword(position, PsiKeyword.FINAL), TailType.HUMBLE_SPACE_BEFORE_WORD));
}
if (CLASS_START.isAcceptable(position, position) &&
PsiTreeUtil.getNonStrictParentOfType(position, PsiLiteralExpression.class, PsiComment.class) == null) {
if (isSuitableForClass(position)) {
for (String s : ModifierChooser.getKeywords(position)) {
result.addElement(new OverrideableSpace(createKeyword(position, s), TailType.HUMBLE_SPACE_BEFORE_WORD));
}
result.addElement(new OverrideableSpace(createKeyword(position, PsiKeyword.CLASS), TailType.HUMBLE_SPACE_BEFORE_WORD));
if (PsiTreeUtil.getParentOfType(position, PsiCodeBlock.class, true, PsiMember.class) == null) {
result.addElement(new OverrideableSpace(createKeyword(position, PsiKeyword.INTERFACE), TailType.HUMBLE_SPACE_BEFORE_WORD));
if (PsiUtil.getLanguageLevel(position).isAtLeast(LanguageLevel.JDK_1_5)) {
result.addElement(new OverrideableSpace(createKeyword(position, PsiKeyword.ENUM), TailType.INSERT_SPACE));
}
}
}
addPrimitiveTypes(result, position);
@@ -538,6 +523,23 @@ public class JavaCompletionData extends JavaAwareCompletionData {
}
}
public static boolean isSuitableForClass(PsiElement position) {
if (psiElement().afterLeaf("@").accepts(position) ||
PsiTreeUtil.getNonStrictParentOfType(position, PsiLiteralExpression.class, PsiComment.class) != null) {
return false;
}
if (psiElement().afterLeaf(
or(
psiElement().withoutText(".").inside(psiElement(PsiModifierList.class).withParent(not(psiElement(PsiParameter.class)))).andNot(
psiElement().inside(PsiAnnotationParameterList.class)),
psiElement().isNull())).accepts(position)) {
return true;
}
return END_OF_BLOCK.isAcceptable(position, position);
}
static void addExpectedTypeMembers(CompletionParameters parameters, final CompletionResultSet result, PsiElement position) {
for (final ExpectedTypeInfo info : JavaSmartCompletionContributor.getExpectedTypes(parameters)) {
new JavaMembersGetter(info.getDefaultType(), parameters).addMembers(parameters.getInvocationCount() > 1, new Consumer<LookupElement>() {
@@ -159,7 +159,7 @@ public abstract class JavaCodeContextType extends TemplateContextType {
return false;
}
return JavaCompletionData.CLASS_START.isAcceptable(element, element) || JavaCompletionData.isInsideParameterList(element);
return JavaCompletionData.isSuitableForClass(element) || JavaCompletionData.isInsideParameterList(element);
}
}
@@ -0,0 +1,5 @@
public class Util {
void foo() {
c<caret>
}
}
@@ -102,6 +102,7 @@ public class KeywordCompletionTest extends LightCompletionTestCase {
public void testCharInAnnotatedParameter() throws Exception { doTest(1, "char"); }
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 testTryInExpression() throws Exception {
configureByFile(BASE_PATH + "/" + getTestName(true) + ".java");