From a46e4f314284eb1fb0bdddf381f0e87b30e142da Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 20 Jun 2018 17:59:57 +0300 Subject: [PATCH] Cleanup: KotlinFindMemberUsagesHandler --- .../handlers/KotlinFindMemberUsagesHandler.kt | 89 ++++++++++++------- 1 file changed, 58 insertions(+), 31 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/findUsages/handlers/KotlinFindMemberUsagesHandler.kt b/idea/src/org/jetbrains/kotlin/idea/findUsages/handlers/KotlinFindMemberUsagesHandler.kt index 6d4acebed66..e2b9baff744 100644 --- a/idea/src/org/jetbrains/kotlin/idea/findUsages/handlers/KotlinFindMemberUsagesHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/findUsages/handlers/KotlinFindMemberUsagesHandler.kt @@ -53,17 +53,25 @@ import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.findOriginalTopMostOverriddenDescriptors import org.jetbrains.kotlin.resolve.source.getPsi -abstract class KotlinFindMemberUsagesHandler - protected constructor(declaration: T, elementsToSearch: Collection, factory: KotlinFindUsagesHandlerFactory) - : KotlinFindUsagesHandler(declaration, elementsToSearch, factory) { +abstract class KotlinFindMemberUsagesHandler protected constructor( + declaration: T, + elementsToSearch: Collection, + factory: KotlinFindUsagesHandlerFactory +) : KotlinFindUsagesHandler(declaration, elementsToSearch, factory) { - private class Function(declaration: KtFunction, - elementsToSearch: Collection, - factory: KotlinFindUsagesHandlerFactory) : KotlinFindMemberUsagesHandler(declaration, elementsToSearch, factory) { + private class Function( + declaration: KtFunction, + elementsToSearch: Collection, + factory: KotlinFindUsagesHandlerFactory + ) : KotlinFindMemberUsagesHandler(declaration, elementsToSearch, factory) { 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 lightMethod = getElement().toLightMethods().firstOrNull() if (lightMethod != null) { @@ -75,9 +83,7 @@ abstract class KotlinFindMemberUsagesHandler override fun createKotlinReferencesSearchOptions(options: FindUsagesOptions): KotlinReferencesSearchOptions { val kotlinOptions = options as KotlinFunctionFindUsagesOptions - return KotlinReferencesSearchOptions(true, - kotlinOptions.isIncludeOverloadUsages, - kotlinOptions.isIncludeOverloadUsages) + return KotlinReferencesSearchOptions(true, kotlinOptions.isIncludeOverloadUsages, kotlinOptions.isIncludeOverloadUsages) } override fun applyQueryFilters(element: PsiElement, options: FindUsagesOptions, query: Query): Query { @@ -87,12 +93,28 @@ abstract class KotlinFindMemberUsagesHandler } } - private class Property(declaration: KtNamedDeclaration, elementsToSearch: Collection, factory: KotlinFindUsagesHandlerFactory) : KotlinFindMemberUsagesHandler(declaration, elementsToSearch, factory) { + private class Property( + declaration: KtNamedDeclaration, + elementsToSearch: Collection, + factory: KotlinFindUsagesHandlerFactory + ) : KotlinFindMemberUsagesHandler(declaration, elementsToSearch, factory) { override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions = factory.findPropertyOptions - override fun getFindUsagesDialog(isSingleFile: Boolean, toShowInNewTab: Boolean, mustOpenInNewTab: Boolean): AbstractFindUsagesDialog { - return KotlinFindPropertyUsagesDialog(getElement(), project, factory.findPropertyOptions, toShowInNewTab, mustOpenInNewTab, isSingleFile, this) + override fun getFindUsagesDialog( + 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): Query { @@ -102,8 +124,7 @@ abstract class KotlinFindMemberUsagesHandler return EmptyQuery() } - val result = query - .applyFilter(kotlinOptions.isSkipImportStatements) { !it.isImportUsage() } + val result = query.applyFilter(kotlinOptions.isSkipImportStatements) { !it.isImportUsage() } if (!kotlinOptions.isReadAccess || !kotlinOptions.isWriteAccess) { val detector = KotlinReadWriteAccessDetector() @@ -130,7 +151,7 @@ abstract class KotlinFindMemberUsagesHandler } private inner class MySearcher( - element: PsiElement, processor: Processor, options: FindUsagesOptions + element: PsiElement, processor: Processor, options: FindUsagesOptions ) : Searcher(element, processor, options) { private val kotlinOptions = options as KotlinCallableFindUsagesOptions @@ -143,9 +164,7 @@ abstract class KotlinFindMemberUsagesHandler val kotlinSearchOptions = createKotlinReferencesSearchOptions(options) val searchParameters = KotlinReferencesSearchParameters(element, options.searchScope, kotlinOptions = kotlinSearchOptions) - applyQueryFilters(element, options, ReferencesSearch.search(searchParameters)).let { query -> - addTask { query.forEach(referenceProcessor) } - } + addTask { applyQueryFilters(element, options, ReferencesSearch.search(searchParameters)).forEach(referenceProcessor) } if (element is KtElement && !isOnlyKotlinSearch(options.searchScope)) { // TODO: very bad code!! ReferencesSearch does not work correctly for constructors and annotation parameters @@ -156,8 +175,12 @@ abstract class KotlinFindMemberUsagesHandler } for (psiMethod in element.toLightMethods()) { - applyQueryFilters(element, options, MethodReferencesSearch.search(psiMethod, psiMethodScopeSearch, true)).let { query -> - addTask { query.forEach(referenceProcessor) } + addTask { + applyQueryFilters( + element, + options, + MethodReferencesSearch.search(psiMethod, psiMethodScopeSearch, true) + ).forEach(referenceProcessor) } } } @@ -179,18 +202,21 @@ abstract class KotlinFindMemberUsagesHandler protected abstract fun createKotlinReferencesSearchOptions(options: FindUsagesOptions): KotlinReferencesSearchOptions - protected abstract fun applyQueryFilters(element: PsiElement, - options: FindUsagesOptions, - query: Query): Query + protected abstract fun applyQueryFilters( + element: PsiElement, + options: FindUsagesOptions, + query: Query + ): Query - 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 { val callableDescriptor = (target as? KtCallableDeclaration)?.resolveToDescriptorIfAny() as? CallableDescriptor val descriptorsToHighlight = if (callableDescriptor is ParameterDescriptor) listOf(callableDescriptor) else - callableDescriptor?.findOriginalTopMostOverriddenDescriptors() ?: emptyList() + callableDescriptor?.findOriginalTopMostOverriddenDescriptors() ?: emptyList() val baseDeclarations = descriptorsToHighlight.map { it.source.getPsi() }.filter { it != null && it != target } @@ -199,17 +225,18 @@ abstract class KotlinFindMemberUsagesHandler val handler = (FindManager.getInstance(project) as FindManagerImpl).findUsagesManager.getFindUsagesHandler(it!!, true) handler?.findReferencesToHighlight(it, searchScope) ?: emptyList() } - } - else { + } else { super.findReferencesToHighlight(target, searchScope) } } companion object { - fun getInstance(declaration: KtNamedDeclaration, - elementsToSearch: Collection = emptyList(), - factory: KotlinFindUsagesHandlerFactory): KotlinFindMemberUsagesHandler { + fun getInstance( + declaration: KtNamedDeclaration, + elementsToSearch: Collection = emptyList(), + factory: KotlinFindUsagesHandlerFactory + ): KotlinFindMemberUsagesHandler { return if (declaration is KtFunction) Function(declaration, elementsToSearch, factory) else