Skip to content

Commit 2f98904

Browse files
committed
对部分类的继承和实现去除了unused,挪了下文件位置
1 parent 4cf1a61 commit 2f98904

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

src/main/kotlin/org/tabooproject/intellij/component/AnnotatedUnusedSuppressor.kt renamed to src/main/kotlin/org/tabooproject/intellij/suppressor/AnnotatedUnusedSuppressor.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package org.tabooproject.intellij.component
1+
package org.tabooproject.intellij.suppressor
22

33
import com.intellij.codeInspection.InspectionSuppressor
44
import com.intellij.codeInspection.SuppressQuickFix
55
import com.intellij.psi.PsiElement
6-
import org.jetbrains.kotlin.psi.*
6+
import org.jetbrains.kotlin.psi.KtAnnotated
77
import org.tabooproject.intellij.findContainingAnnotated
88

9-
109
private val ANNOTATIONS = hashSetOf(
1110
"SubscribeEvent",
1211
"Schedule",
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.tabooproject.intellij.suppressor
2+
3+
import com.intellij.codeInspection.InspectionSuppressor
4+
import com.intellij.codeInspection.SuppressQuickFix
5+
import com.intellij.psi.PsiElement
6+
import com.intellij.psi.util.PsiTreeUtil
7+
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
8+
import org.jetbrains.kotlin.psi.KtClassOrObject
9+
import org.jetbrains.kotlin.psi.KtTypeReference
10+
import org.jetbrains.kotlin.resolve.BindingContext
11+
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
12+
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
13+
14+
private const val INSPECTION = "unused"
15+
16+
private val classes = listOf(
17+
"taboolib.platform.compat.PlaceholderExpansion",
18+
"taboolib.common.platform.Plugin",
19+
)
20+
21+
class ExpansionUnusedSuppressor: InspectionSuppressor {
22+
override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean {
23+
if (toolId != INSPECTION) {
24+
return false
25+
}
26+
27+
return classes.any { className ->
28+
checkIfClassImplementsOrExtends(element, className)
29+
}
30+
}
31+
32+
override fun getSuppressActions(element: PsiElement?, toolId: String): Array<SuppressQuickFix> =
33+
SuppressQuickFix.EMPTY_ARRAY
34+
35+
private fun checkIfClassImplementsOrExtends(element: PsiElement, className: String): Boolean {
36+
val ktClass = PsiTreeUtil.getParentOfType(element, KtClassOrObject::class.java) ?: return false
37+
val context = ktClass.getResolutionFacade().analyze(ktClass, BodyResolveMode.FULL)
38+
return ktClass.implementsInterface(className, context) || ktClass.isSubclassOf(className, context)
39+
}
40+
41+
private fun KtClassOrObject.isSubclassOf(className: String, context: BindingContext): Boolean {
42+
if (fqName?.asString() == className) return true
43+
44+
val superTypes = this.superTypeListEntries
45+
return superTypes.any { typeEntry ->
46+
val typeReference = typeEntry.typeReference
47+
val typeFqName = typeReference?.getFqName(context)
48+
typeFqName == className
49+
}
50+
}
51+
52+
private fun KtClassOrObject.implementsInterface(interfaceName: String, context: BindingContext): Boolean {
53+
if (isSubclassOf(interfaceName, context)) return true
54+
55+
val superTypes = this.superTypeListEntries
56+
return superTypes.any { typeEntry ->
57+
val typeReference = typeEntry.typeReference
58+
val typeFqName = typeReference?.getFqName(context)
59+
typeFqName == interfaceName
60+
}
61+
}
62+
63+
private fun KtTypeReference.getFqName(context: BindingContext): String? {
64+
val type = context[BindingContext.TYPE, this]
65+
return type?.constructor?.declarationDescriptor?.fqNameSafe?.asString()
66+
}
67+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
<extensions defaultExtensionNs="com.intellij">
2121
<moduleBuilder id="TABOO_PROJECT_BUILDER" builderClass="org.tabooproject.intellij.ProjectBuilder"/>
2222
<lang.inspectionSuppressor language="kotlin"
23-
implementationClass="org.tabooproject.intellij.component.AnnotatedUnusedSuppressor"/>
23+
implementationClass="org.tabooproject.intellij.suppressor.AnnotatedUnusedSuppressor"/>
24+
<lang.inspectionSuppressor language="kotlin"
25+
implementationClass="org.tabooproject.intellij.suppressor.ExpansionUnusedSuppressor"/>
2426
</extensions>
2527

2628
</idea-plugin>

0 commit comments

Comments
 (0)