[kotlin] k2 unused symbol: process text search for class names faster

KTIJ-33981

GitOrigin-RevId: 27808734a7e5aa93520be13f6a16fb70c9474df2
This commit is contained in:
Anna Kozlova
2025-05-14 11:42:37 +00:00
committed by intellij-monorepo-bot
parent 6fa704d95d
commit 8540c07a0b
7 changed files with 60 additions and 1 deletions
@@ -152,6 +152,11 @@ public abstract class K2UnusedSymbolHighlightingTestGenerated extends AbstractK2
public void testSecondaryConstructorUnused() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/unusedSymbol/multifile/secondaryConstructorUnused.kt");
}
@TestMetadata("textUsagesClass.kt")
public void testTextUsagesClass() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/unusedSymbol/multifile/textUsagesClass.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@@ -647,6 +652,11 @@ public abstract class K2UnusedSymbolHighlightingTestGenerated extends AbstractK2
runTest("../../../idea/tests/testData/inspectionsLocal/unusedSymbol/suppressedClass.kt");
}
@TestMetadata("textUsages.kt")
public void testTextUsages() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/unusedSymbol/textUsages.kt");
}
@TestMetadata("typeAlias.kt")
public void testTypeAlias() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/unusedSymbol/typeAlias.kt");
@@ -415,10 +415,31 @@ object K2UnusedSymbolUtil {
val handler = (FindManager.getInstance(project) as FindManagerImpl).findUsagesManager.getFindUsagesHandler(declaration, true)
if (handler != null) {
fun classOrObjectHasTextUsages(classOrObject: KtClassOrObject): Boolean {
var hasTextUsages = false
// Finding text usages
if (classOrObject.useScope is GlobalSearchScope) {
handler.processUsagesInText(
classOrObject,
{ hasTextUsages = true; false },
GlobalSearchScope.projectScope(classOrObject.project)
)
}
return hasTextUsages
}
if (declaration is KtClassOrObject && classOrObjectHasTextUsages(declaration)) return true
val options = handler.findUsagesOptions
// effectively disable search for text occurrences for classes which are processed earlier but faster
options.isSearchForTextOccurrences = false
val result = handler.processElementUsages(declaration, Processor {
val refElement = it.element
refElement == null || checkReference(refElement, declaration, originalDeclaration)
}, handler.findUsagesOptions)
}, options)
if (!result) {
return true
}
@@ -17720,6 +17720,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti
public void testSecondaryConstructorUnused() throws Exception {
runTest("testData/inspectionsLocal/unusedSymbol/multifile/secondaryConstructorUnused.kt");
}
@TestMetadata("textUsagesClass.kt")
public void testTextUsagesClass() throws Exception {
runTest("testData/inspectionsLocal/unusedSymbol/multifile/textUsagesClass.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@@ -18215,6 +18220,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti
runTest("testData/inspectionsLocal/unusedSymbol/suppressedClass.kt");
}
@TestMetadata("textUsages.kt")
public void testTextUsages() throws Exception {
runTest("testData/inspectionsLocal/unusedSymbol/textUsages.kt");
}
@TestMetadata("typeAlias.kt")
public void testTypeAlias() throws Exception {
runTest("testData/inspectionsLocal/unusedSymbol/typeAlias.kt");
@@ -0,0 +1,2 @@
// PROBLEM: none
class AB<caret>C
@@ -0,0 +1,8 @@
class ABC {
internal fun f<caret>oo() {}
// use foo sometimes
}
fun main() {
ABC()
}
@@ -0,0 +1,7 @@
class ABC {
// use foo sometimes
}
fun main() {
ABC()
}