Cleanup: KotlinFindMemberUsagesHandler
This commit is contained in:
+58
-31
@@ -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<T : KtNamedDeclaration>
|
||||
protected constructor(declaration: T, elementsToSearch: Collection<PsiElement>, factory: KotlinFindUsagesHandlerFactory)
|
||||
: KotlinFindUsagesHandler<T>(declaration, elementsToSearch, factory) {
|
||||
abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration> protected constructor(
|
||||
declaration: T,
|
||||
elementsToSearch: Collection<PsiElement>,
|
||||
factory: KotlinFindUsagesHandlerFactory
|
||||
) : KotlinFindUsagesHandler<T>(declaration, elementsToSearch, factory) {
|
||||
|
||||
private class Function(declaration: KtFunction,
|
||||
elementsToSearch: Collection<PsiElement>,
|
||||
factory: KotlinFindUsagesHandlerFactory) : KotlinFindMemberUsagesHandler<KtFunction>(declaration, elementsToSearch, factory) {
|
||||
private class Function(
|
||||
declaration: KtFunction,
|
||||
elementsToSearch: Collection<PsiElement>,
|
||||
factory: KotlinFindUsagesHandlerFactory
|
||||
) : KotlinFindMemberUsagesHandler<KtFunction>(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<T : KtNamedDeclaration>
|
||||
|
||||
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<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 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<PsiReference>): Query<PsiReference> {
|
||||
@@ -102,8 +124,7 @@ abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration>
|
||||
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<T : KtNamedDeclaration>
|
||||
}
|
||||
|
||||
private inner class MySearcher(
|
||||
element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions
|
||||
element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions
|
||||
) : Searcher(element, processor, options) {
|
||||
|
||||
private val kotlinOptions = options as KotlinCallableFindUsagesOptions
|
||||
@@ -143,9 +164,7 @@ abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration>
|
||||
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<T : KtNamedDeclaration>
|
||||
}
|
||||
|
||||
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<T : KtNamedDeclaration>
|
||||
|
||||
protected abstract fun createKotlinReferencesSearchOptions(options: FindUsagesOptions): KotlinReferencesSearchOptions
|
||||
|
||||
protected abstract fun applyQueryFilters(element: PsiElement,
|
||||
options: FindUsagesOptions,
|
||||
query: Query<PsiReference>): Query<PsiReference>
|
||||
protected abstract fun applyQueryFilters(
|
||||
element: PsiElement,
|
||||
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> {
|
||||
val callableDescriptor = (target as? KtCallableDeclaration)?.resolveToDescriptorIfAny() as? CallableDescriptor
|
||||
val descriptorsToHighlight = if (callableDescriptor is ParameterDescriptor)
|
||||
listOf(callableDescriptor)
|
||||
else
|
||||
callableDescriptor?.findOriginalTopMostOverriddenDescriptors() ?: emptyList<CallableDescriptor>()
|
||||
callableDescriptor?.findOriginalTopMostOverriddenDescriptors() ?: emptyList()
|
||||
|
||||
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)
|
||||
handler?.findReferencesToHighlight(it, searchScope) ?: emptyList()
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
super.findReferencesToHighlight(target, searchScope)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun getInstance(declaration: KtNamedDeclaration,
|
||||
elementsToSearch: Collection<PsiElement> = emptyList(),
|
||||
factory: KotlinFindUsagesHandlerFactory): KotlinFindMemberUsagesHandler<out KtNamedDeclaration> {
|
||||
fun getInstance(
|
||||
declaration: KtNamedDeclaration,
|
||||
elementsToSearch: Collection<PsiElement> = emptyList(),
|
||||
factory: KotlinFindUsagesHandlerFactory
|
||||
): KotlinFindMemberUsagesHandler<out KtNamedDeclaration> {
|
||||
return if (declaration is KtFunction)
|
||||
Function(declaration, elementsToSearch, factory)
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user