From 675459121af55d6acbcac0fe5b503e089ebefb48 Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Wed, 24 Mar 2021 18:47:22 +0100 Subject: [PATCH] IDEA-264593 [groovy] gdsl: flatten custom member holders GitOrigin-RevId: 14a5f9f819b73085baf6e17712684a00ecdb829a --- .../groovy/dsl/CustomMembersGenerator.java | 32 +++++------------ .../groovy/dsl/CustomMembersHolderImpl.java | 34 +++++++++++++++++++ .../groovy/dsl/GroovyDslExecutor.groovy | 9 +++-- .../dsl/holders/CompoundMembersHolder.java | 15 ++++---- .../dsl/holders/CustomMembersHolder.java | 18 +++++++++- 5 files changed, 72 insertions(+), 36 deletions(-) create mode 100644 plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/CustomMembersHolderImpl.java diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/CustomMembersGenerator.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/CustomMembersGenerator.java index a13089e22ee4..8a73d04a11ce 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/CustomMembersGenerator.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/CustomMembersGenerator.java @@ -3,9 +3,11 @@ package org.jetbrains.plugins.groovy.dsl; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; -import com.intellij.psi.*; +import com.intellij.psi.CommonClassNames; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiType; import com.intellij.psi.impl.FakePsiElement; -import com.intellij.psi.scope.PsiScopeProcessor; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.FList; @@ -18,14 +20,11 @@ import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.dsl.dsltop.GdslMembersProvider; -import org.jetbrains.plugins.groovy.dsl.holders.CompoundMembersHolder; import org.jetbrains.plugins.groovy.dsl.holders.CustomMembersHolder; import org.jetbrains.plugins.groovy.dsl.holders.DeclarationType; -import org.jetbrains.plugins.groovy.dsl.holders.NonCodeMembersHolder; import org.jetbrains.plugins.groovy.dsl.toplevel.ClassContextFilter; import org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor; import org.jetbrains.plugins.groovy.extensions.impl.NamedArgumentDescriptorImpl; -import org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureDescriptor; import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; @@ -34,7 +33,6 @@ import org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils; import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil; import java.util.*; -import java.util.function.Consumer; /** * @author peter @@ -44,7 +42,7 @@ public class CustomMembersGenerator extends GroovyObjectSupport implements GdslM private static final GdslMembersProvider[] PROVIDERS = GdslMembersProvider.EP_NAME.getExtensions(); public static final @NonNls String THROWS = "throws"; private FList myDeclarations = FList.emptyList(); - private final CompoundMembersHolder myDepot = new CompoundMembersHolder(); + private final List myMemberHolders = new ArrayList<>(); private final GroovyClassDescriptor myDescriptor; @Nullable private final Map myBindings; @@ -86,28 +84,16 @@ public class CustomMembersGenerator extends GroovyObjectSupport implements GdslM } @Nullable - public CustomMembersHolder getMembersHolder() { + public List getMembersHolder() { if (!myDeclarations.isEmpty()) { - addMemberHolder(new CustomMembersHolder() { - @Override - public boolean processMembers(GroovyClassDescriptor descriptor, PsiScopeProcessor processor, ResolveState state) { - return NonCodeMembersHolder.generateMembers(ContainerUtil.reverse(myDeclarations), descriptor.justGetPlaceFile()).processMembers( - descriptor, processor, state); - } - - @Override - public void consumeClosureDescriptors(GroovyClassDescriptor descriptor, Consumer consumer) { - NonCodeMembersHolder.generateMembers(ContainerUtil.reverse(myDeclarations), descriptor.justGetPlaceFile()) - .consumeClosureDescriptors(descriptor, consumer); - } - }); + addMemberHolder(new CustomMembersHolderImpl(myDeclarations)); } - return myDepot; + return myMemberHolders; } @Override public void addMemberHolder(CustomMembersHolder holder) { - myDepot.addHolder(holder); + myMemberHolders.add(holder); } private Object[] constructNewArgs(Object[] args) { diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/CustomMembersHolderImpl.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/CustomMembersHolderImpl.java new file mode 100644 index 000000000000..7e7b99b005d7 --- /dev/null +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/CustomMembersHolderImpl.java @@ -0,0 +1,34 @@ +// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.plugins.groovy.dsl; + +import com.intellij.psi.ResolveState; +import com.intellij.psi.scope.PsiScopeProcessor; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.containers.FList; +import org.jetbrains.plugins.groovy.dsl.holders.CustomMembersHolder; +import org.jetbrains.plugins.groovy.dsl.holders.NonCodeMembersHolder; +import org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureDescriptor; + +import java.util.Map; +import java.util.function.Consumer; + +final class CustomMembersHolderImpl implements CustomMembersHolder { + + private final FList myDeclarations; + + CustomMembersHolderImpl(FList declarations) { + myDeclarations = declarations; + } + + @Override + public boolean processMembers(GroovyClassDescriptor descriptor, PsiScopeProcessor processor, ResolveState state) { + return NonCodeMembersHolder.generateMembers(ContainerUtil.reverse(myDeclarations), descriptor.justGetPlaceFile()) + .processMembers(descriptor, processor, state); + } + + @Override + public void consumeClosureDescriptors(GroovyClassDescriptor descriptor, Consumer consumer) { + NonCodeMembersHolder.generateMembers(ContainerUtil.reverse(myDeclarations), descriptor.justGetPlaceFile()) + .consumeClosureDescriptors(descriptor, consumer); + } +} diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/GroovyDslExecutor.groovy b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/GroovyDslExecutor.groovy index 493812772c67..50e0a846100d 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/GroovyDslExecutor.groovy +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/GroovyDslExecutor.groovy @@ -9,7 +9,6 @@ import com.intellij.util.containers.MultiMap import groovy.transform.CompileStatic import groovy.transform.TypeCheckingMode import org.codehaus.groovy.control.CompilerConfiguration -import org.jetbrains.plugins.groovy.dsl.holders.CompoundMembersHolder import org.jetbrains.plugins.groovy.dsl.holders.CustomMembersHolder import org.jetbrains.plugins.groovy.dsl.psi.PsiEnhancerCategory import org.jetbrains.plugins.groovy.dsl.toplevel.ContextFilter @@ -37,19 +36,19 @@ class GroovyDslExecutor { } CustomMembersHolder processVariants(GroovyClassDescriptor descriptor, ProcessingContext ctx) { - if (!enhancers) return CompoundMembersHolder.EMPTY + if (!enhancers) return CustomMembersHolder.EMPTY - CompoundMembersHolder holder = new CompoundMembersHolder() + List holders = new ArrayList<>() for (pair in enhancers) { ProgressManager.checkCanceled() ctx.put(DslPointcut.BOUND, null) if (pair.first.isApplicable(descriptor, ctx)) { def generator = new CustomMembersGenerator(descriptor, ctx.get(DslPointcut.BOUND)) doRun(generator, pair.second) - holder.addHolder(generator.membersHolder) + holders.addAll(generator.membersHolder) } } - return holder + return CustomMembersHolder.create(holders) } @CompileStatic(TypeCheckingMode.SKIP) diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/holders/CompoundMembersHolder.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/holders/CompoundMembersHolder.java index 92f5d568afaf..873628b32c55 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/holders/CompoundMembersHolder.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/holders/CompoundMembersHolder.java @@ -1,8 +1,9 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.plugins.groovy.dsl.holders; import com.intellij.psi.ResolveState; import com.intellij.psi.scope.PsiScopeProcessor; +import org.jetbrains.annotations.NotNull; import org.jetbrains.plugins.groovy.dsl.GroovyClassDescriptor; import org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureDescriptor; @@ -13,9 +14,13 @@ import java.util.function.Consumer; /** * @author ilyas */ -public class CompoundMembersHolder implements CustomMembersHolder { +class CompoundMembersHolder implements CustomMembersHolder { - private final List myHolders = new ArrayList<>(); + private final List myHolders; + + CompoundMembersHolder(@NotNull List holders) { + myHolders = new ArrayList<>(holders); + } @Override public boolean processMembers(GroovyClassDescriptor descriptor, PsiScopeProcessor processor, ResolveState state) { @@ -31,8 +36,4 @@ public class CompoundMembersHolder implements CustomMembersHolder { holder.consumeClosureDescriptors(descriptor, consumer); } } - - public synchronized void addHolder(CustomMembersHolder holder) { - myHolders.add(holder); - } } diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/holders/CustomMembersHolder.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/holders/CustomMembersHolder.java index eb191a4c73eb..186401a025ba 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/holders/CustomMembersHolder.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/holders/CustomMembersHolder.java @@ -1,11 +1,14 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.plugins.groovy.dsl.holders; import com.intellij.psi.ResolveState; import com.intellij.psi.scope.PsiScopeProcessor; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; import org.jetbrains.plugins.groovy.dsl.GroovyClassDescriptor; import org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureDescriptor; +import java.util.List; import java.util.function.Consumer; /** @@ -27,4 +30,17 @@ public interface CustomMembersHolder { boolean processMembers(GroovyClassDescriptor descriptor, PsiScopeProcessor processor, ResolveState state); void consumeClosureDescriptors(GroovyClassDescriptor descriptor, Consumer consumer); + + static @NotNull CustomMembersHolder create(@NotNull List holders) { + List nonEmptyHolders = ContainerUtil.filter(holders, it -> it != EMPTY); + if (nonEmptyHolders.isEmpty()) { + return EMPTY; + } + else if (nonEmptyHolders.size() == 1) { + return nonEmptyHolders.get(0); + } + else { + return new CompoundMembersHolder(nonEmptyHolders); + } + } }