[groovy] make ClassProcessor extend FindFirstProcessor

This commit is contained in:
Daniil Ovchinnikov
2018-01-18 19:54:19 +03:00
parent 5ea28d4a9a
commit 24fa82bf75
4 changed files with 25 additions and 26 deletions
@@ -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<AnnotationHint> 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
@@ -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<PsiElement> RESOLVE_CONTEXT = Key.create("RESOLVE_CONTEXT");
ElementClassHint CLASSES = kind -> kind == CLASS;
EnumSet<DeclarationKind> RESOLVE_KINDS_CLASS = EnumSet.of(CLASS);
EnumSet<DeclarationKind> RESOLVE_KINDS_METHOD = EnumSet.of(METHOD);
EnumSet<DeclarationKind> RESOLVE_KINDS_PROPERTY = EnumSet.of(VARIABLE, FIELD, ENUM_CONST);
@@ -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<out PsiType> = PsiType.EMPTY_ARRAY,
private val annotationResolve: Boolean = false
) : GrScopeProcessorWithHints(name, RESOLVE_KINDS_CLASS), AnnotationHint, GrResolverProcessor<ClassResolveResult> {
annotationResolve: Boolean = false
) : FindFirstProcessor<ClassResolveResult>(name) {
private val myResults = SmartList<ClassResolveResult>()
override val results: List<ClassResolveResult> 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 <T : Any?> getHint(hintKey: Key<T>): T? {
if (hintKey == AnnotationHint.HINT_KEY && annotationResolve) return this as T
return super<GrScopeProcessorWithHints>.getHint(hintKey)
}
override fun isAnnotationResolve(): Boolean = annotationResolve
}
@@ -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> HINT_KEY = Key.create("groovy.resolve.kind.hint");
public static final Hint CLASSES = kind -> kind == CLASS;
}