Support find usages for declarations with multiple overridden Java methods (e.g. J -> J -> K)

This commit is contained in:
Alexey Sedunov
2014-02-10 17:41:32 +04:00
parent 0e451bd35e
commit 40eaf49a8c
3 changed files with 95 additions and 17 deletions
@@ -34,6 +34,7 @@ import org.jetbrains.jet.lang.psi.JetClassOrObject
import com.intellij.find.findUsages.FindUsagesManager
import com.intellij.find.findUsages.JavaFindUsagesHandlerFactory
import com.intellij.psi.PsiMethod
import org.jetbrains.jet.plugin.findUsages.handlers.DelegatingFindMemberUsagesHandler
public class KotlinFindUsagesHandlerFactory(project: Project) : FindUsagesHandlerFactory() {
val javaHandlerFactory = JavaFindUsagesHandlerFactory(project)
@@ -58,23 +59,9 @@ public class KotlinFindUsagesHandlerFactory(project: Project) : FindUsagesHandle
is JetNamedFunction, is JetProperty, is JetParameter -> {
val declaration = element as JetNamedDeclaration
return if (forHighlightUsages) KotlinFindMemberUsagesHandler.getInstance(declaration, this)
else JetRefactoringUtil.checkSuperMethods(declaration, null, "super.methods.action.key.find.usages")?.let { callables ->
when (callables.size()) {
0 -> FindUsagesHandler.NULL_HANDLER
1 -> {
val callable = callables.get(0)
when (callable) {
is JetNamedDeclaration ->
KotlinFindMemberUsagesHandler.getInstance(callable, this)
is PsiMethod ->
javaHandlerFactory.createFindUsagesHandler(callable, forHighlightUsages)
else -> null
}
}
else ->
KotlinFindMemberUsagesHandler.getInstance(declaration, callables, this)
}
if (forHighlightUsages) return KotlinFindMemberUsagesHandler.getInstance(declaration, this)
return JetRefactoringUtil.checkSuperMethods(declaration, null, "super.methods.action.key.find.usages")?.let { callables ->
if (callables.empty) FindUsagesHandler.NULL_HANDLER else DelegatingFindMemberUsagesHandler(declaration, callables, this)
}
}
@@ -57,6 +57,22 @@ public class KotlinFunctionFindUsagesOptions(project: Project): KotlinCallableFi
}
}
fun KotlinFunctionFindUsagesOptions.toJavaMethodOptions(project: Project): JavaMethodFindUsagesOptions {
val javaOptions = JavaMethodFindUsagesOptions(project)
javaOptions.fastTrack = fastTrack
javaOptions.isCheckDeepInheritance = isCheckDeepInheritance
javaOptions.isImplementingMethods = isImplementingMethods
javaOptions.isIncludeInherited = isIncludeInherited
javaOptions.isIncludeOverloadUsages = isIncludeOverloadUsages
javaOptions.isOverridingMethods = isOverridingMethods
javaOptions.isSearchForTextOccurrences = isSearchForTextOccurrences
javaOptions.isSkipImportStatements = isSkipImportStatements
javaOptions.isUsages = isUsages
javaOptions.searchScope = searchScope
return javaOptions
}
public class KotlinPropertyFindUsagesOptions(project: Project): KotlinCallableFindUsagesOptions, JavaVariableFindUsagesOptions(project) {
override var searchOverrides: Boolean = false
}
@@ -0,0 +1,75 @@
/*
* Copyright 2010-2014 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.jet.plugin.findUsages.handlers
import com.intellij.find.findUsages.FindUsagesHandler
import com.intellij.psi.PsiElement
import com.intellij.usageView.UsageInfo
import com.intellij.util.Processor
import com.intellij.find.findUsages.FindUsagesOptions
import org.jetbrains.jet.plugin.findUsages.KotlinFindUsagesHandlerFactory
import kotlin.properties.Delegates
import org.jetbrains.jet.lang.psi.JetNamedDeclaration
import com.intellij.psi.PsiMethod
import com.intellij.find.findUsages.JavaFindUsagesHandler
import com.intellij.find.findUsages.JavaMethodFindUsagesOptions
import org.jetbrains.jet.plugin.findUsages.KotlinFunctionFindUsagesOptions
import org.jetbrains.jet.plugin.findUsages.toJavaMethodOptions
import com.intellij.openapi.actionSystem.DataContext
class DelegatingFindMemberUsagesHandler(
val declaration: JetNamedDeclaration,
val elementsToSearch: Collection<PsiElement>,
val factory: KotlinFindUsagesHandlerFactory
) : FindUsagesHandler(declaration) {
private val kotlinHandler = KotlinFindMemberUsagesHandler.getInstance(declaration, elementsToSearch, factory)
private val javaHandler: JavaFindUsagesHandler by Delegates.lazy {
JavaFindUsagesHandler(declaration, elementsToSearch.copyToArray(), factory.javaHandlerFactory)
}
fun getHandler(element: PsiElement): FindUsagesHandler? =
when (element) {
is JetNamedDeclaration ->
KotlinFindMemberUsagesHandler.getInstance(element, elementsToSearch, factory)
is PsiMethod ->
JavaFindUsagesHandler(element, elementsToSearch.copyToArray(), factory.javaHandlerFactory)
else -> null
}
override fun getPrimaryElements(): Array<PsiElement> =
kotlinHandler.getPrimaryElements()
override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions {
return kotlinHandler.getFindUsagesOptions(dataContext)
}
override fun processElementUsages(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
val handler = getHandler(element)
if (handler == null) return true
val handlerOptions = when (handler) {
/* Can't have KotlinPropertyFindUsagesOptions here since Kotlin properties do not override java methods, so
* elementsToSearch contains property declarations only */
is JavaFindUsagesHandler -> (options as KotlinFunctionFindUsagesOptions).toJavaMethodOptions(element.getProject())
else -> options
}
return handler.processElementUsages(element, processor, handlerOptions)
}
}