User: rednesto Date: 08 May 23 18:25 Revision: 434cea58751a01adda1e0649eb10b8d7a72ee385 Summary: Fix #2030 hang during translation search TeamCity URL: http://ci.mcdev.io:80/viewModification.html?tab=vcsModificationFiles&modId=8498&personal=false Index: src/main/kotlin/translations/reference/TranslationReferenceSearch.kt =================================================================== --- src/main/kotlin/translations/reference/TranslationReferenceSearch.kt (revision fba57df81658e5afcf439108c4ae75d6ba4d527f) +++ src/main/kotlin/translations/reference/TranslationReferenceSearch.kt (revision 434cea58751a01adda1e0649eb10b8d7a72ee385) @@ -24,20 +24,22 @@ import com.intellij.find.FindModel import com.intellij.find.impl.FindInProjectUtil import com.intellij.openapi.application.runReadAction -import com.intellij.openapi.progress.ProgressIndicator -import com.intellij.openapi.progress.ProgressManager -import com.intellij.openapi.progress.Task -import com.intellij.openapi.progress.util.ProgressIndicatorBase import com.intellij.psi.PsiReference import com.intellij.psi.search.searches.ReferencesSearch import com.intellij.usages.FindUsagesProcessPresentation import com.intellij.usages.UsageViewPresentation import com.intellij.util.Processor import com.intellij.util.QueryExecutor -import java.util.concurrent.CompletableFuture +import com.intellij.util.application class TranslationReferenceSearch : QueryExecutor { override fun execute(parameters: ReferencesSearch.SearchParameters, consumer: Processor): Boolean { + if (application.isReadAccessAllowed) { + // Skip when we're in a read action because FindInProjectUtil.findUsages forbids it + // I am unsure why we are in a read action sometimes, thankfully references still seem to resolve correctly + return true + } + val entry = parameters.elementToSearch val key = runReadAction { TranslationFiles.toTranslation(entry)?.key } ?: return true @@ -63,31 +65,22 @@ .filter { it.isNotEmpty() } .joinToString("|") { "(${Regex.escape(it)})" } - val future = CompletableFuture() - ProgressManager.getInstance().runProcessWithProgressAsynchronously( - object : Task.Backgroundable(parameters.project, "Searching for references", true) { - override fun run(indicator: ProgressIndicator) { - FindInProjectUtil.findUsages( - model, - parameters.project, - { - if (it.file != null && it.element != null && it.rangeInElement != null) { - val highlighted = it.file?.findElementAt(it.rangeInElement!!.startOffset) - val ref = highlighted?.parent?.references - ?.find { ref -> ref is TranslationReference } as TranslationReference? + FindInProjectUtil.findUsages( + model, + parameters.project, + { + if (it.file != null && it.element != null && it.rangeInElement != null) { + val highlighted = it.file?.findElementAt(it.rangeInElement!!.startOffset) + val ref = highlighted?.parent?.references + ?.find { ref -> ref is TranslationReference } as TranslationReference? - if (ref?.key?.full == key && !consumer.process(ref)) { - future.complete(false) + if (ref?.key?.full == key) { + consumer.process(ref) - } - } - true - }, - FindUsagesProcessPresentation(UsageViewPresentation()), - ) + } + } + true + }, + FindUsagesProcessPresentation(UsageViewPresentation()), + ) - future.complete(true) + return true - } + } - }, - ProgressIndicatorBase() - ) - return future.get() - } +} -}