Searching for property accessors, too.

This commit is contained in:
Evgeny Gerashchenko
2015-01-19 16:25:05 +03:00
parent 8fd5e72632
commit b0e2afc8cb
3 changed files with 32 additions and 5 deletions
@@ -46,6 +46,7 @@ import org.jetbrains.kotlin.idea.search.usagesSearch.INVOKE_OPERATION_NAME
import org.jetbrains.kotlin.psi.JetEnumEntry import org.jetbrains.kotlin.psi.JetEnumEntry
import org.jetbrains.kotlin.psi.JetProperty import org.jetbrains.kotlin.psi.JetProperty
import org.jetbrains.kotlin.idea.search.usagesSearch.PropertyUsagesSearchHelper import org.jetbrains.kotlin.idea.search.usagesSearch.PropertyUsagesSearchHelper
import org.jetbrains.kotlin.idea.search.usagesSearch.getAccessorNames
public class UnusedSymbolInspection : AbstractKotlinInspection() { public class UnusedSymbolInspection : AbstractKotlinInspection() {
private val javaInspection = UnusedDeclarationInspection() private val javaInspection = UnusedDeclarationInspection()
@@ -137,15 +138,21 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
val useScope = declaration.getUseScope() val useScope = declaration.getUseScope()
if (useScope is GlobalSearchScope) { if (useScope is GlobalSearchScope) {
val searchCostResult = psiSearchHelper.isCheapEnoughToSearch(declaration.getName(), useScope, null, null) var zeroOccurrences = true
when (searchCostResult) { for (name in listOf(declaration.getName()) + declaration.getAccessorNames()) {
ZERO_OCCURRENCES -> return false // function is surely unused when (psiSearchHelper.isCheapEnoughToSearch(name, useScope, null, null)) {
FEW_OCCURRENCES -> {} // do search (following code) ZERO_OCCURRENCES -> {} // go on, check other names
FEW_OCCURRENCES -> zeroOccurrences = false
TOO_MANY_OCCURRENCES -> return true // searching usages is too expensive; behave like it is used TOO_MANY_OCCURRENCES -> return true // searching usages is too expensive; behave like it is used
} }
} }
if (zeroOccurrences) {
return false
}
}
val searchHelper: UsagesSearchHelper<out JetNamedDeclaration> = when (declaration) { val searchHelper: UsagesSearchHelper<out JetNamedDeclaration> = when (declaration) {
is JetClass -> ClassUsagesSearchHelper(constructorUsages = true, nonConstructorUsages = true, skipImports = true) is JetClass -> ClassUsagesSearchHelper(constructorUsages = true, nonConstructorUsages = true, skipImports = true)
is JetNamedFunction -> FunctionUsagesSearchHelper(skipImports = true) is JetNamedFunction -> FunctionUsagesSearchHelper(skipImports = true)
@@ -0,0 +1,12 @@
package test;
import foo.FooPackage;
import foo.Obj;
class usedInJava {
public static void main(String[] args) {
FooPackage.getUsedByGetter();
FooPackage.setUsedBySetter(":|");
System.out.println(Obj.CONST);
}
}
@@ -0,0 +1,8 @@
package foo
val usedByGetter = ":)"
var usedBySetter = ":)"
object Obj {
val CONST = ":)"
}