Skip to content

Commit

Permalink
对部分类的继承和实现去除了unused,挪了下文件位置
Browse files Browse the repository at this point in the history
  • Loading branch information
EmptyIrony committed Aug 12, 2024
1 parent 4cf1a61 commit 2f98904
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package org.tabooproject.intellij.component
package org.tabooproject.intellij.suppressor

import com.intellij.codeInspection.InspectionSuppressor
import com.intellij.codeInspection.SuppressQuickFix
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.KtAnnotated
import org.tabooproject.intellij.findContainingAnnotated


private val ANNOTATIONS = hashSetOf(
"SubscribeEvent",
"Schedule",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.tabooproject.intellij.suppressor

import com.intellij.codeInspection.InspectionSuppressor
import com.intellij.codeInspection.SuppressQuickFix
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode

private const val INSPECTION = "unused"

private val classes = listOf(
"taboolib.platform.compat.PlaceholderExpansion",
"taboolib.common.platform.Plugin",
)

class ExpansionUnusedSuppressor: InspectionSuppressor {
override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean {
if (toolId != INSPECTION) {
return false
}

return classes.any { className ->
checkIfClassImplementsOrExtends(element, className)
}
}

override fun getSuppressActions(element: PsiElement?, toolId: String): Array<SuppressQuickFix> =
SuppressQuickFix.EMPTY_ARRAY

private fun checkIfClassImplementsOrExtends(element: PsiElement, className: String): Boolean {
val ktClass = PsiTreeUtil.getParentOfType(element, KtClassOrObject::class.java) ?: return false
val context = ktClass.getResolutionFacade().analyze(ktClass, BodyResolveMode.FULL)
return ktClass.implementsInterface(className, context) || ktClass.isSubclassOf(className, context)
}

private fun KtClassOrObject.isSubclassOf(className: String, context: BindingContext): Boolean {
if (fqName?.asString() == className) return true

val superTypes = this.superTypeListEntries
return superTypes.any { typeEntry ->
val typeReference = typeEntry.typeReference
val typeFqName = typeReference?.getFqName(context)
typeFqName == className
}
}

private fun KtClassOrObject.implementsInterface(interfaceName: String, context: BindingContext): Boolean {
if (isSubclassOf(interfaceName, context)) return true

val superTypes = this.superTypeListEntries
return superTypes.any { typeEntry ->
val typeReference = typeEntry.typeReference
val typeFqName = typeReference?.getFqName(context)
typeFqName == interfaceName
}
}

private fun KtTypeReference.getFqName(context: BindingContext): String? {
val type = context[BindingContext.TYPE, this]
return type?.constructor?.declarationDescriptor?.fqNameSafe?.asString()
}
}
4 changes: 3 additions & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
<extensions defaultExtensionNs="com.intellij">
<moduleBuilder id="TABOO_PROJECT_BUILDER" builderClass="org.tabooproject.intellij.ProjectBuilder"/>
<lang.inspectionSuppressor language="kotlin"
implementationClass="org.tabooproject.intellij.component.AnnotatedUnusedSuppressor"/>
implementationClass="org.tabooproject.intellij.suppressor.AnnotatedUnusedSuppressor"/>
<lang.inspectionSuppressor language="kotlin"
implementationClass="org.tabooproject.intellij.suppressor.ExpansionUnusedSuppressor"/>
</extensions>

</idea-plugin>

0 comments on commit 2f98904

Please sign in to comment.