From 24fa82bf758df35ae67a3b5665207d1748db2fb4 Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Wed, 17 Jan 2018 01:43:51 +0300 Subject: [PATCH] [groovy] make ClassProcessor extend FindFirstProcessor --- .../groovy/lang/resolve/AnnotationHint.java | 4 +- .../lang/resolve/processors/ClassHint.java | 7 +++- .../lang/resolve/processors/ClassProcessor.kt | 37 ++++++++----------- .../resolve/processors/GroovyResolveKind.java | 3 +- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/AnnotationHint.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/AnnotationHint.java index c989fb59d15b..ca8a7ebdb52a 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/AnnotationHint.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/AnnotationHint.java @@ -1,4 +1,4 @@ -// Copyright 2000-2017 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-2018 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.lang.resolve; import com.intellij.openapi.util.Key; @@ -7,6 +7,8 @@ public interface AnnotationHint { Key HINT_KEY = Key.create("groovy.annotation.resolve"); + AnnotationHint ANNOTATION_RESOLVE = () -> true; + /** * @return {@code true} if this processor expects only classes that may be annotations or * {@code false} if this processor expects all other declarations except classes that may be annotations diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/ClassHint.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/ClassHint.java index 7af639d37505..8a6d1b53a243 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/ClassHint.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/ClassHint.java @@ -1,9 +1,9 @@ -// Copyright 2000-2017 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-2018 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.lang.resolve.processors; import com.intellij.openapi.util.Key; import com.intellij.psi.PsiElement; +import com.intellij.psi.scope.ElementClassHint; import com.intellij.psi.scope.ElementClassHint.DeclarationKind; import java.util.EnumSet; @@ -15,6 +15,9 @@ import static com.intellij.psi.scope.ElementClassHint.DeclarationKind.*; */ public interface ClassHint { Key RESOLVE_CONTEXT = Key.create("RESOLVE_CONTEXT"); + + ElementClassHint CLASSES = kind -> kind == CLASS; + EnumSet RESOLVE_KINDS_CLASS = EnumSet.of(CLASS); EnumSet RESOLVE_KINDS_METHOD = EnumSet.of(METHOD); EnumSet RESOLVE_KINDS_PROPERTY = EnumSet.of(VARIABLE, FIELD, ENUM_CONST); diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/ClassProcessor.kt b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/ClassProcessor.kt index 5fd7dab7894f..f6fef845ea19 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/ClassProcessor.kt +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/ClassProcessor.kt @@ -1,43 +1,36 @@ -// Copyright 2000-2017 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-2018 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.lang.resolve.processors -import com.intellij.openapi.util.Key import com.intellij.psi.* -import com.intellij.util.SmartList +import com.intellij.psi.scope.ElementClassHint import org.jetbrains.plugins.groovy.lang.resolve.AnnotationHint import org.jetbrains.plugins.groovy.lang.resolve.ClassResolveResult -import org.jetbrains.plugins.groovy.lang.resolve.GrResolverProcessor -import org.jetbrains.plugins.groovy.lang.resolve.processors.ClassHint.RESOLVE_KINDS_CLASS class ClassProcessor( name: String, private val place: PsiElement?, private val typeArguments: Array = PsiType.EMPTY_ARRAY, - private val annotationResolve: Boolean = false -) : GrScopeProcessorWithHints(name, RESOLVE_KINDS_CLASS), AnnotationHint, GrResolverProcessor { + annotationResolve: Boolean = false +) : FindFirstProcessor(name) { - private val myResults = SmartList() - override val results: List get() = myResults + init { + hint(ElementClassHint.KEY, ClassHint.CLASSES) + if (annotationResolve) { + hint(AnnotationHint.HINT_KEY, AnnotationHint.ANNOTATION_RESOLVE) + } + } - override fun execute(element: PsiElement, state: ResolveState): Boolean { - val clazz = element as? PsiClass ?: return true - if (clazz is PsiTypeParameter) return true + override fun result(element: PsiElement, state: ResolveState): ClassResolveResult? { + val clazz = element as? PsiClass ?: return null + if (clazz is PsiTypeParameter) return null val substitutor = state.get(PsiSubstitutor.KEY) ?: PsiSubstitutor.EMPTY - myResults += ClassResolveResult( + return ClassResolveResult( element = clazz, place = place, resolveContext = state.get(ClassHint.RESOLVE_CONTEXT), substitutor = substitutor.putAll(clazz, typeArguments) ) - return false } - - @Suppress("UNCHECKED_CAST") - override fun getHint(hintKey: Key): T? { - if (hintKey == AnnotationHint.HINT_KEY && annotationResolve) return this as T - return super.getHint(hintKey) - } - - override fun isAnnotationResolve(): Boolean = annotationResolve } + diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/GroovyResolveKind.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/GroovyResolveKind.java index a36d54e3bb00..d97e40ff34bf 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/GroovyResolveKind.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/GroovyResolveKind.java @@ -1,4 +1,4 @@ -// Copyright 2000-2017 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-2018 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.lang.resolve.processors; import com.intellij.openapi.util.Key; @@ -32,4 +32,5 @@ public enum GroovyResolveKind { } public static final Key HINT_KEY = Key.create("groovy.resolve.kind.hint"); + public static final Hint CLASSES = kind -> kind == CLASS; }