From ce302a3b75457462f58e013de038e21df00e22e9 Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Fri, 3 Nov 2017 15:43:29 -0500 Subject: [PATCH] Fix for issue #360: proposals for closure delegate are instance based --- .../tests/FieldCompletionTests.groovy | 50 +++++++++++++++++-- ...ementAndExpressionCompletionProcessor.java | 11 +++- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/ide-test/org.codehaus.groovy.eclipse.codeassist.completion.test/src/org/codehaus/groovy/eclipse/codeassist/tests/FieldCompletionTests.groovy b/ide-test/org.codehaus.groovy.eclipse.codeassist.completion.test/src/org/codehaus/groovy/eclipse/codeassist/tests/FieldCompletionTests.groovy index f7f31a0275..3bb6873365 100644 --- a/ide-test/org.codehaus.groovy.eclipse.codeassist.completion.test/src/org/codehaus/groovy/eclipse/codeassist/tests/FieldCompletionTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.codeassist.completion.test/src/org/codehaus/groovy/eclipse/codeassist/tests/FieldCompletionTests.groovy @@ -292,8 +292,9 @@ final class FieldCompletionTests extends CompletionTestSuite { } class Sub extends Super { def meth() { - (0..10).each { - xx + (0..10).each { + xx + } } } '''.stripIndent() @@ -310,8 +311,9 @@ final class FieldCompletionTests extends CompletionTestSuite { } class Sub extends Super { def meth() { - (0..10).each { - xx + (0..10).each { + xx + } } } '''.stripIndent() @@ -320,6 +322,46 @@ final class FieldCompletionTests extends CompletionTestSuite { proposalExists(proposals, 'xxx', 1) } + @Test // https://github.com/groovy/groovy-eclipse/issues/360 + void testClosure8() { + String contents = '''\ + class A { + def xxx + } + class B { + def xyz + void meth(A a) { + a.with { + x + } + } + } + '''.stripIndent() + ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'x')) + proposalExists(proposals, 'xxx', 1) // from the delegate + proposalExists(proposals, 'xyz', 1) // from the owner + } + + @Test // https://github.com/groovy/groovy-eclipse/issues/360 + void testClosure9() { + String contents = '''\ + class A { + def xxx + } + class B { + def xyz + static void meth(A a) { + a.with { + x + } + } + } + '''.stripIndent() + ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'x')) + proposalExists(proposals, 'xxx', 1) // from the delegate + proposalExists(proposals, 'xyz', 0) // *not* from owner + } + @Test void testEnumReceiver1() { addJavaSource('enum E { CONST; public static final String VALUE = ""; }', 'E') diff --git a/ide/org.codehaus.groovy.eclipse.codeassist.completion/src/org/codehaus/groovy/eclipse/codeassist/processors/StatementAndExpressionCompletionProcessor.java b/ide/org.codehaus.groovy.eclipse.codeassist.completion/src/org/codehaus/groovy/eclipse/codeassist/processors/StatementAndExpressionCompletionProcessor.java index 84eabd6173..b81ba276d3 100644 --- a/ide/org.codehaus.groovy.eclipse.codeassist.completion/src/org/codehaus/groovy/eclipse/codeassist/processors/StatementAndExpressionCompletionProcessor.java +++ b/ide/org.codehaus.groovy.eclipse.codeassist.completion/src/org/codehaus/groovy/eclipse/codeassist/processors/StatementAndExpressionCompletionProcessor.java @@ -360,8 +360,15 @@ public List generateProposals(IProgressMonitor monitor) { isStatic = isStatic() || requestor.isStatic; completionType = getCompletionType(requestor); - IProposalCreator[] creators = chooseProposalCreators(isStatic); - proposalCreatorLoop(context, requestor, completionType, isStatic, groovyProposals, creators, false); + boolean isStatic1 = isStatic; + if (context.location == ContentAssistLocation.STATEMENT) { + ClassNode closureThis = requestor.currentScope.getThis(); + if (closureThis != null && !closureThis.equals(completionType)) { + isStatic1 = false; // completionType refers to delegate, which is an instance + } + } + IProposalCreator[] creators = chooseProposalCreators(isStatic1); + proposalCreatorLoop(context, requestor, completionType, isStatic1, groovyProposals, creators, false); if (completionType.equals(VariableScope.CLASS_CLASS_NODE) && !completionType.getGenericsTypes()[0].getType().equals(VariableScope.CLASS_CLASS_NODE) &&