Do not run unused symbol inspection if declaration in used in more than 3 scripts
This commit is contained in:
@@ -19,18 +19,27 @@ package org.jetbrains.kotlin.idea.search
|
||||
import com.intellij.openapi.fileTypes.FileType
|
||||
import com.intellij.openapi.fileTypes.FileTypeRegistry
|
||||
import com.intellij.openapi.progress.ProgressIndicator
|
||||
import com.intellij.openapi.progress.ProgressManager
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.roots.FileIndexFacade
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.impl.cache.impl.id.IdIndex
|
||||
import com.intellij.psi.impl.cache.impl.id.IdIndexEntry
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import com.intellij.psi.search.LocalSearchScope
|
||||
import com.intellij.psi.search.PsiSearchHelper
|
||||
import com.intellij.psi.search.SearchScope
|
||||
import com.intellij.psi.search.searches.ReferencesSearch
|
||||
import com.intellij.util.Processor
|
||||
import com.intellij.util.indexing.FileBasedIndex
|
||||
import org.jetbrains.kotlin.idea.KotlinFileType
|
||||
import org.jetbrains.kotlin.idea.util.compat.psiSearchHelperInstance
|
||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.script.findScriptDefinition
|
||||
import org.jetbrains.kotlin.types.expressions.OperatorConventions
|
||||
|
||||
infix fun SearchScope.and(otherScope: SearchScope): SearchScope = intersectWith(otherScope)
|
||||
@@ -108,7 +117,36 @@ fun PsiSearchHelper.isCheapEnoughToSearchConsideringOperators(
|
||||
fileToIgnoreOccurrencesIn: PsiFile?,
|
||||
progress: ProgressIndicator?
|
||||
): PsiSearchHelper.SearchCostResult {
|
||||
if (OperatorConventions.isConventionName(Name.identifier(name))) return PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES
|
||||
if (OperatorConventions.isConventionName(Name.identifier(name))) {
|
||||
return PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES
|
||||
}
|
||||
|
||||
if (!isCheapToSearchUsagesInScripts(scope.restrictToKotlinSources(), name)) {
|
||||
return PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES
|
||||
}
|
||||
|
||||
return isCheapEnoughToSearch(name, scope, fileToIgnoreOccurrencesIn, progress)
|
||||
}
|
||||
|
||||
private fun isCheapToSearchUsagesInScripts(scope: GlobalSearchScope, name: String): Boolean {
|
||||
val project = scope.project ?: return true
|
||||
|
||||
var scriptsCount = 0
|
||||
val processor = object : Processor<VirtualFile> {
|
||||
override fun process(file: VirtualFile): Boolean {
|
||||
ProgressManager.checkCanceled()
|
||||
if (findScriptDefinition(file, project) == null) return true
|
||||
return scriptsCount++ < 3
|
||||
}
|
||||
}
|
||||
|
||||
val index = FileIndexFacade.getInstance(project)
|
||||
return runReadAction {
|
||||
FileBasedIndex.getInstance().processFilesContainingAllKeys(
|
||||
IdIndex.NAME,
|
||||
listOf(IdIndexEntry(name, true)),
|
||||
scope,
|
||||
null,
|
||||
{ file -> !index.shouldBeFound(scope, file) || processor.process(file) })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user