From a229fe44e50501018726798a2932b03b1d25525a Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Wed, 19 Oct 2016 20:03:34 +0300 Subject: [PATCH] [groovy] @DelegatesTo provider API --- .../DefaultDelegatesToProvider.java | 4 +-- .../delegatesTo/GrDelegatesToProvider.java | 29 +++++++++++++++++++ .../resolve/delegatesTo/grDelegatesToUtil.kt | 12 +++++++- plugins/groovy/src/META-INF/plugin.xml | 2 ++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/GrDelegatesToProvider.java diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/DefaultDelegatesToProvider.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/DefaultDelegatesToProvider.java index 02cca1938dd8..10609aa980af 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/DefaultDelegatesToProvider.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/DefaultDelegatesToProvider.java @@ -50,10 +50,10 @@ import static org.jetbrains.plugins.groovy.lang.resolve.delegatesTo.GrDelegatesT /** * @author Max Medvedev */ -public class DefaultDelegatesToProvider { +public class DefaultDelegatesToProvider implements GrDelegatesToProvider { @Nullable - public static DelegatesToInfo getDelegatesToInfo(@NotNull final GrClosableBlock closableBlock) { + public DelegatesToInfo getDelegatesToInfo(@NotNull final GrClosableBlock closableBlock) { GrCall call = getContainingCall(closableBlock); if (call == null) return null; diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/GrDelegatesToProvider.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/GrDelegatesToProvider.java new file mode 100644 index 000000000000..09e428e32d88 --- /dev/null +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/GrDelegatesToProvider.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.lang.resolve.delegatesTo; + +import com.intellij.openapi.extensions.ExtensionPointName; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock; + +public interface GrDelegatesToProvider { + + ExtensionPointName EP_NAME = ExtensionPointName.create("org.intellij.groovy.delegatesToProvider"); + + @Nullable + DelegatesToInfo getDelegatesToInfo(@NotNull GrClosableBlock closure); +} diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/grDelegatesToUtil.kt b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/grDelegatesToUtil.kt index 92610a792350..3c825ba681b9 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/grDelegatesToUtil.kt +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/delegatesTo/grDelegatesToUtil.kt @@ -16,9 +16,19 @@ package org.jetbrains.plugins.groovy.lang.resolve.delegatesTo import com.intellij.openapi.util.Key +import com.intellij.psi.util.CachedValueProvider.Result +import com.intellij.psi.util.CachedValuesManager +import com.intellij.psi.util.PsiModificationTracker import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock @JvmField val DELEGATES_TO_KEY = Key.create("groovy.closure.delegatesTo.type") @JvmField val DELEGATES_TO_STRATEGY_KEY = Key.create("groovy.closure.delegatesTo.strategy") -fun getDelegatesToInfo(closure: GrClosableBlock): DelegatesToInfo? = DefaultDelegatesToProvider.getDelegatesToInfo(closure) +fun getDelegatesToInfo(closure: GrClosableBlock): DelegatesToInfo? = CachedValuesManager.getCachedValue(closure) { + Result.create(doGetDelegatesToInfo(closure), PsiModificationTracker.MODIFICATION_COUNT) +} + +private fun doGetDelegatesToInfo(closure: GrClosableBlock): DelegatesToInfo? { + return GrDelegatesToProvider.EP_NAME.extensions.asSequence().mapNotNull { it.getDelegatesToInfo(closure) }.firstOrNull() +} + diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index 2ecc3d1828d4..c1b67523bd7d 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -85,6 +85,7 @@ + @@ -248,6 +249,7 @@ +