remove enclosing read action around the entire method references search, use more fine-grained read actions (KT-7917); more cleanup
This commit is contained in:
+2
-1
@@ -153,9 +153,10 @@ public class KotlinPsiSearchHelper(private val project: Project): PsiSearchHelpe
|
||||
}
|
||||
|
||||
public fun processFilesWithText(item: UsagesSearchRequestItem, consumer: Processor<PsiReference>): Boolean {
|
||||
val scope = runReadAction { item.target.effectiveScope }
|
||||
return item.words.all { word ->
|
||||
val textProcessor = ResultTextProcessorImpl(item, consumer)
|
||||
processElementsWithWord(textProcessor, item.target.effectiveScope, word, item.target.location.searchContext, true)
|
||||
processElementsWithWord(textProcessor, scope, word, item.target.location.searchContext, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,14 +56,14 @@ public class KotlinFindUsagesHandlerFactory(project: Project) : FindUsagesHandle
|
||||
is JetNamedFunction, is JetProperty, is JetParameter, is JetConstructor<*> -> {
|
||||
val declaration = element as JetNamedDeclaration
|
||||
|
||||
if (forHighlightUsages) return KotlinFindMemberUsagesHandler.getInstance(declaration, this)
|
||||
if (forHighlightUsages) return KotlinFindMemberUsagesHandler.getInstance(declaration, factory = this)
|
||||
JetRefactoringUtil.checkSuperMethods(declaration, null, "super.methods.action.key.find.usages")?.let { callables ->
|
||||
when (callables.size()) {
|
||||
0 -> FindUsagesHandler.NULL_HANDLER
|
||||
1 -> {
|
||||
val target = callables.first().unwrapped ?: return FindUsagesHandler.NULL_HANDLER
|
||||
if (target is JetNamedDeclaration) {
|
||||
KotlinFindMemberUsagesHandler.getInstance(target, this)
|
||||
KotlinFindMemberUsagesHandler.getInstance(target, factory = this)
|
||||
}
|
||||
else {
|
||||
javaHandlerFactory.createFindUsagesHandler(target, false)
|
||||
|
||||
+32
-54
@@ -31,6 +31,7 @@ import com.intellij.usageView.UsageInfo
|
||||
import com.intellij.util.CommonProcessors
|
||||
import com.intellij.util.Processor
|
||||
import org.jetbrains.kotlin.asJava.LightClassUtil
|
||||
import org.jetbrains.kotlin.asJava.toLightMethods
|
||||
import org.jetbrains.kotlin.idea.findUsages.*
|
||||
import org.jetbrains.kotlin.idea.findUsages.dialogs.KotlinFindFunctionUsagesDialog
|
||||
import org.jetbrains.kotlin.idea.findUsages.dialogs.KotlinFindPropertyUsagesDialog
|
||||
@@ -39,6 +40,7 @@ import org.jetbrains.kotlin.idea.search.declarationsSearch.HierarchySearchReques
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearch
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearchHelper
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearchRequest
|
||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import java.util.Collections
|
||||
|
||||
@@ -58,9 +60,9 @@ public abstract class KotlinFindMemberUsagesHandler<T : JetNamedDeclaration>
|
||||
|
||||
override fun getFindUsagesDialog(isSingleFile: Boolean, toShowInNewTab: Boolean, mustOpenInNewTab: Boolean): AbstractFindUsagesDialog {
|
||||
val options = factory.findFunctionOptions
|
||||
val lightMethods = getLightMethods(getElement())!!.iterator()
|
||||
if (lightMethods.hasNext()) {
|
||||
return KotlinFindFunctionUsagesDialog(lightMethods.next(), getProject(), options, toShowInNewTab, mustOpenInNewTab, isSingleFile, this)
|
||||
val lightMethod = getElement().toLightMethods().firstOrNull()
|
||||
if (lightMethod != null) {
|
||||
return KotlinFindFunctionUsagesDialog(lightMethod, getProject(), options, toShowInNewTab, mustOpenInNewTab, isSingleFile, this)
|
||||
}
|
||||
|
||||
return super.getFindUsagesDialog(isSingleFile, toShowInNewTab, mustOpenInNewTab)
|
||||
@@ -83,74 +85,50 @@ public abstract class KotlinFindMemberUsagesHandler<T : JetNamedDeclaration>
|
||||
protected abstract fun getSearchHelper(options: KotlinCallableFindUsagesOptions): UsagesSearchHelper<T>
|
||||
|
||||
override fun searchReferences(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
|
||||
return ApplicationManager.getApplication().runReadAction(object : Computable<Boolean> {
|
||||
override fun compute(): Boolean? {
|
||||
val kotlinOptions = options as KotlinCallableFindUsagesOptions
|
||||
val kotlinOptions = options as KotlinCallableFindUsagesOptions
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
val request = getSearchHelper(kotlinOptions).newRequest(options.toSearchTarget<T>(element as T, true))
|
||||
val request = runReadAction {
|
||||
@suppress("UNCHECKED_CAST")
|
||||
getSearchHelper(kotlinOptions).newRequest(options.toSearchTarget<T>(element as T, true))
|
||||
}
|
||||
|
||||
val uniqueProcessor = CommonProcessors.UniqueProcessor(processor)
|
||||
val uniqueProcessor = CommonProcessors.UniqueProcessor(processor)
|
||||
|
||||
for (ref in UsagesSearch.search(request)) {
|
||||
KotlinFindUsagesHandler.Companion.processUsage(uniqueProcessor, ref)
|
||||
}
|
||||
for (ref in UsagesSearch.search(request)) {
|
||||
KotlinFindUsagesHandler.processUsage(uniqueProcessor, ref)
|
||||
}
|
||||
|
||||
val psiMethod = if (element is PsiMethod)
|
||||
element
|
||||
else if (element is JetConstructor<*>)
|
||||
LightClassUtil.getLightClassMethod(element as JetFunction)
|
||||
else
|
||||
null
|
||||
if (psiMethod != null) {
|
||||
for (ref in MethodReferencesSearch.search(psiMethod, options.searchScope, true)) {
|
||||
KotlinFindUsagesHandler.Companion.processUsage(uniqueProcessor, ref.getElement())
|
||||
}
|
||||
}
|
||||
|
||||
if (kotlinOptions.searchOverrides) {
|
||||
HierarchySearchRequest<PsiElement>(element, options.searchScope, true).searchOverriders().forEach(PsiElementProcessorAdapter(object : PsiElementProcessor<PsiMethod> {
|
||||
override fun execute(method: PsiMethod): Boolean {
|
||||
return KotlinFindUsagesHandler.Companion.processUsage(uniqueProcessor, method.getNavigationElement())
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
return true
|
||||
val psiMethod: PsiMethod? = when (element) {
|
||||
is PsiMethod -> element
|
||||
is JetConstructor<*> -> LightClassUtil.getLightClassMethod(element as JetFunction)
|
||||
else -> null
|
||||
}
|
||||
if (psiMethod != null) {
|
||||
for (ref in MethodReferencesSearch.search(psiMethod, options.searchScope, true)) {
|
||||
KotlinFindUsagesHandler.processUsage(uniqueProcessor, ref)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (kotlinOptions.searchOverrides) {
|
||||
for (method in HierarchySearchRequest(element, options.searchScope, true).searchOverriders()) {
|
||||
if (!KotlinFindUsagesHandler.processUsage(uniqueProcessor, method.getNavigationElement())) break
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun isSearchForTextOccurencesAvailable(psiElement: PsiElement, isSingleFile: Boolean): Boolean = !isSingleFile
|
||||
|
||||
companion object {
|
||||
|
||||
internal fun getLightMethods(element: JetNamedDeclaration): Iterable<PsiMethod>? = when(element) {
|
||||
is JetFunction -> {
|
||||
val method = LightClassUtil.getLightClassMethod(element)
|
||||
if (method != null) listOf(method) else emptyList<PsiMethod>()
|
||||
}
|
||||
|
||||
is JetProperty ->
|
||||
LightClassUtil.getLightClassPropertyMethods(element)
|
||||
|
||||
is JetParameter ->
|
||||
LightClassUtil.getLightClassPropertyMethods(element)
|
||||
|
||||
else -> null
|
||||
}
|
||||
|
||||
public fun getInstance(declaration: JetNamedDeclaration,
|
||||
elementsToSearch: Collection<PsiElement>,
|
||||
elementsToSearch: Collection<PsiElement> = emptyList(),
|
||||
factory: KotlinFindUsagesHandlerFactory): KotlinFindMemberUsagesHandler<out JetNamedDeclaration> {
|
||||
return if (declaration is JetFunction)
|
||||
Function(declaration, elementsToSearch, factory)
|
||||
else
|
||||
Property(declaration, elementsToSearch, factory)
|
||||
}
|
||||
|
||||
public fun getInstance(declaration: JetNamedDeclaration, factory: KotlinFindUsagesHandlerFactory): KotlinFindMemberUsagesHandler<out JetNamedDeclaration> {
|
||||
return getInstance(declaration, emptyList<PsiElement>(), factory)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import com.intellij.psi.search.SearchScope
|
||||
import com.intellij.usageView.UsageInfo
|
||||
import com.intellij.util.Processor
|
||||
import org.jetbrains.kotlin.idea.findUsages.KotlinFindUsagesHandlerFactory
|
||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.Collections
|
||||
@@ -53,16 +54,12 @@ public abstract class KotlinFindUsagesHandler<T : PsiElement>(psiElement: T,
|
||||
protected fun searchTextOccurrences(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
|
||||
val scope = options.searchScope
|
||||
|
||||
val searchText = options.isSearchForTextOccurrences && scope is GlobalSearchScope
|
||||
|
||||
if (searchText) {
|
||||
if (options.fastTrack != null) {
|
||||
options.fastTrack.searchCustom {
|
||||
processUsagesInText(element, processor, scope as GlobalSearchScope)
|
||||
}
|
||||
if (options.isSearchForTextOccurrences && scope is GlobalSearchScope) {
|
||||
if (options.fastTrack == null) {
|
||||
return processUsagesInText(element, processor, scope)
|
||||
}
|
||||
else {
|
||||
return processUsagesInText(element, processor, scope as GlobalSearchScope)
|
||||
options.fastTrack.searchCustom {
|
||||
processUsagesInText(element, processor, scope)
|
||||
}
|
||||
}
|
||||
return true
|
||||
@@ -94,12 +91,13 @@ public abstract class KotlinFindUsagesHandler<T : PsiElement>(psiElement: T,
|
||||
|
||||
protected fun processUsage(processor: Processor<UsageInfo>, ref: PsiReference?): Boolean {
|
||||
if (ref == null) return true
|
||||
val rangeInElement = ref.getRangeInElement()
|
||||
return processor.process(UsageInfo(ref.getElement(), rangeInElement.getStartOffset(), rangeInElement.getEndOffset(), false))
|
||||
val usageInfo = runReadAction {
|
||||
val rangeInElement = ref.getRangeInElement()
|
||||
UsageInfo(ref.getElement(), rangeInElement.getStartOffset(), rangeInElement.getEndOffset(), false)
|
||||
}
|
||||
return processor.process(usageInfo)
|
||||
}
|
||||
|
||||
protected fun processUsage(processor: Processor<UsageInfo>, element: PsiElement): Boolean {
|
||||
return processor.process(UsageInfo(element))
|
||||
}
|
||||
protected fun processUsage(processor: Processor<UsageInfo>, element: PsiElement): Boolean = processor.process(UsageInfo(element))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user