From c579d1f9fc6a4cfe29f21c86d0f08e06dfb88056 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 23 Dec 2011 18:28:01 +0100 Subject: [PATCH] IDEA-79253 'class' keyword isn't suggested inside Groovy class --- .../lang/completion/GroovyCompletionData.java | 57 +++++++++++-------- .../completion/GroovyCompletionTest.groovy | 4 ++ .../groovy/oldCompletion/keyword/var13.test | 5 +- .../groovy/oldCompletion/keyword/var6.test | 3 + 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionData.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionData.java index d861ebbaed2c..17ab4c1b8616 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionData.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionData.java @@ -29,6 +29,7 @@ import com.intellij.patterns.PlatformPatterns; import com.intellij.psi.*; import com.intellij.psi.templateLanguages.OuterLanguageElement; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.ArrayUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.plugins.groovy.lang.groovydoc.lexer.GroovyDocTokenTypes; import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocInlinedTag; @@ -83,7 +84,10 @@ public class GroovyCompletionData { } addTypeDefinitionKeywords(result, position); - addExtendsImplements(position, result); + for (String keyword : addExtendsImplements(position)) { + result.addElement(keyword(keyword, TailType.SPACE)); + } + registerControlCompletion(position, result); if (parent instanceof GrExpression) { @@ -144,9 +148,10 @@ public class GroovyCompletionData { } } - private static void addExtendsImplements(PsiElement context, CompletionResultSet result) { + @NotNull + private static String[] addExtendsImplements(PsiElement context) { if (context.getParent() == null) { - return; + return ArrayUtil.EMPTY_STRING_ARRAY; } PsiElement elem = context.getParent(); @@ -162,21 +167,20 @@ public class GroovyCompletionData { ext &= elem instanceof GrInterfaceDefinition || elem instanceof GrClassDefinition; impl &= elem instanceof GrEnumTypeDefinition || elem instanceof GrClassDefinition; - if (!ext && !impl) return; + if (!ext && !impl) return ArrayUtil.EMPTY_STRING_ARRAY; PsiElement[] children = elem.getChildren(); for (PsiElement child : children) { ext &= !(child instanceof GrExtendsClause); if (child instanceof GrImplementsClause || child instanceof GrTypeDefinitionBody) { - return; + return ArrayUtil.EMPTY_STRING_ARRAY; } } - if (ext) { - result.addElement(keyword(PsiKeyword.EXTENDS, TailType.SPACE)); - } - if (impl) { - result.addElement(keyword(PsiKeyword.IMPLEMENTS, TailType.SPACE)); + if (ext && impl) { + return new String[]{PsiKeyword.EXTENDS, PsiKeyword.IMPLEMENTS}; } + + return new String[]{ext ? PsiKeyword.EXTENDS : PsiKeyword.IMPLEMENTS}; } public static void addKeywords(CompletionResultSet result, boolean space, String... keywords) { @@ -268,17 +272,24 @@ public class GroovyCompletionData { } public static boolean suggestClassInterfaceEnum(PsiElement context) { - if (context.getParent() != null && - (context.getParent() instanceof GrReferenceExpression) && - context.getParent().getParent() instanceof GroovyFile) { + if (suggestThrows(context) || addExtendsImplements(context).length > 0) { + return false; + } + + PsiElement parent = context.getParent(); + if (parent instanceof GrTypeDefinitionBody) { return true; } - if (context.getParent() != null && - (context.getParent() instanceof GrReferenceExpression) && - (context.getParent().getParent() instanceof GrApplicationStatement || - context.getParent().getParent() instanceof GrCall) && - context.getParent().getParent().getParent() instanceof GroovyFile) { - return true; + + if (parent instanceof GrReferenceExpression) { + if (parent.getParent() instanceof GroovyFile) { + return true; + } + if ((parent.getParent() instanceof GrApplicationStatement || + parent.getParent() instanceof GrCall) && + parent.getParent().getParent() instanceof GroovyFile) { + return true; + } } final PsiElement leaf = GroovyCompletionUtil.getLeafByOffset(context.getTextRange().getStartOffset() - 1, context); if (leaf != null) { @@ -286,11 +297,11 @@ public class GroovyCompletionData { prev = PsiImplUtil.realPrevious(prev); if (prev instanceof GrModifierList && prev.getParent() != null && - prev.getParent().getParent() instanceof GroovyFile) + prev.getParent().getParent() instanceof GroovyFile) { return true; + } - PsiElement parent = leaf.getParent(); - if (parent instanceof GroovyFile) { + if (leaf.getParent() instanceof GroovyFile) { return GroovyCompletionUtil.isNewStatement(context, false); } } @@ -426,7 +437,7 @@ public class GroovyCompletionData { return false; } - public static boolean suggestThrows(PsiElement context) { + private static boolean suggestThrows(PsiElement context) { PsiElement candidate = null; if (GroovyCompletionUtil.isInTypeDefinitionBody(context)) { PsiElement run = context; diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy index 9926fe4d5353..23b18f2a4c25 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy @@ -1058,6 +1058,10 @@ class X { checkSingleItemCompletion 'new ArrayLi', 'new ArrayList<>()' } + public void testInnerClassStart() { + checkSingleItemCompletion 'class Foo { cl }', 'class Foo { class }' + } + public void testPropertyBeforeAccessor() { doVariantableTest 'soSe', 'setSoSe' } diff --git a/plugins/groovy/testdata/groovy/oldCompletion/keyword/var13.test b/plugins/groovy/testdata/groovy/oldCompletion/keyword/var13.test index 4dee412356b2..72f1e3bece98 100644 --- a/plugins/groovy/testdata/groovy/oldCompletion/keyword/var13.test +++ b/plugins/groovy/testdata/groovy/oldCompletion/keyword/var13.test @@ -15,11 +15,14 @@ abstract boolean byte char +class def double +enum final float int +interface long native private @@ -31,4 +34,4 @@ strictfp synchronized transient void -volatile +volatile \ No newline at end of file diff --git a/plugins/groovy/testdata/groovy/oldCompletion/keyword/var6.test b/plugins/groovy/testdata/groovy/oldCompletion/keyword/var6.test index cb3adddefd23..923580443c31 100644 --- a/plugins/groovy/testdata/groovy/oldCompletion/keyword/var6.test +++ b/plugins/groovy/testdata/groovy/oldCompletion/keyword/var6.test @@ -14,11 +14,14 @@ abstract boolean byte char +class def double +enum final float int +interface long native private