diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/FindFirstProcessor.kt b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/FindFirstProcessor.kt new file mode 100644 index 000000000000..2fd5a85e3d5a --- /dev/null +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/FindFirstProcessor.kt @@ -0,0 +1,30 @@ +/* + * 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. + */ +package org.jetbrains.plugins.groovy.lang.resolve.processors + +import com.intellij.psi.PsiElement +import com.intellij.psi.ResolveState +import com.intellij.psi.scope.NameHint +import com.intellij.psi.scope.ProcessorWithHints +import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult +import org.jetbrains.plugins.groovy.lang.resolve.GrResolverProcessor + +abstract class FindFirstProcessor(name: String) : ProcessorWithHints(), GrResolverProcessor { + + init { + hint(NameHint.KEY, NameHint { name }) + } + + private var result: GroovyResolveResult? = null + + final override val results: List get() = result?.let { listOf(it) } ?: emptyList() + + final override fun execute(element: PsiElement, state: ResolveState): Boolean { + assert(result == null) + result = result(element, state) + return result == null + } + + abstract fun result(element: PsiElement, state: ResolveState): GroovyResolveResult? +} diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/LocalVariableProcessor.kt b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/LocalVariableProcessor.kt index 3adc6f6cefd7..ed18ed895613 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/LocalVariableProcessor.kt +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/processors/LocalVariableProcessor.kt @@ -1,34 +1,27 @@ -// 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-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. + */ package org.jetbrains.plugins.groovy.lang.resolve.processors import com.intellij.psi.PsiElement import com.intellij.psi.ResolveState import com.intellij.psi.scope.ElementClassHint -import com.intellij.psi.scope.NameHint -import com.intellij.psi.scope.ProcessorWithHints import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable import org.jetbrains.plugins.groovy.lang.resolve.ElementGroovyResult -import org.jetbrains.plugins.groovy.lang.resolve.GrResolverProcessor -class LocalVariableProcessor(name: String) : ProcessorWithHints(), GrResolverProcessor { +class LocalVariableProcessor(name: String) : FindFirstProcessor(name) { init { - hint(NameHint.KEY, NameHint { name }) hint(ElementClassHint.KEY, ElementClassHint { false }) - hint(GroovyResolveKind.HINT_KEY, GroovyResolveKind.Hint { it === GroovyResolveKind.VARIABLE }) + hint(GroovyResolveKind.HINT_KEY, GroovyResolveKind.Hint { it == GroovyResolveKind.VARIABLE }) } - private var resolveResult: GroovyResolveResult? = null - - override val results: List get() = resolveResult?.let { listOf(it) } ?: emptyList() - - override fun execute(element: PsiElement, state: ResolveState): Boolean { - if (element !is GrVariable || element is GrField) return true + override fun result(element: PsiElement, state: ResolveState): GroovyResolveResult? { + if (element !is GrVariable || element is GrField) return null assert(element !is GrBindingVariable) - resolveResult = ElementGroovyResult(element) - return false + return ElementGroovyResult(element) } }