From ee63f62c499dbe5c429df8051c9df3b651c063fb Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 12 Sep 2012 23:08:03 +0200 Subject: [PATCH] IDEA-91465 inappropriate autocompletion of 'def' to NoClassDefFoundError after block labels in Spock specifications --- .../lang/completion/GroovyCompletionData.java | 3 +- .../lang/completion/GroovyCompletionUtil.java | 13 +++++++-- .../completion/KeywordCompletionTest.groovy | 1 + .../oldCompletion/keyword/afterLabel.test | 28 +++++++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 plugins/groovy/testdata/groovy/oldCompletion/keyword/afterLabel.test 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 156fc062b897..b394e60c234d 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 @@ -405,7 +405,7 @@ public class GroovyCompletionData { } if (GroovyCompletionUtil.isNewStatement(context, true)) { final PsiElement leaf = GroovyCompletionUtil.getLeafByOffset(offset - 1, context); - if (leaf != null && leaf.getParent() instanceof GrStatementOwner) { + if (leaf != null && (leaf.getParent() instanceof GrStatementOwner || leaf.getParent() instanceof GrLabeledStatement)) { return true; } } @@ -429,6 +429,7 @@ public class GroovyCompletionData { } if (superParent instanceof GrStatementOwner || + superParent instanceof GrLabeledStatement || superParent instanceof GrIfStatement || superParent instanceof GrForStatement || superParent instanceof GrWhileStatement) { diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionUtil.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionUtil.java index ac098049f949..7c5a11531221 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionUtil.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionUtil.java @@ -53,6 +53,7 @@ import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement; import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement; import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult; import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration; import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock; @@ -140,8 +141,13 @@ public class GroovyCompletionUtil { public static boolean isNewStatement(PsiElement element, boolean canBeAfterBrace) { PsiElement previousLeaf = getLeafByOffset(element.getTextRange().getStartOffset() - 1, element); previousLeaf = PsiImplUtil.realPrevious(previousLeaf); - if (previousLeaf != null && canBeAfterBrace && mLCURLY.equals(previousLeaf.getNode().getElementType())) { - return true; + if (previousLeaf != null) { + if (canBeAfterBrace && mLCURLY.equals(previousLeaf.getNode().getElementType())) { + return true; + } + if (mCOLON.equals(previousLeaf.getNode().getElementType()) && previousLeaf.getParent() instanceof GrLabeledStatement) { + return true; + } } return (previousLeaf == null || SEPARATORS.contains(previousLeaf.getNode().getElementType())); } @@ -206,7 +212,8 @@ public class GroovyCompletionUtil { if (parent instanceof GrApplicationStatement) { parent = parent.getParent(); } - if ((parent instanceof GrCodeBlock || parent instanceof GrCaseSection) && isNewStatement(context, true)) { + if ((parent instanceof GrCodeBlock || parent instanceof GrCaseSection || parent instanceof GrLabeledStatement) && + isNewStatement(context, true)) { return true; } } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/KeywordCompletionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/KeywordCompletionTest.groovy index 9ee11dcdbb7b..6e6593706a54 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/KeywordCompletionTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/KeywordCompletionTest.groovy @@ -93,6 +93,7 @@ public class KeywordCompletionTest extends CompletionTestBase { void testAssert() { doTest() } void testReturn() { doTest() } void testAssertInClosure() { doTest() } + void testAfterLabel() { doTest() } String basePath = TestUtils.testDataPath + 'groovy/oldCompletion/keyword' diff --git a/plugins/groovy/testdata/groovy/oldCompletion/keyword/afterLabel.test b/plugins/groovy/testdata/groovy/oldCompletion/keyword/afterLabel.test new file mode 100644 index 000000000000..13aa418c86c7 --- /dev/null +++ b/plugins/groovy/testdata/groovy/oldCompletion/keyword/afterLabel.test @@ -0,0 +1,28 @@ +foo: +----- +as +assert +boolean +byte +char +def +double +false +final +float +for +int +long +new +null +return +short +super +switch +synchronized +this +throw +true +try +void +while \ No newline at end of file