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 {
|
public fun processFilesWithText(item: UsagesSearchRequestItem, consumer: Processor<PsiReference>): Boolean {
|
||||||
|
val scope = runReadAction { item.target.effectiveScope }
|
||||||
return item.words.all { word ->
|
return item.words.all { word ->
|
||||||
val textProcessor = ResultTextProcessorImpl(item, consumer)
|
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<*> -> {
|
is JetNamedFunction, is JetProperty, is JetParameter, is JetConstructor<*> -> {
|
||||||
val declaration = element as JetNamedDeclaration
|
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 ->
|
JetRefactoringUtil.checkSuperMethods(declaration, null, "super.methods.action.key.find.usages")?.let { callables ->
|
||||||
when (callables.size()) {
|
when (callables.size()) {
|
||||||
0 -> FindUsagesHandler.NULL_HANDLER
|
0 -> FindUsagesHandler.NULL_HANDLER
|
||||||
1 -> {
|
1 -> {
|
||||||
val target = callables.first().unwrapped ?: return FindUsagesHandler.NULL_HANDLER
|
val target = callables.first().unwrapped ?: return FindUsagesHandler.NULL_HANDLER
|
||||||
if (target is JetNamedDeclaration) {
|
if (target is JetNamedDeclaration) {
|
||||||
KotlinFindMemberUsagesHandler.getInstance(target, this)
|
KotlinFindMemberUsagesHandler.getInstance(target, factory = this)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
javaHandlerFactory.createFindUsagesHandler(target, false)
|
javaHandlerFactory.createFindUsagesHandler(target, false)
|
||||||
|
|||||||
+32
-54
@@ -31,6 +31,7 @@ import com.intellij.usageView.UsageInfo
|
|||||||
import com.intellij.util.CommonProcessors
|
import com.intellij.util.CommonProcessors
|
||||||
import com.intellij.util.Processor
|
import com.intellij.util.Processor
|
||||||
import org.jetbrains.kotlin.asJava.LightClassUtil
|
import org.jetbrains.kotlin.asJava.LightClassUtil
|
||||||
|
import org.jetbrains.kotlin.asJava.toLightMethods
|
||||||
import org.jetbrains.kotlin.idea.findUsages.*
|
import org.jetbrains.kotlin.idea.findUsages.*
|
||||||
import org.jetbrains.kotlin.idea.findUsages.dialogs.KotlinFindFunctionUsagesDialog
|
import org.jetbrains.kotlin.idea.findUsages.dialogs.KotlinFindFunctionUsagesDialog
|
||||||
import org.jetbrains.kotlin.idea.findUsages.dialogs.KotlinFindPropertyUsagesDialog
|
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.UsagesSearch
|
||||||
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearchHelper
|
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearchHelper
|
||||||
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearchRequest
|
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearchRequest
|
||||||
|
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import java.util.Collections
|
import java.util.Collections
|
||||||
|
|
||||||
@@ -58,9 +60,9 @@ public abstract class KotlinFindMemberUsagesHandler<T : JetNamedDeclaration>
|
|||||||
|
|
||||||
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 lightMethods = getLightMethods(getElement())!!.iterator()
|
val lightMethod = getElement().toLightMethods().firstOrNull()
|
||||||
if (lightMethods.hasNext()) {
|
if (lightMethod != null) {
|
||||||
return KotlinFindFunctionUsagesDialog(lightMethods.next(), getProject(), options, toShowInNewTab, mustOpenInNewTab, isSingleFile, this)
|
return KotlinFindFunctionUsagesDialog(lightMethod, getProject(), options, toShowInNewTab, mustOpenInNewTab, isSingleFile, this)
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.getFindUsagesDialog(isSingleFile, toShowInNewTab, mustOpenInNewTab)
|
return super.getFindUsagesDialog(isSingleFile, toShowInNewTab, mustOpenInNewTab)
|
||||||
@@ -83,74 +85,50 @@ public abstract class KotlinFindMemberUsagesHandler<T : JetNamedDeclaration>
|
|||||||
protected abstract fun getSearchHelper(options: KotlinCallableFindUsagesOptions): UsagesSearchHelper<T>
|
protected abstract fun getSearchHelper(options: KotlinCallableFindUsagesOptions): UsagesSearchHelper<T>
|
||||||
|
|
||||||
override fun searchReferences(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
|
override fun searchReferences(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
|
||||||
return ApplicationManager.getApplication().runReadAction(object : Computable<Boolean> {
|
val kotlinOptions = options as KotlinCallableFindUsagesOptions
|
||||||
override fun compute(): Boolean? {
|
|
||||||
val kotlinOptions = options as KotlinCallableFindUsagesOptions
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
val request = runReadAction {
|
||||||
val request = getSearchHelper(kotlinOptions).newRequest(options.toSearchTarget<T>(element as T, true))
|
@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)) {
|
for (ref in UsagesSearch.search(request)) {
|
||||||
KotlinFindUsagesHandler.Companion.processUsage(uniqueProcessor, ref)
|
KotlinFindUsagesHandler.processUsage(uniqueProcessor, ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
val psiMethod = if (element is PsiMethod)
|
val psiMethod: PsiMethod? = when (element) {
|
||||||
element
|
is PsiMethod -> element
|
||||||
else if (element is JetConstructor<*>)
|
is JetConstructor<*> -> LightClassUtil.getLightClassMethod(element as JetFunction)
|
||||||
LightClassUtil.getLightClassMethod(element as JetFunction)
|
else -> null
|
||||||
else
|
}
|
||||||
null
|
if (psiMethod != null) {
|
||||||
if (psiMethod != null) {
|
for (ref in MethodReferencesSearch.search(psiMethod, options.searchScope, true)) {
|
||||||
for (ref in MethodReferencesSearch.search(psiMethod, options.searchScope, true)) {
|
KotlinFindUsagesHandler.processUsage(uniqueProcessor, ref)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
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
|
override fun isSearchForTextOccurencesAvailable(psiElement: PsiElement, isSingleFile: Boolean): Boolean = !isSingleFile
|
||||||
|
|
||||||
companion object {
|
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,
|
public fun getInstance(declaration: JetNamedDeclaration,
|
||||||
elementsToSearch: Collection<PsiElement>,
|
elementsToSearch: Collection<PsiElement> = emptyList(),
|
||||||
factory: KotlinFindUsagesHandlerFactory): KotlinFindMemberUsagesHandler<out JetNamedDeclaration> {
|
factory: KotlinFindUsagesHandlerFactory): KotlinFindMemberUsagesHandler<out JetNamedDeclaration> {
|
||||||
return if (declaration is JetFunction)
|
return if (declaration is JetFunction)
|
||||||
Function(declaration, elementsToSearch, factory)
|
Function(declaration, elementsToSearch, factory)
|
||||||
else
|
else
|
||||||
Property(declaration, elementsToSearch, factory)
|
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.usageView.UsageInfo
|
||||||
import com.intellij.util.Processor
|
import com.intellij.util.Processor
|
||||||
import org.jetbrains.kotlin.idea.findUsages.KotlinFindUsagesHandlerFactory
|
import org.jetbrains.kotlin.idea.findUsages.KotlinFindUsagesHandlerFactory
|
||||||
|
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||||
|
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
import java.util.Collections
|
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 {
|
protected fun searchTextOccurrences(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
|
||||||
val scope = options.searchScope
|
val scope = options.searchScope
|
||||||
|
|
||||||
val searchText = options.isSearchForTextOccurrences && scope is GlobalSearchScope
|
if (options.isSearchForTextOccurrences && scope is GlobalSearchScope) {
|
||||||
|
if (options.fastTrack == null) {
|
||||||
if (searchText) {
|
return processUsagesInText(element, processor, scope)
|
||||||
if (options.fastTrack != null) {
|
|
||||||
options.fastTrack.searchCustom {
|
|
||||||
processUsagesInText(element, processor, scope as GlobalSearchScope)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
options.fastTrack.searchCustom {
|
||||||
return processUsagesInText(element, processor, scope as GlobalSearchScope)
|
processUsagesInText(element, processor, scope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@@ -94,12 +91,13 @@ public abstract class KotlinFindUsagesHandler<T : PsiElement>(psiElement: T,
|
|||||||
|
|
||||||
protected fun processUsage(processor: Processor<UsageInfo>, ref: PsiReference?): Boolean {
|
protected fun processUsage(processor: Processor<UsageInfo>, ref: PsiReference?): Boolean {
|
||||||
if (ref == null) return true
|
if (ref == null) return true
|
||||||
val rangeInElement = ref.getRangeInElement()
|
val usageInfo = runReadAction {
|
||||||
return processor.process(UsageInfo(ref.getElement(), rangeInElement.getStartOffset(), rangeInElement.getEndOffset(), false))
|
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 {
|
protected fun processUsage(processor: Processor<UsageInfo>, element: PsiElement): Boolean = processor.process(UsageInfo(element))
|
||||||
return processor.process(UsageInfo(element))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user