Cleanup: KotlinFindMemberUsagesHandler

This commit is contained in:
Mikhail Glukhikh
2018-06-20 17:59:57 +03:00
parent cc2869988f
commit a46e4f3142
@@ -53,17 +53,25 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.findOriginalTopMostOverriddenDescriptors import org.jetbrains.kotlin.resolve.findOriginalTopMostOverriddenDescriptors
import org.jetbrains.kotlin.resolve.source.getPsi import org.jetbrains.kotlin.resolve.source.getPsi
abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration> abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration> protected constructor(
protected constructor(declaration: T, elementsToSearch: Collection<PsiElement>, factory: KotlinFindUsagesHandlerFactory) declaration: T,
: KotlinFindUsagesHandler<T>(declaration, elementsToSearch, factory) { elementsToSearch: Collection<PsiElement>,
factory: KotlinFindUsagesHandlerFactory
) : KotlinFindUsagesHandler<T>(declaration, elementsToSearch, factory) {
private class Function(declaration: KtFunction, private class Function(
elementsToSearch: Collection<PsiElement>, declaration: KtFunction,
factory: KotlinFindUsagesHandlerFactory) : KotlinFindMemberUsagesHandler<KtFunction>(declaration, elementsToSearch, factory) { elementsToSearch: Collection<PsiElement>,
factory: KotlinFindUsagesHandlerFactory
) : KotlinFindMemberUsagesHandler<KtFunction>(declaration, elementsToSearch, factory) {
override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions = factory.findFunctionOptions override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions = factory.findFunctionOptions
override fun getFindUsagesDialog(isSingleFile: Boolean, toShowInNewTab: Boolean, mustOpenInNewTab: Boolean): AbstractFindUsagesDialog { override fun getFindUsagesDialog(
isSingleFile: Boolean,
toShowInNewTab: Boolean,
mustOpenInNewTab: Boolean
): AbstractFindUsagesDialog {
val options = factory.findFunctionOptions val options = factory.findFunctionOptions
val lightMethod = getElement().toLightMethods().firstOrNull() val lightMethod = getElement().toLightMethods().firstOrNull()
if (lightMethod != null) { if (lightMethod != null) {
@@ -75,9 +83,7 @@ abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration>
override fun createKotlinReferencesSearchOptions(options: FindUsagesOptions): KotlinReferencesSearchOptions { override fun createKotlinReferencesSearchOptions(options: FindUsagesOptions): KotlinReferencesSearchOptions {
val kotlinOptions = options as KotlinFunctionFindUsagesOptions val kotlinOptions = options as KotlinFunctionFindUsagesOptions
return KotlinReferencesSearchOptions(true, return KotlinReferencesSearchOptions(true, kotlinOptions.isIncludeOverloadUsages, kotlinOptions.isIncludeOverloadUsages)
kotlinOptions.isIncludeOverloadUsages,
kotlinOptions.isIncludeOverloadUsages)
} }
override fun applyQueryFilters(element: PsiElement, options: FindUsagesOptions, query: Query<PsiReference>): Query<PsiReference> { override fun applyQueryFilters(element: PsiElement, options: FindUsagesOptions, query: Query<PsiReference>): Query<PsiReference> {
@@ -87,12 +93,28 @@ abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration>
} }
} }
private class Property(declaration: KtNamedDeclaration, elementsToSearch: Collection<PsiElement>, factory: KotlinFindUsagesHandlerFactory) : KotlinFindMemberUsagesHandler<KtNamedDeclaration>(declaration, elementsToSearch, factory) { private class Property(
declaration: KtNamedDeclaration,
elementsToSearch: Collection<PsiElement>,
factory: KotlinFindUsagesHandlerFactory
) : KotlinFindMemberUsagesHandler<KtNamedDeclaration>(declaration, elementsToSearch, factory) {
override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions = factory.findPropertyOptions override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions = factory.findPropertyOptions
override fun getFindUsagesDialog(isSingleFile: Boolean, toShowInNewTab: Boolean, mustOpenInNewTab: Boolean): AbstractFindUsagesDialog { override fun getFindUsagesDialog(
return KotlinFindPropertyUsagesDialog(getElement(), project, factory.findPropertyOptions, toShowInNewTab, mustOpenInNewTab, isSingleFile, this) isSingleFile: Boolean,
toShowInNewTab: Boolean,
mustOpenInNewTab: Boolean
): AbstractFindUsagesDialog {
return KotlinFindPropertyUsagesDialog(
getElement(),
project,
factory.findPropertyOptions,
toShowInNewTab,
mustOpenInNewTab,
isSingleFile,
this
)
} }
override fun applyQueryFilters(element: PsiElement, options: FindUsagesOptions, query: Query<PsiReference>): Query<PsiReference> { override fun applyQueryFilters(element: PsiElement, options: FindUsagesOptions, query: Query<PsiReference>): Query<PsiReference> {
@@ -102,8 +124,7 @@ abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration>
return EmptyQuery() return EmptyQuery()
} }
val result = query val result = query.applyFilter(kotlinOptions.isSkipImportStatements) { !it.isImportUsage() }
.applyFilter(kotlinOptions.isSkipImportStatements) { !it.isImportUsage() }
if (!kotlinOptions.isReadAccess || !kotlinOptions.isWriteAccess) { if (!kotlinOptions.isReadAccess || !kotlinOptions.isWriteAccess) {
val detector = KotlinReadWriteAccessDetector() val detector = KotlinReadWriteAccessDetector()
@@ -130,7 +151,7 @@ abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration>
} }
private inner class MySearcher( private inner class MySearcher(
element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions
) : Searcher(element, processor, options) { ) : Searcher(element, processor, options) {
private val kotlinOptions = options as KotlinCallableFindUsagesOptions private val kotlinOptions = options as KotlinCallableFindUsagesOptions
@@ -143,9 +164,7 @@ abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration>
val kotlinSearchOptions = createKotlinReferencesSearchOptions(options) val kotlinSearchOptions = createKotlinReferencesSearchOptions(options)
val searchParameters = KotlinReferencesSearchParameters(element, options.searchScope, kotlinOptions = kotlinSearchOptions) val searchParameters = KotlinReferencesSearchParameters(element, options.searchScope, kotlinOptions = kotlinSearchOptions)
applyQueryFilters(element, options, ReferencesSearch.search(searchParameters)).let { query -> addTask { applyQueryFilters(element, options, ReferencesSearch.search(searchParameters)).forEach(referenceProcessor) }
addTask { query.forEach(referenceProcessor) }
}
if (element is KtElement && !isOnlyKotlinSearch(options.searchScope)) { if (element is KtElement && !isOnlyKotlinSearch(options.searchScope)) {
// TODO: very bad code!! ReferencesSearch does not work correctly for constructors and annotation parameters // TODO: very bad code!! ReferencesSearch does not work correctly for constructors and annotation parameters
@@ -156,8 +175,12 @@ abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration>
} }
for (psiMethod in element.toLightMethods()) { for (psiMethod in element.toLightMethods()) {
applyQueryFilters(element, options, MethodReferencesSearch.search(psiMethod, psiMethodScopeSearch, true)).let { query -> addTask {
addTask { query.forEach(referenceProcessor) } applyQueryFilters(
element,
options,
MethodReferencesSearch.search(psiMethod, psiMethodScopeSearch, true)
).forEach(referenceProcessor)
} }
} }
} }
@@ -179,18 +202,21 @@ abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration>
protected abstract fun createKotlinReferencesSearchOptions(options: FindUsagesOptions): KotlinReferencesSearchOptions protected abstract fun createKotlinReferencesSearchOptions(options: FindUsagesOptions): KotlinReferencesSearchOptions
protected abstract fun applyQueryFilters(element: PsiElement, protected abstract fun applyQueryFilters(
options: FindUsagesOptions, element: PsiElement,
query: Query<PsiReference>): Query<PsiReference> options: FindUsagesOptions,
query: Query<PsiReference>
): Query<PsiReference>
override fun isSearchForTextOccurencesAvailable(psiElement: PsiElement, isSingleFile: Boolean): Boolean = !isSingleFile && psiElement !is KtParameter override fun isSearchForTextOccurencesAvailable(psiElement: PsiElement, isSingleFile: Boolean): Boolean =
!isSingleFile && psiElement !is KtParameter
override fun findReferencesToHighlight(target: PsiElement, searchScope: SearchScope): Collection<PsiReference> { override fun findReferencesToHighlight(target: PsiElement, searchScope: SearchScope): Collection<PsiReference> {
val callableDescriptor = (target as? KtCallableDeclaration)?.resolveToDescriptorIfAny() as? CallableDescriptor val callableDescriptor = (target as? KtCallableDeclaration)?.resolveToDescriptorIfAny() as? CallableDescriptor
val descriptorsToHighlight = if (callableDescriptor is ParameterDescriptor) val descriptorsToHighlight = if (callableDescriptor is ParameterDescriptor)
listOf(callableDescriptor) listOf(callableDescriptor)
else else
callableDescriptor?.findOriginalTopMostOverriddenDescriptors() ?: emptyList<CallableDescriptor>() callableDescriptor?.findOriginalTopMostOverriddenDescriptors() ?: emptyList()
val baseDeclarations = descriptorsToHighlight.map { it.source.getPsi() }.filter { it != null && it != target } val baseDeclarations = descriptorsToHighlight.map { it.source.getPsi() }.filter { it != null && it != target }
@@ -199,17 +225,18 @@ abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration>
val handler = (FindManager.getInstance(project) as FindManagerImpl).findUsagesManager.getFindUsagesHandler(it!!, true) val handler = (FindManager.getInstance(project) as FindManagerImpl).findUsagesManager.getFindUsagesHandler(it!!, true)
handler?.findReferencesToHighlight(it, searchScope) ?: emptyList() handler?.findReferencesToHighlight(it, searchScope) ?: emptyList()
} }
} } else {
else {
super.findReferencesToHighlight(target, searchScope) super.findReferencesToHighlight(target, searchScope)
} }
} }
companion object { companion object {
fun getInstance(declaration: KtNamedDeclaration, fun getInstance(
elementsToSearch: Collection<PsiElement> = emptyList(), declaration: KtNamedDeclaration,
factory: KotlinFindUsagesHandlerFactory): KotlinFindMemberUsagesHandler<out KtNamedDeclaration> { elementsToSearch: Collection<PsiElement> = emptyList(),
factory: KotlinFindUsagesHandlerFactory
): KotlinFindMemberUsagesHandler<out KtNamedDeclaration> {
return if (declaration is KtFunction) return if (declaration is KtFunction)
Function(declaration, elementsToSearch, factory) Function(declaration, elementsToSearch, factory)
else else