Class used in text is not unused.
This commit is contained in:
@@ -31,6 +31,9 @@ import org.jetbrains.kotlin.idea.JetBundle
|
||||
import com.intellij.codeInspection.ProblemHighlightType
|
||||
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection
|
||||
import org.jetbrains.kotlin.asJava.LightClassUtil
|
||||
import org.jetbrains.kotlin.idea.findUsages.handlers.KotlinFindClassUsagesHandler
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.kotlin.idea.findUsages.KotlinFindUsagesHandlerFactory
|
||||
|
||||
public class UnusedSymbolInspection : AbstractKotlinInspection() {
|
||||
private val javaInspection = UnusedDeclarationInspection()
|
||||
@@ -45,8 +48,10 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
|
||||
val lightClass = LightClassUtil.getPsiClass(klass)
|
||||
if (lightClass != null && javaInspection.isEntryPoint(lightClass)) return
|
||||
|
||||
val classUseScope = klass.getUseScope()
|
||||
|
||||
val usagesSearchHelper = KotlinClassFindUsagesOptions(holder.getProject()).toClassHelper()
|
||||
val request = usagesSearchHelper.newRequest(UsagesSearchTarget(klass, klass.getUseScope()))
|
||||
val request = usagesSearchHelper.newRequest(UsagesSearchTarget(klass, classUseScope))
|
||||
val query = UsagesSearch.search(request)
|
||||
|
||||
var foundNonTrivialUsage = false
|
||||
@@ -61,6 +66,16 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
|
||||
}
|
||||
})
|
||||
|
||||
// Finding text usages
|
||||
if (classUseScope is GlobalSearchScope) {
|
||||
val findClassUsagesHandler = KotlinFindClassUsagesHandler(klass, KotlinFindUsagesHandlerFactory(klass.getProject()))
|
||||
findClassUsagesHandler.processUsagesInText(
|
||||
klass,
|
||||
{ foundNonTrivialUsage = true; false },
|
||||
classUseScope
|
||||
)
|
||||
}
|
||||
|
||||
if (!foundNonTrivialUsage) {
|
||||
holder.registerProblem(
|
||||
klass.getNameIdentifier(),
|
||||
|
||||
@@ -23,4 +23,12 @@
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
|
||||
<description>Class 'Local' is never used</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>usedInTextByShortName.kt</file>
|
||||
<line>3</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/usedInTextByShortName.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
|
||||
<description>Class 'UsedInTextByShortName' is never used</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
package foo
|
||||
|
||||
class UsedInTextOnly
|
||||
@@ -0,0 +1 @@
|
||||
foo.UsedInTextOnly
|
||||
@@ -0,0 +1,3 @@
|
||||
package foo
|
||||
|
||||
class UsedInTextByShortName
|
||||
@@ -0,0 +1 @@
|
||||
UsedInTextByShortName
|
||||
@@ -53,11 +53,15 @@ public abstract class AbstractJetInspectionTest: LightCodeInsightFixtureTestCase
|
||||
setTestDataPath("${JetTestUtils.getHomeDirectory()}/$srcDir")
|
||||
|
||||
val psiFiles = srcDir
|
||||
.listFiles { it.getName().endsWith(".kt") }!!
|
||||
.listFiles { it.getName().endsWith(".kt") || it.getName().endsWith(".txt") }!!
|
||||
.map {
|
||||
file ->
|
||||
val text = FileUtil.loadFile(file, true)
|
||||
val fileText = if (text.startsWith("package")) text else "package ${file.getName().trimTrailing(".kt")};$text"
|
||||
val fileText =
|
||||
if (text.startsWith("package") || !file.getName().endsWith(".kt"))
|
||||
text
|
||||
else
|
||||
"package ${file.getName().trimTrailing(".kt")};$text"
|
||||
configureByText(file.getName(), fileText)!!
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user